If all of the dictionaries have the same set of keys, then you could do something pretty simple:
NSArray *keys = ...; //the list of keys that all of the dictionaries contain
NSMutableArray *subpredicates = [NSMutableArray array];
for (NSString *key in keys) {
NSPredicate *subpredicate = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", key, searchText];
[subpredicates addObject:subpredicate];
}
NSPredicate *filter = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
Then you can use filter
to filter your NSArray
(using -filteredArrayUsingPredicate
).
If, on the other hand, you have an array of arbitrary dictionaries that all have different keys, you'd need to something a bit more perverse:
NSPredicate *filter = [NSPredicate predicateWithFormat:@"SUBQUERY(FUNCTION(SELF, 'allKeys'), $k, SELF[$k] contains[cd] %@).@count > 0", searchText];
A bit about what this is doing:
FUNCTION(SELF, 'allKeys')
- this will execute -allKeys
on SELF
(an NSDictionary
) and return an NSArray
of all the keys in the dictionary
SUBQUERY(allKeys, $k, SELF[$k] contains[cd] %@)
- This will iterate over every item in allKeys
, with each successive item being placed into the $k
variable. For each item, it will execute SELF[$k] contains %@
. This will basically end up doing: [theDictionary objectForKey:$k] contains[cd] %@
. If this returns YES
, then the $k
item will be aggregated into a new array.
SUBQUERY(...).@count > 0
- after finding all of the keys that correspond to values that contain your search text, we check and see if there were any. If there were (ie, the size of the array is larger than 0), then the overall dictionary will be part of the final, filtered array.
I recommend going with the first approach, if at all possible. SUBQUERY
and FUNCTION
are a bit arcane, and the first is much easier to understand.
And here's another way, which you actually almost had in your question. Instead of doing ANY SELF.allValues contains[cd] %@
, you can do ANY FUNCTION(SELF, 'allValues') contains[cd] %@
. This is equivalent to my SUBQUERY
madness, but much simpler. Kudos to you for thinking of using ANY
(I usually forget that it exists).
EDIT
The reason SELF.allValues
doesn't work, is that this is interpreted as a keypath, and -[NSDictionary valueForKey:]
is supposed to be the same as -[NSDictionary objectForKey:]
. The catch here is that if you prefix the key with @
, then it forwards on to [super valueForKey:]
, which will do what you're expecting. So you could really do:
ANY SELF.@allValues contains[cd] %@
Or simply:
ANY @allValues contains[cd] %@
And this will work (and is the best and simplest approach).