How to use NSPredicate for Key-Path values

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2010-05-13T20:10:56Z Indexed on 2010/05/13 20:14 UTC
Read the original article Hit count: 521

Filed under:
|
|
|

Using an NSPredicate for an array is rather straight forward using filteredArrayUsingPredicate:.

How is this done for key-path values? Meaning, I have an array of objects (in this case, the objects are of the same type). The objects each have an instance variable called name. As per the documentation, it says to do the following:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
   @"ANY employees.firstName like 'Matthew'"];

Is that -also- used in filteredArrayUsingPredicate? What if I have an array of People objects? does that mean I would use:

NSArray *allPeopleObjects; // pre-populated
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY people.name like 'Foo'"];
NSArray *matching = [allPeopleObjects filteredArrayUsingPredicate:predicate];

Documentation is a bit lacking in that department.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa