简体   繁体   English

使用NSPredicate和array进行cloudKit搜索

[英]Using NSPredicate with array for cloudKit search

When using a NSPredicate , I'm trying to search all objects (strings) contained within an array. 使用NSPredicate ,我试图搜索数组中包含的所有对象(字符串)。 The code sample below works but the predicate only collects the object in the first index only?? 下面的代码示例有效,但谓词仅在第一个索引中收集对象? The NSPredicate is used for a CKQueryOperation . NSPredicate用于CKQueryOperation Each Record has a value for a key named Category . 每个Record都有一个名为Category的键的值。

    let array: [String] = ["Education", "Sport", "TV and Film"]

    // select all records
    //let predicate = NSPredicate(format: "Category = %@", category )



    let predicate = NSPredicate (format: "Category == %@", argumentArray: array)

    let query = CKQuery(recordType: "quizRecord", predicate: predicate)

    // get just one value only
    let operation = CKQueryOperation(query: query)

  //code works but only queries Records with the Category for "Education" 

Try replacing: 尝试更换:

"Category == %@"

With: 附:

"Category IN %@"

I was having the same issue as Peter Wiley. 我遇到了与彼得·威利(Peter Wiley)相同的问题。 The solution I found was a mash up of Danny Bravo's solution and comment on that solution. 我找到的解决方案是Danny Bravo解决方案的混搭,并对该解决方案发表了评论。

let arrayPredicate = NSPredicate(format: "Name IN %@", argumentArray: [names])

To get multiple results I needed to use both the keyword "IN" in the format and wrap my argumentArray with []. 要获得多个结果,我需要同时使用格式中的关键字“ IN”,并用[]包装我的argumentsArray。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM