【问题标题】:How to filter array on the basis of some value contained within that如何根据其中包含的某些值过滤数组
【发布时间】:2023-04-05 18:15:02
【问题描述】:

我有如下数组中的对象集合。

arr_recipes = [{
  recipe.name = r1,
  recipe_tags = [t1, t2, t3]
},
{
 recipe.name = r1,
 recipe_tags = [t1, t2]
},
{
 recipe.name = r1,
 recipe_tags = [t4, t5, t6]
}]

现在,我想通过标签名称搜索 arr_recipes 的内容,即如果我输入 t1,它应该返回以下结果:

 arr_searchresult= [{
  recipe.name = r1,
  recipe_tags = [t1, t2, t3]
},
{
 recipe.name = r1,
 recipe_tags = [t1, t2, t3]
}
}]

如果我在 uisearhcbar 中输入 t3,我应该得到如下结果:

arr_searchresult = [{
  recipe.name = r1,
  recipe_tags = [t1, t2, t3]
}]

谁能建议我在搜索结果之上的 obtian tha 的谓词?

【问题讨论】:

    标签: ios objective-c search nsarray nspredicate


    【解决方案1】:

    使用这个:

    NSArray *arr_recipes = @[@{@"recipe.name" : @"r1",
                             @"recipe_tags" : @[@"t1", @"t2", @"t3"]},
                             @{@"recipe.name" : @"r1",
                             @"recipe_tags" : @[@"t1", @"t2"] } ];
    
    NSString *searchText = @"t3"; // set your search field value.
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recipe_tags contains[cd] %@", searchText];
    
    NSArray *filteredArray = [arr_recipes filteredArrayUsingPredicate:predicate];
    
    NSLog(@"%@", filteredArray);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多