Say I have a Core Data entity called Person. How would I get an NSArray of Persons whose properties match certain values? For instance someone of a particular age, height, or weight… or someone with a whose height,weight and age are specific values…
Can I use an NSPredicate like so:
NSPredicate *pred =
[NSPredicate predicateWithFormat:
@"(age == 25) OR (height_in_cms == 185) OR (age == 30 AND height_in_cms == 170 AND weight_in_kgs == 80)";
// All properties are NSNumber
I’m not an expert on the syntax for
predicateWithFormat:, but you have the basic gist. You can find details on the format in Apple’s Predicate Programming Guide. If you’re asking what to do with the predicate once you have it, here is a snippet that shows you the steps:If you want the results to be sorted, you can pass an array of sort descriptors to the fetch request using
setSortDescriptors:prior to executing the fetch.