gpt4 book ai didi

nspredicate - 使用 "CONTAINS"运算符时,带有 NSPredicate 的 CKQuery 失败

转载 作者:行者123 更新时间:2023-12-02 03:18:05 25 4
gpt4 key购买 nike

根据 Apples 类引用 CKQuery,运算符 CONTAINS 是受支持的运算符之一。但是,这似乎不起作用。我有一个名为 myRecordRecordType,以及一个字段名称为 name 类型为 String 的记录。我尝试使用两种不同的谓词获取记录,一种使用“==”运算符,另一种使用CONTAINS 运算符。

func getRecords() {
let name = "John"
let Predicate1 = NSPredicate(format: "name == %@",name)
let Predicate2 = NSPredicate(format: "name CONTAINS %@",name)

let sort = NSSortDescriptor(key: "Date", ascending: false)
let query = CKQuery(recordType: "myRecord", predicate: Predicate1)
// let query = CKQuery(recordType: "myRecord", predicate: Predicate2)
query.sortDescriptors = [sort]

let operation = CKQueryOperation(query: query)
operation.desiredKeys = ["name", "Date"]

operation.recordFetchedBlock = { (record) in
print(record["name"])

operation.queryCompletionBlock = { [unowned self] (cursor, error) in
dispatch_async(dispatch_get_main_queue()) {
if error == nil {

print ("sucess")
} else {
print("couldn't fetch record error:\(error?.localizedDescription)")

}
}

}

CKContainer.defaultContainer().publicCloudDatabase.addOperation(operation)
}

使用Predicate1,输出为:

Optional(John)
sucess

使用Predicate2,输出为:

couldn't fetch record error:Optional("Field \'name\' has a value type of STRING and cannot be queried using filter type LIST_CONTAINS")

同时使用 [c] 忽略大小写会导致服务器问题。

如何正确使用运算符CONTAINS

编辑:我现在仔细查看了文档,发现 CONTAINS 只能与 SELF 一起使用。这意味着所有字符串字段都将用于搜索。有没有更好的办法?

最佳答案

这是一个异常,如下所示:

With one exception, the CONTAINS operator can be used only to test list membership. The exception is when you use it to perform full-text searches in conjunction with the self key path. The self key path causes the server to look in searchable string-based fields for the specified token string. For example, a predicate string of @"self contains 'blue'" searches for the word “blue” in all fields marked for inclusion in full-text searches. You cannot use the self key path to search in fields whose type is not a string.

因此,您可以使用“self”而不是“%K”来搜索字符串字段的子文本。

For the full document written by Apple

关于nspredicate - 使用 "CONTAINS"运算符时,带有 NSPredicate 的 CKQuery 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35369827/

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