gpt4 book ai didi

swift - NSCompoundPredicate 和 Cloudkit

转载 作者:搜寻专家 更新时间:2023-10-31 22:50:45 33 4
gpt4 key购买 nike

我正在尝试使用 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)

最佳答案

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

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

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

但我在使用同时包含 AND 和 OR 语句的谓词方面经验不佳。如果这个谓词不起作用,那么我认为唯一的解决方案是执行 2 个单独的查询,然后合并结果。您可以使用 ExSwift 中的联合函数来做到这一点图书馆

关于swift - NSCompoundPredicate 和 Cloudkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29376315/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com