NSPredicate IN来自数组元素的查询

ard*_*evd 12 nspredicate swift

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

所以基本上是这样的:

fetchRequest.predicate = NSPredicate(format: "connectionType IN { all items in array }
Run Code Online (Sandbox Code Playgroud)

我发现了一些Objective C示例,我认为这是处理这个问题,但我刚刚进入iOS开发并且只使用了Swift,所以在这里有一些帮助会非常感激:)

我试过这样做:

 fetchRequest.predicate = NSPredicate(format: "connectionType IN \(myIntArray)"
Run Code Online (Sandbox Code Playgroud)

但是,这会返回NSInvalidArgumentException.我觉得我很接近.

NSInvalidArgumentException', reason: 'Unable to parse the format string "connectionType IN [28, 28]" 
Run Code Online (Sandbox Code Playgroud)

如果我执行以下操作:

fetchRequest.predicate = NSPredicate(format: "connectionType IN %@", myArray)
Run Code Online (Sandbox Code Playgroud)

我得到了这个错误:

NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (connectionType IN {28, 28})'
Run Code Online (Sandbox Code Playgroud)

Ana*_*i P 25

你可以构造一个这样的谓词:

NSPredicate(format:"connectionType IN %@", yourIntArray)
Run Code Online (Sandbox Code Playgroud)

我希望这是有帮助的.