#import "AppDelegate.h"#import "Person.h"@implementation AppDelegate@synthesize managedObjectContext = _managedObjectContext;@synthesize managedObjectModel = _managedObjectModel;@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [self createNewPersonWithFirstName:@"Anthony" lastName:@"Robbins" age:51]; [self createNewPersonWithFirstName:@"Richard" lastName:@"Branson" age:61]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Person"]; NSSortDescriptor *ageSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES]; NSSortDescriptor *firstNameSort = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]; fetchRequest.sortDescriptors = @[ageSort, firstNameSort]; NSError *requstError = nil; NSArray *persons = [self.managedObjectContext executeFetchRequest:fetchRequest error:&requstError]; for (Person *person in persons) { NSLog(@"First Name = %@", person.firstName); NSLog(@"Last Name= %@", person.lastName); NSLog(@"Age = %lu", (unsigned long)[person.age unsignedCharValue]); } self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}-(BOOL) deletePerson:(NSArray*)praramPersons{ BOOL isDelete = NO; if ([praramPersons count] > 0) { Person *lastPerson = [praramPersons lastObject]; [self.managedObjectContext deleteObject:lastPerson]; NSError *savingError = nil; if ([self.managedObjectContext save:&savingError]) { NSLog(@"Successfully deleted the last Person in the array"); } else { NSLog(@"Failed to delete the last Person in the array"); } } else { NSLog(@"Could not find any Person entities in the context"); } return isDelete;}-(BOOL) createPersonSuccess:(NSArray*)paramPersons{ BOOL createResult = NO; if ([paramPersons count] > 0) { createResult = YES; NSUInteger counter = 1; for (Person *thisPerson in paramPersons) { NSLog(@"Person %lu First Name = %@", (unsigned long)counter, thisPerson.firstName); NSLog(@"Person %lu lastName = %@", (unsigned long)counter, thisPerson.lastName); NSLog(@"Person %lu Age = %ld", (unsigned long)counter, (unsigned long)[thisPerson.age unsignedIntegerValue]); counter ++; } } else { NSLog(@"Could not find any Person entities in the context"); } return createResult;}-(void)createNewPerson{ Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext]; if (newPerson != nil) { newPerson.firstName = @"Anthony"; newPerson.lastName = @"Robbins"; newPerson.age = @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."); } }-(BOOL) createNewPersonWithFirstName:(NSString*)paramFirstName lastName:(NSString*)paramLastName age:(NSUInteger)paramAge{ BOOL result = NO; if ([paramFirstName length] == 0 | [paramLastName length] == 0) { NSLog(@"First and Last names are mandatory"); return NO; } Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext]; if (newPerson == nil) { NSLog(@"Failed to create the new person."); return NO; } newPerson.firstName = paramFirstName; newPerson.lastName = paramLastName; newPerson.age = @(paramAge); NSError *savingError = nil; if ([self.managedObjectContext save:&savingError]) { return YES; } else { NSLog(@"Failed to save the new person.Error = %@", savingError); } return result;}
Nslog
2014-09-13 00:20:18.452 CoreDataDemo[683:60b] First Name = Anthony2014-09-13 00:20:18.453 CoreDataDemo[683:60b] Last Name= Robbins2014-09-13 00:20:18.454 CoreDataDemo[683:60b] Age = 512014-09-13 00:20:18.454 CoreDataDemo[683:60b] First Name = Anthony2014-09-13 00:20:18.454 CoreDataDemo[683:60b] Last Name= Robbins2014-09-13 00:20:18.455 CoreDataDemo[683:60b] Age = 512014-09-13 00:20:18.455 CoreDataDemo[683:60b] First Name = Anthony2014-09-13 00:20:18.455 CoreDataDemo[683:60b] Last Name= Robbins2014-09-13 00:20:18.456 CoreDataDemo[683:60b] Age = 512014-09-13 00:20:18.456 CoreDataDemo[683:60b] First Name = Anthony2014-09-13 00:20:18.456 CoreDataDemo[683:60b] Last Name= Robbins2014-09-13 00:20:18.457 CoreDataDemo[683:60b] Age = 512014-09-13 00:20:18.457 CoreDataDemo[683:60b] First Name = Anthony2014-09-13 00:20:18.457 CoreDataDemo[683:60b] Last Name= Robbins2014-09-13 00:20:18.458 CoreDataDemo[683:60b] Age = 512014-09-13 00:20:18.458 CoreDataDemo[683:60b] First Name = Anthony2014-09-13 00:20:18.458 CoreDataDemo[683:60b] Last Name= Robbins2014-09-13 00:20:18.459 CoreDataDemo[683:60b] Age = 512014-09-13 00:20:18.459 CoreDataDemo[683:60b] First Name = Anthony2014-09-13 00:20:18.485 CoreDataDemo[683:60b] Last Name= Robbins2014-09-13 00:20:18.486 CoreDataDemo[683:60b] Age = 512014-09-13 00:20:18.487 CoreDataDemo[683:60b] First Name = Richard2014-09-13 00:20:18.487 CoreDataDemo[683:60b] Last Name= Branson2014-09-13 00:20:18.487 CoreDataDemo[683:60b] Age = 612014-09-13 00:20:18.488 CoreDataDemo[683:60b] First Name = Richard2014-09-13 00:20:18.488 CoreDataDemo[683:60b] Last Name= Branson2014-09-13 00:20:18.489 CoreDataDemo[683:60b] Age = 612014-09-13 00:20:18.489 CoreDataDemo[683:60b] First Name = Richard2014-09-13 00:20:18.489 CoreDataDemo[683:60b] Last Name= Branson2014-09-13 00:20:18.490 CoreDataDemo[683:60b] Age = 612014-09-13 00:20:18.490 CoreDataDemo[683:60b] First Name = Richard2014-09-13 00:20:18.491 CoreDataDemo[683:60b] Last Name= Branson2014-09-13 00:20:18.491 CoreDataDemo[683:60b] Age = 612014-09-13 00:20:18.495 CoreDataDemo[683:60b] Application windows are expected to have a root view controller at the end of application launch
Objective-C coredata