#import "Creating_and_Saving_Data_Using_Core_DataAppDelegate.h" #import "Person.h"
@implementation Creating_and_Saving_Data_Using_Core_DataAppDelegate
@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
...
3. In the application:didFinishLaunchingWithOptions: method of your shared ap-
plication delegate, write this code:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];
Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"
inManagedObjectContext:self.managedObjectContext]; if (newPerson != nil){
newPerson.firstName = @"Anthony";
newPerson.lastName = @"Robbins";
newPerson.age = [NSNumber numberWithUnsignedInteger:51];
NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError]){
NSLog(@"Successfully saved the context."); } else {
NSLog(@"Failed to save the context. Error = %@", savingError); }
} else {
NSLog(@"Failed to create the new person."); }
self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];
return YES; }
评论