ios NSPredicate 在 NSArray 上搜索任何对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18822777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:13:38  来源:igfitidea点击:

NSPredicate on an NSArray to search for any object

iosobjective-carraysnspredicate

提问by David DelMonte

I have an array of objects with names, addresses, tel. nos, etc.

我有一组带有名称、地址、电话的对象。否等。

I want to be able to search the array for any occurrence of a term - whether in the name field, the address field, etc.

我希望能够在数组中搜索任何出现的术语 - 无论是在名称字段、地址字段等中。

I have something like this in mind :

我有这样的想法:

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    // Update the filtered array based on the search text and scope.
    // Remove all objects from the filtered search array


    [self.searchResults removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchText];
    searchResults = [NSMutableArray arrayWithArray:[contactArray filteredArrayUsingPredicate:predicate]];
}

This causes an exception "Can't use in/contains operator with collection".

这会导致异常“不能将 in/contains 运算符与集合一起使用”。

UPDATE. I can now search on up to three fields. When I add a fourth (in any sequence), I get this exception: "Unable to parse the format string ..."

更新。我现在最多可以搜索三个字段。当我添加第四个(以任何顺序)时,我收到此异常:“无法解析格式字符串......”

The Predicate code is now:

谓词代码现在是:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.narrative contains[c] %@ OR SELF.category contains[c] %@ OR SELF.date contains[c] OR SELF.name contains[c] %@", searchText, searchText, searchText, searchText];

     searchResults = [NSMutableArray arrayWithArray:[allDreams filteredArrayUsingPredicate:predicate]];

Is three a limit of predicate search fields? How do I get around this? Thanks again.

三个是谓词搜索字段的限制吗?我该如何解决这个问题?再次感谢。

回答by Monolo

Just use a predicate string that checks for them all:

只需使用一个谓词字符串来检查它们:

@"name contains[cd] %@ OR address contains[cd] %@"

you can add as many as you want.

您可以根据需要添加任意数量。

The only downside is that you'll need to add the same search string for each field you want to test, which can seem a bit ugly.

唯一的缺点是您需要为要测试的每个字段添加相同的搜索字符串,这看起来有点难看。

If your objects are dictionaries, then there is a way to truly search all values without necessarily knowing their names at compile time, using a subquery.

如果您的对象是字典,那么有一种方法可以使用子查询真正搜索所有值,而不必在编译时知道它们的名称。

It works like this:

它是这样工作的:

@"subquery(self.@allValues, $av, $av contains %@).@count > 0"

It uses the @allValuesspecial key (or method call if you prefer) for dictionary objects and uses that to filter any value that contains your search string. If any is found (i.e., the count is positive), the object is included in the results.

它使用@allValues字典对象的特殊键(或方法调用,如果您愿意),并使用它来过滤包含搜索字符串的任何值。如果找到(即计数为正),则该对象将包含在结果中。

Notice that this will examine all values indiscriminately, even those that you don't want to include if you have any in your dictionary.

请注意,这将不加选择地检查所有值,即使是那些您不想包含在字典中的值。

回答by Kjuly

I think your array item is not pure string, right?

我认为您的数组项不是纯字符串,对吗?

Suppose your array item contains nameattribute (even it's a dictionary), you can search it in this way:

假设您的数组项包含name属性(即使它是字典),您可以通过以下方式搜索它:

NSPredicate * predicate =
  [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@ OR name LIKE[cd] %@", searchText, searchText];

here, namecan be SELF.name.

在这里,name可以SELF.name



HERE's an official document.

是一份官方文件。