-1

Here I want to filter dictionary in the array which contains the "cate = subcat" could anybody help me please

 menuArr :(
        {
        Cate = subcat;
        Description = "Grilled Chicken";
        Id = 2;
        Image = "http://asaraa.com/our_development/restaurant/admin/logo_image/large/28322grilled_chicken.jpg";
        Name = "Grilled Chicken";
        Price = 0;
        Qty = "";
        Title = "grilled chicken";
    },
        {
        Cate = product;
        Description = gravey;
        Id = 12;
        Image = "http://asaraa.com/our_development/restaurant/admin/logo_image/large/27166mutton.jpg";
        Name = gravey;
        Price = 50;
        Qty = 1;
        Title = gravey;
    },
        {
        Cate = product;
        Description = "Chicken Korma";
        Id = 15;
        Image = "http://asaraa.com/our_development/restaurant/admin/logo_image/large/77845Indian_chicken_recipes.jpg";
        Name = "Chicken Korma";
        Price = 99;
        Qty = 1;
        Title = "Chicken Korma";
    },
        {
        Cate = subcat;
        Description = "Chicken Sandwiches";
        Id = 16;
        Image = "http://asaraa.com/our_development/restaurant/admin/logo_image/large/67831chicken-sandwich-melt-0204_300.jpg";
        Name = "Chicken Sandwiches";
        Price = 0;
        Qty = "";
        Title = "Chicken Sandwiches";
    }
)
dineshprasanna
  • 1,284
  • 4
  • 20
  • 37

3 Answers3

2
NSString *searchString = @"subcat"
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Cate = %@",searchString];
NSArray *array = [yourArray filteredArrayUsingPredicate:predicate];
if (array.count > 0)
{
// here goes your code
}
Manu
  • 4,730
  • 2
  • 20
  • 45
0

Using Linq to ObjectiveC you can filter it as follows:

NSArray* itemsInCategory = [menuArr where:^BOOL(id dic) {
    return [[dic objectForKey:@"Cate"] isEqualToString:@"subcat"];
}];

This finds all the dictionaries where Cate=subcat.

ColinE
  • 68,894
  • 15
  • 164
  • 232
0

Use NSPredicate

NSArray *filtered = [yourArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(Cate LIKE[CD] %@)", @"subcat"]];
Maulik
  • 19,348
  • 14
  • 82
  • 137