NSArray filterArrayUsingPredicate 从不返回/阻塞执行

NSArray filterArrayUsingPredicate never returning/ blocking execution

我正在尝试根据这些对象的 name 属性 过滤一组自定义对象。

为了确保一切正常,我只是使用了它,它应该与数组中的 1 个元素完全匹配:

NSLog(@"Length before %lu", (unsigned long) [self.mutableAvailableSources count]);

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name == CNN)"];
[self.mutableAvailableSources filterUsingPredicate:predicate];

NSLog(@"Length after %lu", (unsigned long) [self.mutableAvailableSources count]);

这应该记录

Length before 9
Length after 1

但是,它只记录

Length before 9

然后它停止执行。过滤数组后的任何内容都不会被执行。我尝试在第二个 NSLog(..); 语句上放置一个断点,但它永远不会到达。但是,如果我在过滤数组的行上放置一个断点,它将停止并且我可以确认数组和谓词都存在。

这是怎么回事?

顺便说一句,这是在异步块中发生的,因此应用程序永远不会冻结。但是块中的其余代码不会执行。

更改为:

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == 'CNN'"];

在谓词中,您必须使用查询语言,在本例中是字符串文字:在查询语言中有两个选项:'stringLitareal' 或 "stringLiteral" 但是第二个不起作用,因为使用了 "在 objective-C 中,您需要对 " 进行转义,就是这样:name == \"CNN\"。你可以测试一下,是一样的:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == \"CNN\""];

更多信息:https://developers.google.com/chart/interactive/docs/querylanguage#literals

括号不是问题(查询语言)。这也有效:@"(name == 'CNN')"