NSPredicate string matching against NSSet's
I'm trying to use an NSPredicate to search for string matches against an array of CoreData objects generated like so:
// Grab an arr开发者_如何学JAVAay of all Company projects
NSArray *array = [[company projects] allObjects];
I use the following predicate to match any project names, company names or client names with a case-insensitive string (note: this should allow for partial matches, so that 'App' will match 'Apple Inc.', etc):
(name LIKE[cd] %@) OR (ANY companies.name LIKE[cd] %@) OR (ANY companies.clients.name LIKE[cd] %@)
The CoreData relationships mentioned in the predicate look like so:
SELF -> (NSString *) name
SELF -> (NSSet *) companies -> (NSString *) name
SELF -> (NSSet *) companies -> (NSSet *) -> clients -> (NSString *) name
Whenever I attempt to filter by the above predicate, I find the following in my Console:
HIToolbox: ignoring exception 'Can't do regex matching on object {(
"Apple Inc.",
"Test Co.",
Microsoft
)}.'
If I'm understanding things correctly, it looks as though trying to match against the keypath "companies.clients.name" returns an NSSet (or other object) where an NSString (name) was expected.
What am I doing wrong?
companies.clients.name
will return collection of NSSet
s, where each element in the colletion is the contents of one companie's client's names (i.e. an NSSet
). You probably want to use companies.@unionOfSets.clients.names
in your predicate string.
See the guide on using Set and Array Operators in Key-Value Coding.
精彩评论