从给定键值对的字典数组中过滤NSDictionary

时间:2018-05-10 07:33:56

标签: objective-c uitableview nsarray nsdictionary nspredicate

dataProvider_array = @[
                  @{
                      @"shop_id": @"4",
                      @"shop_name": @"Test shop 1”,
                      @"orders": @[
                                 @{
                                     @"order_id": @"288",
                                     @"user_name": @"User 1",
                                     @"order_type": @“special”,
                                 },
                                 @{
                                     @"order_id": @"289",
                                     @"user_name": @"User 1",
                                     @"order_type": @"special",
                                 },
                                 @{
                                     @"order_id": @"285",
                                     @"user_name": @"User 1”,
                                     @"order_type": @"normal",
                                 },
                                 @{
                                     @"order_id": @"286”,
                                     @"user_name": @"User 1”,
                                     @"order_type": @"normal",
                                 },
                                 @{
                                     @"order_id": @"287”,
                                     @"user_name": @"User 1”,
                                     @"order_type": @"normal",
                                 },
                                 @{
                                    @"order_id": @"284”,
                                     @"user_name": @"User 1”,
                                     @"order_type": @"normal",
                                 }
                                 ]
                  },
                  @{
                      @"shop_id": @"1",
                      @"shop_name": @"Test Shop 2”,
                      @"orders": @[
                                @{
                                    @"order_id": @"288",
                                   @"user_name": @"User 1",
                                 @"order_type": @"special",
                             },
                                 @{
                                     @"order_id": @"289",
                                     @"user_name": @"User 1",
                                     @"order_type": @"special",
                                 },
                                 @{
                                     @"order_id": @"290”,
                                     @"user_name": @"User 1”,
                                     @"order_type": @"normal",
                                 }
                                 ]
                  }
                  ];

此数据显示在分组表视图中,并带有选择订单的选项。 我们有2种类型的正常和特殊的商店。因此,当用户选择任何特殊类型的订单时,所有具有相同订单ID的订单必须由用户选择。

例如,假设用户为第二个商店选择了order_id 289(id = 1),那么用户也必须为其他商店选择相同的订单ID 289。我怎么能保持这个东西?

1 个答案:

答案 0 :(得分:1)

如果您想过滤,那么您可以使用此解决方案,但我不确定这是最好的解决方案: -

NSString *orderID=@"289";//Replace this with your order id
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSArray *categories = [evaluatedObject objectForKey:@"order"];
NSPredicate *subPredicate=[NSPredicate predicateWithFormat:@"(order_id == %@)",orderID];
   return [categories filteredArrayUsingPredicate:subPredicate].count;
}];
NSArray *filteredArray=[dataProvider_array filteredArrayUsingPredicate:predicate];
  

为了测试这个,我创建了自己的数据,如: -

    NSArray *arr=@[@{@"order":@[@{@"order_id":@"1",
                                  @"order_name":@"a"
                                  },
                                @{@"order_id":@"2",
                                  @"order_name":@"b"
                                  }]},
                   @{@"order":@[@{@"order_id":@"3",
                                  @"order_name":@"a"
                                  },
                                @{@"order_id":@"4",
                                  @"order_name":@"b"
                                  }]},
                   @{@"order":@[@{@"order_id":@"5",
                                  @"order_name":@"a"
                                  },
                                @{@"order_id":@"6",
                                  @"order_name":@"b"
                                  }]}
                   ];
    NSString *orderID=@"1";
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        NSArray *categories = [evaluatedObject objectForKey:@"order"];
        NSPredicate *subPredicate=[NSPredicate predicateWithFormat:@"(order_id == %@)",orderID];
        return [categories filteredArrayUsingPredicate:subPredicate].count;
    }];
    NSArray *filteredArray=[arr filteredArrayUsingPredicate:predicate];

或者@Willeke建议

filteredArray = [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ANY order.order_id == %@",orderID]]