13.2. Generating Class Files for Core Data Entities

Problem

You followed the instructions in Recipe 13.1 and you want to know how to create code based on your object model.

Solution

Follow these steps:

  1. In Xcode, find the file with the xcdatamodel extension that was created for your application when you created the application itself in Xcode. Click on the file, and you should see the editor on the righthand side of the Xcode window.

  2. Select the Person entity that we created earlier (see Recipe 13.1).

  3. Select FileNew File in Xcode.

  4. In the New File dialog, make sure you have selected iOS as the main category and Core Data as the subcategory. Then choose the NSManagedObject subclass item from the righthand side of the dialog and press Next, as shown in Figure 13-11.

  5. Now choose where to save your files in your project and press Create, as shown in Figure 13-12.

Now you will see two new files in your project, called Person.h and Person.m. Open the contents of the Person.h file. It will look like the following:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface Person : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * lastName;
@property (nonatomic, retain) NSNumber * age;

@end
Creating a managed object subclass in Xcode

Figure 13-11. Creating a managed object subclass in Xcode

Figure 13-12. Selecting the destination folder for the managed object ...

Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.