问题描述
为一个古怪的标题道歉.我有一个 Int 数组,我想定义一个 NSPredicate 来过滤掉 connectionType 等于数组中包含的值的项目.
Apologies for a quirky title. I have an array of Ints and I would like to define an NSPredicate that will filter out items with connectionType equals to a value contained in the array.
所以基本上是这样的:
fetchRequest.predicate = NSPredicate(format: "connectionType IN { all items in array }
我发现了一些我认为可以解决这个问题的 Objective C 示例,但我刚刚进入 iOS 开发并且只使用过 Swift,所以在这里提供一些帮助将不胜感激:)
I've found some Objective C examples that I think deals with this, but I just got into iOS development and have only worked with Swift, so some help here would be greatly appreciated :)
我尝试这样做:
fetchRequest.predicate = NSPredicate(format: "connectionType IN \(myIntArray)"
然而,这会返回一个 NSInvalidArgumentException.我觉得我很接近.
However, this returns an NSInvalidArgumentException. I feel I'm close though.
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})'
推荐答案
你会构造一个这样的谓词:
You would construct a predicate like this:
NSPredicate(format:"connectionType IN %@", yourIntArray)
我希望这会有所帮助.
这篇关于来自数组元素的 NSPredicate IN 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!