【发布时间】:2016-10-11 05:20:08
【问题描述】:
在 iOS 10.0.1 和 Xcode 8.0 上,我有一个 CKQuery 可以成功返回各种条件的数据(BEGINSWITH 和等于某些字符串等)。现在我正在专门寻找在其中一个字段中具有空值的记录。
根据Apple's Docs,加上How do I set up a NSPredicate to look for objects that have a nil attribute和NSPredicate with boolean is not comparing a boolean to NO,下面的语法应该搜索nil值:
NSPredicate *searchConditions = [NSPredicate predicateWithFormat:@"joiner = nil "];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"availableURLs"
predicate:searchConditions];
当我设置断点并检查searchConditions 时,它的计算结果为joiner == nil。当我尝试使用它时,第二行抛出异常:
Terminating app due to uncaught exception 'CKException', reason:
'Invalid predicate: joiner == nil (Error Domain=CKErrorDomain Code=12
"Invalid right expression in <joiner == nil>: <nil> is not a function expression"
UserInfo={ck_isComparisonError=true,
NSLocalizedDescription=Invalid right expression in <joiner == nil>: <nil> is not a function expression,
NSUnderlyingError=0x1706516d0 {Error Domain=CKErrorDomain Code=12 "<nil> is not a function expression"
UserInfo={NSLocalizedDescription=<nil> is not a function expression, ck_isComparisonError=true}}})'
我已经尝试了=、==、nil、Nil、NIL 和 NULL 的所有组合。全部评估为joiner == nil 并抛出异常。
在第一个链接问题中,OP 使用 predicateWithSubstitutionVariables 替换 [NSNull null]。我也试过了,但它再次评估为joiner == nil 并抛出了同样的异常。
Apple Docs 有一个专门用于搜索 nil 的部分,我看不出他们的示例与我的示例有何不同:
predicate = [NSPredicate predicateWithFormat:@"firstName = nil"];
我在这里缺少什么?
【问题讨论】:
-
我认为问题更多是关于
CKQuery的限制,而不是NSPredicate,这可以解释为什么你可以在NSArray的自定义对象中使用nil...这不是很清楚说,但来自CKQuery的文档:Building Your Predicates An NSPredicate object defines the logical conditions for determining whether a record is a match for a query. The CKQuery class supports only a subset of the predicate behaviors offered by the full NSPredicate class. -
@Larme 谢谢,我只是更仔细地重新阅读了 ckquery 文档。我之前错过了关于仅支持 nspredicate 的子集的内容。如果您将其作为答案发布,我会接受并可能会帮助下一个没有足够近距离阅读文档的人。
标签: ios objective-c nspredicate cloudkit