使用“CONTAINS”运算符

时间:2016-02-12 18:36:03

标签: nspredicate cloudkit ckquery

根据Apples类参考CKQuery,运算符CONTAINS是受支持的运算符之一。但是,这似乎不起作用。我有一个名为RecordType的{​​{1}},以及一个字段名为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一起使用。这意味着所有String字段都将用于搜索。是不是有更好的方法?

1 个答案:

答案 0 :(得分:1)

这是一个例外,如下所述:

  

除了一个例外,CONTAINS运算符只能用于测试   列出会员资格例外情况是您使用它来执行全文   与自键路径一起搜索。自我关键路径   使服务器查找可搜索的基于字符串的字段   指定的标记字符串例如,@“self”的谓词字符串   包含'blue'“在标记为的所有字段中搜索单词”blue“   包含在全文搜索中。您不能使用自键路径   在类型不是字符串的字段中搜索。

因此,您可以使用'self'代替'%K'来搜索字符串字段的子文本。

For the full document written by Apple

相关问题