10

For example, I have a Managed Object Model with an Entity called "Friends", and a friend has a firstName. I want to get all friends where the firstName is equal to "George". How can I do that?

Felix Lamouroux
  • 7,414
  • 29
  • 46
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260

1 Answers1

21

Use this:

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Friends" inManagedObjectContext:context]; 

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 

[request setEntity:entityDescription]; 

[request setPredicate:[NSPredicate predicateWithFormat:@"firstName == 'George'"]]; 
NSError *error = nil; 
NSArray *array = [context executeFetchRequest:request error:&error];
diederikh
  • 25,221
  • 5
  • 36
  • 49