NSPredicate IN query from array elements

穿精又带淫゛_ 提交于 2019-12-06 20:14:16

问题


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.

So basically something like:

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

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 :)

I tried doing this:

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

However, this returns an NSInvalidArgumentException. I feel I'm close though.

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

If I do the following:

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

I get this error instead:

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

回答1:


You would construct a predicate like this:

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

I hope this is helpful.



来源:https://stackoverflow.com/questions/35188118/nspredicate-in-query-from-array-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!