Filtering an array of arrays that contain a dictionary using NSPredicate

I have an array titled charactersAndDescriptions that contains several arrays, and in each of those arrays is an NSDictionary with two keys/values. It looks like this:

I need to filter this array on the value for the key 'desc,' comparing it against a string variable searchText (and it should return results where that string exists anywhere in the desc). I want the returned structure to be the same as the original - arrays containing a dictionary inside an array. Here is my code:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"desc contains[c] %@", searchText];
self.resultsArray = [self.charactersAndDescriptions filteredArrayUsingPredicate:resultPredicate];

The problem is, self.resultsArray always returns 0 objects. I don't see why that's the case. I was looking at questions on here where they had only one array of dictionaries but couldn't find any for nested arrays. Hoping you can help, thanks!


I am not sure if you can do it with a single predicate. But you can iterate through individual array of dictionaries, filter them and add the filtered list to your final array. Try something like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"desc contains[c] %@", searchText];
NSMutableArray *finalColletion = [NSMutableArray array];
for (NSArray *temp in self.charactersAndDescriptions) {
    NSArray *result =[temp filteredArrayUsingPredicate:predicate];
    if([result count] > 0) {
        [finalColletion addObject:result];

Need Your Help

Beginning Erlang: Odd Result from rem

list erlang operators erlang-shell

I am trying to learn Erlang and had gotten no further than operators when I ran into a problem:

How to produce beep sound using "\a" escape character?


If I write the following program, then there is no beep sound on running the code.