October 2013
Intermediate to advanced
1053 pages
28h 7m
English
You want to be able to read the contents of your entities (tables) using Core Data.
Use an instance of NSFetchRequest:
-(BOOL)createNewPersonWithFirstName:(NSString*)paramFirstNamelastName:(NSString*)paramLastNameage:(NSUInteger)paramAge{BOOLresult=NO;if([paramFirstNamelength]==0||[paramLastNamelength]==0){NSLog(@"First and Last names are mandatory.");returnNO;}Person*newPerson=[NSEntityDescriptioninsertNewObjectForEntityForName:@"Person"inManagedObjectContext:self.managedObjectContext];if(newPerson==nil){NSLog(@"Failed to create the new person.");returnNO;}newPerson.firstName=paramFirstName;newPerson.lastName=paramLastName;newPerson.age=@(paramAge);NSError*savingError=nil;if([self.managedObjectContextsave:&savingError]){returnYES;}else{NSLog(@"Failed to save the new person. Error = %@",savingError);}returnresult;}-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{[selfcreateNewPersonWithFirstName:@"Anthony"lastName:@"Robbins"age:51];[selfcreateNewPersonWithFirstName:@"Richard"lastName:@"Branson"age:61];/* Tell the request that we want to read thecontents of the Person entity *//* Create the fetch request first */NSFetchRequest*fetchRequest=[[NSFetchRequestalloc]initWithEntityName:@"Person"];NSError*requestError=nil;/* And execute the fetch request on the context */NSArray*persons=
Read now
Unlock full access