使用nsarray的所有值过滤NSMutableArray

时间:2016-08-20 07:43:29

标签: ios objective-c nsmutablearray

我有像这样的MutableArray ......

    restaurants = ({
    cuisines =   ({
                    "cuisine_id" = 1;
                    "cuisine_name" = Indian;
                },
                {
                    "cuisine_id" = 2;
                    "cuisine_name" = SriLanka;
                }
            );}

            { cuisines = ({
                    "cuisine_id" = 1;
                    "cuisine_name" = Chinese;
                },
                {
                    "cuisine_id" = 2;
                    "cuisine_name" = Afghani;
                }
            );}

           {cuisines =({
                    "cuisine_id" = 1;
                    "cuisine_name" = Chinese;
                },
                {
                    "cuisine_id" = 2;
                    "cuisine_name" = Afghani;
                }
            );}
        )

我正在过滤餐馆数组有多个值的烹饪名称 例如,如果我有,

Nsarray喜欢这个cuisine_name( 印度, 斯里兰卡, 中文, )

我想根据cusine_name的每个对象过滤我的Restaurants数组。

目前我这样做..

        NSPredicate *predicateCuisines = [NSPredicate   predicateWithFormat:@"(cuisines.cuisine_name = %@)",[cuisineTypes      valueForKey:@"cuisine_name"]];
    [self.arrRestaurantList filterUsingPredicate:predicateCuisines];

但它过滤使用说明...对于包含整个三个值(印度,斯里兰卡,中国)的餐厅数组,但我想过滤每个烹饪对象的对象。 任何帮助都会非常感激..

1 个答案:

答案 0 :(得分:2)

使用此

  NSArray* arrtu4 = @[@{@"cuisines" : @[@{@"cuisine_id" : @1,@"cuisine_name" : @"Indian"},@{ @"cuisine_id" : @2, @"cuisine_name" : @"SriLanka"}]},@{ @"cuisines" : @[@{@"cuisine_id" : @1,@"cuisine_name" : @"Chinese"},@{@"cuisine_id" : @2,@"cuisine_name" : @"Afghani"}]},@{@"cuisines" :@[@{@"cuisine_id" :@1,@"cuisine_name" : @"Chinese"},@{ @"cuisine_id" : @2,@"cuisine_name" : @"Afghani"}]}];
    NSMutableArray *arrryfilted = [NSMutableArray array];

    NSArray *arrayCountrys = @[@"Chinese",@"SriLanka",@"Indian"];

for (int i = 0; i<arrtu4.count; i++)
{

    NSArray *filterArray2 = [[arrtu4 objectAtIndex:i]valueForKey:@"cuisines"];

    NSArray *filterArray3 = [filterArray2 filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        return [arrayCountrys containsObject:[evaluatedObject valueForKey:@"cuisine_name"]]  ;

                }]];




     [arrryfilted addObjectsFromArray:filterArray3 ];



}

NSLog(@"%@",arrryfilted);

输出:

2016-08-20 16:44:59.856 coredataReltaion[6762:152672] (
        {
        "cuisine_id" = 1;
        "cuisine_name" = Indian;
    },
        {
        "cuisine_id" = 2;
        "cuisine_name" = SriLanka;
    },
        {
        "cuisine_id" = 1;
        "cuisine_name" = Chinese;
    },
        {
        "cuisine_id" = 1;
        "cuisine_name" = Chinese;
    }
)