Search code examples
iosobjective-cnspredicate

Search NSArray of NSDictionary. How to find using NSPredicate?


I have an NSArray. It has one or more NSDictionary in each index. Based on the search input, I want to check whether it contain the value in contact_Label inside contact_detail dictionary. It will look like this:

(
  {
     "contact_detail" =     {
          "contact_is_in_phone" = 1;
          "contact_Label" = "Tyler Globussoft";
          "contact_displayname" = "Suzan Arohh";
     },
     "last_msg_details" =     {
           .....
     };
  },
  {
  }
);

I have tired like this. But not getting the result.

NSArray *contacts = self.dataArray; //your array of NSDictionary objects
NSPredicate *filter = [NSPredicate predicateWithFormat:@"contact_Label = %@",stringValue];
NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:filter];

Solution

  • You can use

    NSArray *contacts = self.dataArray; //your array of NSDictionary objects
    NSPredicate *filter = [NSPredicate predicateWithFormat:@"contact_detail.contact_Label = %@",stringValue];
    NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:filter];
    

    Happy coding...