Learn Objective-C on the Mac - Mark Dalrymple [68]
Start by creating a new application project. In Xcode, choose File➤New Project, then select Application on the left side of the window. Click to turn on the Use Core Data for storage checkbox (see Figure 7-2), and then click Choose. This procedure is slightly different for Xcode versions prior to 3.2, on Mac OS X 10.5 and earlier, where Core Data Application appears as a separate project type. Navigate to the directory where you want to save your new project, and enter MythBase as the project’s name.
Now that the project is created, we need to prepare it for garbage collection in the usual way. Select the top-level MythBase group in the navigation pane, and open the Info window by pressing ⌘I. Switch to the Build tab, select All Configurations in the Configuration popup, then start typing “garbage” in the search field to the right. When the Objective-C Garbage Collection item is shown, click the popup containing its value on the right and change it to Required.
Figure 7-2. Creating a new application project, and turning on the Core Data option
Defining the Model
At this point, you’ll have a brand-new project, similar to the ones you’ve created before. Choosing to use Core Data causes Xcode to use a slightly different project template, so this project will have a thing or two you haven’t seen in your old projects. In Xcode’s navigator pane, you’ll find a new top-level group called Models; inside that, you’ll see the project’s default empty model file, called MythBase_DataModel.xcdatamodel. A model file contains metadata about your application’s model layer. You create the model file using a graphical tool built into Xcode, and your application reads the model file at runtime.
MODELING: WHAT?
From this point, we’re going to assume that you have some idea of what object modeling or database modeling is. But on the offchance that you’re totally perplexed by this, here’s a very brief summary. The idea is that the things your application is dealing with, the content that your application is “about,” can be split into independent chunks and organized into a reasonable structure through modeling techniques. Using Core Data, you’ll want to figure out how to organize your data along the following lines:■ Entities: An entity is used to describe a uniquely identifiable “thing” in your application, something that can exist and be described and identified on its own terms. Entities are usually the big “nouns” of your system. People, companies, and monetary transactions are all entities. Eye color, market capitalization, and transaction amounts are not.
■ Attributes: Anything that seems like a descriptive feature of an entity, without referring to anything else, is probably an attribute of that entity. Eye color, market capitalization, and transaction amounts are all likely to be attributes of the aforementioned people, companies, and monetary transactions. A person’s current account balance, a company’s CEO’s phone number, and a transaction recipient’s email address are not attributes of the entities we mentioned. Each of these is better modeled by traversing a relationship to another entity and accessing an attribute found there.
■ Relationships: Use relationships to establish linkages between entities. Relationships can be one-to-many (one person may be the recipient of many transactions), many-to-one (many people can be employed by the same company), one-to-one (each person can have exactly one spouse), or many-to-many (a company can have many people as customers, and at the same time a person can be a customer of many companies).
The model file contains information that should be familiar to anyone who’s ever done any sort of object modeling, database modeling, or really any sort of entity-relationship modeling at all. First of all you have the concept of an entity, which corresponds roughly to a class in object modeling or a table in database modeling. Each entity consists