使用 NSPredicate 过滤数据数组,其中过滤文本是字符串数组

Filtering array of data where the filter text is array of strings using NSPredicate

我的数组是模型对象的集合,其字段是name、address、arrTags。 这里的 arrTags 是一个 NSArray.

我必须根据该 arrTags 值过滤我的数组。

例如。我在 arrTags 中有像伦敦、巴黎、澳大利亚这样的值。

我想显示数组索引,如果我键入字母 L,则在 myArray 中包含 London。

到目前为止,我已经完成了以下工作:

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"arrTags contains[c] %@", searchText];
  arr_searchResults =[[NSMutableArray alloc] initWithArray:[myArray filteredArrayUsingPredicate:predicate]];
  [image_view reloadData];

仅当我在搜索栏中键入 London 时才会显示数组。 但是如果我开始输入 L ,我想显示数组。

提前致谢。

如果你想从数组 (arrTaggs) 内部搜索,你应该使用

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY arrTags CONTAINS[c] %@", searchText];