3

我正在尝试使用 Cloudkit 使用复合谓词来执行此操作,但 Xcode 错误显示“意外表达式”。有人知道我的代码有什么问题吗?感谢任何帮助!

let userRef = CKReference(recordID: userID, action: .None)

    let predicateOne = NSPredicate(format: "recordID IN %@ AND user = %@", postIDs, userRef)
// I need all user post's that match the IDs in the array and where the user ID matches the Id in the reference field for the user.

    let predicateTwo = NSPredicate(format: "recordID IN %@", followIDs)
// Or I want recordID's that match the IDs in this array.
// I want records if either predicate condition is met, or both, which is why I'm using an OrPredicateType.

    let predicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [predicateOne!, predicateTwo!])
    let query = CKQuery(recordType: "UserPosts", predicate: predicate)
    let queryOp = CKQueryOperation(query: query)
4

1 回答 1

2

对于 CloudKit 使用 NSPredicate 有一些规则。有关详细信息,请参阅Apple 文档

您可以尝试像这样创建谓词:

NSPredicate(format: "recordID IN %@ OR (recordID IN %@ AND user = %@)", followIDs, postIDs, userRef)

但是我在使用带有 AND 和 OR 语句的谓词方面有不好的经验。如果这个谓词不起作用,那么我认为唯一的解决方案是执行 2 个单独的查询,然后组合结果。你可以通过使用ExSwift库中的 union 函数来做到这一点

于 2015-03-31T19:42:14.760 回答