Learn Objective-C on the Mac - Mark Dalrymple [69]
Using Xcode’s Model Editor
Start by double-clicking the MythBase_DataModel.xcdatamodel file in Xcode’s navigation pane. This opens up the model file in Xcode’s built-in model editor, which resembles a blank sheet of graph paper topped by a couple of table views and some other controls, as seen in Figure 7-3.
Figure 7-3. A new model file, before creating any entities
The graph paper area is a sort of workspace, where the entities you create will be shown, along with all their attributes and relationships. The area at the top, above the graph paper, shows a list of all entities on the left, the selected entity’s properties in the middle, and detailed information about the selected entity or property (whichever was clicked last) on the right. You can edit all the model file’s important details in the upper portion, while the lower portion lets you graphically reorganize the display as you wish, so that you can visually group the entities together in a way that make sense to you.
Creating an Entity
For MythBase, we’re going to create a single entity, called MythicalPerson, with a handful of attributes. To start, create a new entity by clicking the “+” button that appears near the window’s center-left point, just below the Entity tableview. This places a new entity, called simply Entity, in the upper-left-hand table view, as well as in the middle of the graph paper area. With the new entity selected, its details are visible in the upper right. Go change the new entity’s name to “MythicalPerson,” and leave the rest of the controls in their default states. Note that the class name for the entity is NSManagedObject, a generic class included in Core Data that provides all the basic functionality for Core Data’s model objects. Later we’ll need to write some code for this entity, at which point we’ll create a custom subclass of NSManagedObject, but for now the generic class will do just fine.
Now let’s create our entity. We’ve done some object modeling and have come up with a few characteristics that a mythical person may have.
The MythicalPerson entity will have six attributes. These are shown in Table 7-1, which includes a general description of the type of each attribute, and the corresponding Core Data storage type.
Table 7-1. MythicalPerson Attributes
Core Data includes String and Date types, as well as a selection of numeric types: Int 16, Int 32, Int 64, Float, Double, Decimal, and Bool. In addition, it includes a Binary type which allows generic storage of any sort of data you may want to attach to an entity (which you pack into a binary chunk on your own), and a special Transformable type which allows many otherwise unsupported Cocoa classes, such as NSImage, to be stored with Core Data. More on that later. Note that the Core Data storage types have different names from the value classes in Cocoa (NSString, NSNumber, and the like). When Core Data attributes are read into a running application, they are converted into the nearest Cocoa equivalent, which means, for example, that all the numeric types end up as NSNumber in an application, and are converted back into the underlying storage format for saving.
Creating Attributes
Let’s start creating MythicalPerson’s attributes. Click to select the MythicalPerson entity, then click the small “+” button underneath the Property table view to add a new property. A small menu of choices pops up. Click Add Attribute and an attribute will appear in the table view. On the right, you