swift - 来自数组元素的 NSPredicate IN 查询

标签 swift nspredicate

为古怪的标题道歉。我有一个 Ints 数组,我想定义一个 NSPredicate,它将过滤掉 connectionType 等于数组中包含的值的项目。

所以基本上是这样的:

fetchRequest.predicate = NSPredicate(format: "connectionType IN { all items in array }

我已经找到了一些我认为可以解决这个问题的 Objective C 示例,但我刚刚进入 iOS 开发并且只使用过 Swift,所以非常感谢这里的一些帮助:)

我试过这样做:

 fetchRequest.predicate = NSPredicate(format: "connectionType IN \(myIntArray)"

然而,这会返回一个 NSInvalidArgumentException。我觉得我很接近。

NSInvalidArgumentException', reason: 'Unable to parse the format string "connectionType IN [28, 28]" 

如果我执行以下操作:

fetchRequest.predicate = NSPredicate(format: "connectionType IN %@", myArray)

我得到的是这个错误:

NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (connectionType IN {28, 28})'

最佳答案

你会像这样构造一个谓词:

NSPredicate(format:"connectionType IN %@", yourIntArray)

希望对您有所帮助。

关于swift - 来自数组元素的 NSPredicate IN 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35188118/

相关文章:

swift:在登录 View Controller 中未调用 AWSCognitoIdentityInteractiveAuthenticationDelegate 方法

ios - 如何删除多个并行数组中的重复数据

ios - 将 componentsseparatedbystring 添加到具有核心数据的谓词中

ios - 使用 NSPredicate 过滤 NSArray

ios - NSPredicate 和过滤

swift - Swift 中 init 属性的语法糖?

ios - 如何在 Swift 中从不同的 ViewController 重新加载 TableView 中的数据

ios - 从解析后端删除后解析图像文件在应用程序中持久存在

ios - 如何在 iOS7 中搜索 NSString 句子中用逗号分隔的特定单词?

ios - NSPredicate with core data, search word with boundaries in string 属性