【问题标题】:CKQuery using NSPredicate searching for NIL always throws an Invalid Expression errorCKQuery 使用 NSPredicate 搜索 NIL 总是抛出 Invalid Expression 错误
【发布时间】: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 attributeNSPredicate 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 的限制,而不是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


【解决方案1】:

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

构建您的谓词
NSPredicate 对象定义了用于确定记录是否匹配 询问。 CKQuery 类仅支持谓词的子集 完整的 NSPredicate 类提供的行为。

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

【讨论】:

  • 那么在使用 CKQuery 构建谓词时,还有其他方法可以测试 nil 吗?
猜你喜欢
  • 1970-01-01
  • 2011-09-25
  • 2020-02-04
  • 2020-06-17
  • 2022-07-27
  • 2022-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多