我有数组中的对象集合,如下所示。
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]
}]
任何人都可以建议我使用上述结果的谓词吗?
答案 0 :(得分: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);