anotherDeveloper
anotherDeveloper

Reputation: 77

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

Upvotes: 4

Views: 15278

Answers (2)

malex
malex

Reputation: 10096

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;

Upvotes: 22

Rohan Bhale
Rohan Bhale

Reputation: 1345

To get to the name you have to do this:

for (NSDictionary *itemDict in myArray)
{
    NSArray *womensArray = (NSArray*)[itemDict objectForKey:@"Womens"];

    for (NSDictionary *womenItemDict in womensArray)
    {
          NSString *name = [womenItemDict objectForKey:@"Name"];

          //Do the comparison here
    }
}

Upvotes: -7

Related Questions