0

Hi I have a Array like this:

@[
    @{@"Name":@"marko"; @"ids"=@[@"121",@"156",@"175",@"154"];},
    @{@"Name":@"shine";@"ids"=@[@"175",@"165",@"154",@"187"];},
    @{@"Name":@"David";@"ids"=@[@"857",@"297",@"156",@"254"];},
    @{@"Name":@"Harry"; @ids"=@[@"297",@"154",@"867",@"201"];},
]; 

How to get the dictionaries having the string @"156" in the ids array using an NSPredicate.

Pieter Kuijpers
  • 7,247
  • 5
  • 28
  • 36
Ajay
  • 185
  • 3
  • 18
  • Have a look at this similar question: http://stackoverflow.com/questions/958622/using-nspredicate-to-filter-an-nsarray-based-on-nsdictionary-keys – Pieter Kuijpers Sep 25 '15 at 12:53
  • I Worked like this but i didn't get the result what i want . if "array" is the mainArray. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ids == %@",@"156"]; NSArray *filtered = [array filteredArrayUsingPredicate:predicate]; But it shows 0 count .Please Give Suggestions. – Ajay Sep 25 '15 at 14:30

1 Answers1

0

Try this out:

NSDictionary *dict1 = @{@"Name": @"marko", @"ids" : @[@"121",@"156",@"175",@"154"]};
NSDictionary *dict2 = @{@"Name":@"shine", @"ids" : @[@"175",@"165",@"154",@"187"]};
NSDictionary *dict3 = @{@"Name":@"David", @"ids" : @[@"857",@"297",@"156",@"254"]};
NSDictionary *dict4 = @{@"Name":@"Harry", @"ids" : @[@"297",@"154",@"867",@"201"]};

NSArray *test = @[dict1, dict2, dict3, dict4];

NSArray *filtered = [test filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ids CONTAINS[cd] %@", @"156"]];
Abhinav
  • 37,684
  • 43
  • 191
  • 309