CKQuery 使用 NSPredicate 搜索 NIL 总是抛出 Invalid Expression 错误

CKQuery using NSPredicate searching for NIL always throws an Invalid Expression error

在 iOS 10.0.1 和 Xcode 8.0 上,我有一个 CKQuery 在各种条件下成功返回数据(BEGINSWITH 并且等于某些字符串等)。现在我正在专门查找其中一个字段中具有空值的记录。

根据 Apple's Docs, plus How do I set up a NSPredicate to look for objects that have a nil attribute and 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}}})'

我已经尝试了 ===nilNilNILNULL 的所有组合。全部评估为 joiner == nil 并抛出异常。

在第一个链接问题中,OP 使用 predicateWithSubstitutionVariables 替换 [NSNull null]。我也尝试过,但它再次评估为 joiner == nil 并抛出相同的异常。

Apple Docs 有一节专门介绍如何搜索 nil,我看不出他们的示例与我的示例有何不同:

predicate = [NSPredicate predicateWithFormat:@"firstName = nil"];

我在这里错过了什么?

根据 CKQuery doc,似乎 CKQuery 不允许在谓词中使用 nil(在撰写答案时在 iOS 10 中)。

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.

您的 NSPredicate 构造似乎没有错,因为它可以在 NSArray 的自定义对象上工作。