How to use NSPredicate for Key-Path values
- by randombits
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.