Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate for Array of Dictionaries

I have an

Array
  Item 0 -- Dictionary
    Sport -- String
    Mens  -- Array
      Item 0 -- Dictionary
        Name -- String
        Rules -- String
        Description -- String
    Womens  -- Array
      Item 0 -- Dictionary
        Name -- String
        Rules -- String
        Description -- String
   Item 1 -- Dictionary
      And so on.....

I would like to create a NSPredicate searching if a given string is contained inside of Name. How can I achieve reaching that deep?

If you cant understand the graph. I have an array full of dictionaries, Inside the dictionaries are A string, An Array of dictionaries, and An Array of dictionaries, Inside of the dictionaries are string objects

So How do I get into the second dictionaries and search the Name key

Thanks in advance

like image 713
anotherDeveloper Avatar asked Dec 06 '22 03:12

anotherDeveloper


1 Answers

You need the following

NSString *str = <search string>;
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY Mens.Name LIKE %@ OR ANY Womens.Name LIKE %@", str, str];
NSArray *result = [your_array filteredArrayUsingPredicate:pred];
BOOL success = result.count > 0;
like image 97
malex Avatar answered Jan 26 '23 00:01

malex