0

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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Balasubramanian
  • 5,274
  • 6
  • 33
  • 62
  • 4
    `@"contact_detail.contact_Label = %@"`? – Larme Sep 22 '16 at 15:22
  • As @Larme mentioned, try Cocoa's [Key Value Coding (KVC)](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html) feature. – Sean Sep 22 '16 at 15:24
  • Possible duplicate of [Using NSPredicate to filter an NSArray based on NSDictionary keys](http://stackoverflow.com/questions/958622/using-nspredicate-to-filter-an-nsarray-based-on-nsdictionary-keys) – Teepeemm Sep 23 '16 at 19:28

1 Answers1

3

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...

Sailendra
  • 1,318
  • 14
  • 29