NSPredicate IN request from array elements

Sorry for the fancy name. I have an Ints array, and I would like to define an NSPredicate that will filter elements with connectionType equal to the value contained in the array.

So basically something like:

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

I found some examples of Objective-C that I think deal with this, but I just started working with iOS and only worked with Swift, so it will be very helpful to help us :)

I tried to do this:

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

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

 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})' 
+5
source share
1 answer

You would build a predicate like this:

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

I hope this will be helpful.

+9
source

Source: https://habr.com/ru/post/1242247/


All Articles