NSPredicate 与 OR 返回错误

Posted

技术标签:

【中文标题】NSPredicate 与 OR 返回错误【英文标题】:NSPredicate with OR returning error 【发布时间】:2015-06-19 22:47:02 【问题描述】:

我对以下谓词有疑问:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(creatorUserRecordID == %@) OR (toUser == %@)", userId, userId];

当我在查询 CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Message" predicate:predicate]; 中使用它时,我有一个错误提示:'CKException',原因:'Unexpected expression'。

当我像这样单独使用这两个时:

   NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"(creatorUserRecordID == %@)", userId];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"(toUser == %@)", userId];

然后使用这些谓词之一执行查询它工作正常。我也尝试过使用 NSCompoundPredicate 但结果总是一样的......有什么想法吗?

【问题讨论】:

【参考方案1】:

CKQuery 的文档列出了所有有效的谓词语法。奇怪的是,在“基本复合谓词”下,它列出了NOTAND&&OR|| 未列出,显然不支持 CloudKit 查询谓词。

【讨论】:

我在文档中找到的是:基本复合谓词 AND、&& 逻辑 AND。或者,||逻辑或。不是, !逻辑非。我有什么问题吗? 好的,知道了。 CKQuery 不允许带有 OR 的谓词,尽管 NSPredicate 允许。我的错。你能提示我如何使用 OR 查询 CloudKit 吗?

以上是关于NSPredicate 与 OR 返回错误的主要内容,如果未能解决你的问题,请参考以下文章