根据 Apples Class Reference CKQuery
,运算符CONTAINS
是受支持的运算符之一。但是,这似乎不起作用。我有一个RecordType
被叫myRecord
和一个字段名称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
. 这意味着所有字符串字段都将用于搜索。没有更好的方法吗?