来自数组元素的 NSPredicate IN 查询

NSPredicate IN query from array elements

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

所以基本上是这样的:

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

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

我试过这样做:

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

然而,这 returns 一个 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)

希望对您有所帮助。