过滤数组中的字典

时间:2013-04-06 06:51:29

标签: iphone ios objective-c nsmutablearray nsmutabledictionary

这里我想过滤包含“cate = subcat”的数组中的字典 请有人帮帮我

 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";
    }
)

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

使用Linq to ObjectiveC您可以按如下方式对其进行过滤:

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

这会找到Cate = subcat的所有词典。

答案 2 :(得分:0)

使用NSPredicate

NSArray *filtered = [yourArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(Cate LIKE[CD] %@)", @"subcat"]];