NSPredicate for Array of Dictionaries

15,233

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;
Share:
15,233
anotherDeveloper
Author by

anotherDeveloper

Updated on June 07, 2022

Comments

  • anotherDeveloper
    anotherDeveloper almost 2 years

    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

  • Martin R
    Martin R over 10 years
    I do not understand why "LIKE" is so often suggested in a predicate. "LIKE" does a simple wildcard search, so "name LIKE 'A*B'" finds all names starting with an A and ending with a B. In most cases, you want "==" or perhaps "BEGINSWITH" or "CONTAINS". - (+1) though.
  • malex
    malex over 10 years
    You should use exactly what is more appropriate in your own search task. Here LIKE is only for illustration.
  • malex
    malex over 10 years
    The question is not in the choice between like, contains or something else. Beginners in using predicates usually don't realize how to make deep search with different key words as ANY.
  • StuartM
    StuartM over 9 years
    ...for pointing out the 'ANY'. Without that and using the LIKE would cause an error with this data layout. Thanks
  • Patel Maulik
    Patel Maulik over 8 years
    { error: 0, error_msg: "No error", feed: [ { company_id: 127, company_news: [ { company_name: "test company", short_description: "This is just Test", }, { company_name: "test company", short_description: "This is just Test", } ] } how search this type array and dict