Invalid Realm Object Predicate Search

Using Realm DB in a fast application. I am trying to filter the results using a predicate as follows:

class func fetchUsersFromDB(usersId: [String]) -> [User]{ var users = [User]() let realm = Realm() let predicate = NSPredicate(format: "objectId IN %@", argumentArray: usersId) var allUsers = realm.objects(User).filter(predicate) users = Array(allUsers) return users } 

But this does not compile. I get this error:

 Terminating app due to uncaught exception 'Invalid value', reason: 'IN clause requires an array of items' 

Any ideas what I'm doing wrong?

+6
source share
2 answers

Remove the argumentArray: label, since with it you are calling the wrong initializer for NSPredicate:

let predicate = NSPredicate(format: "objectId IN %@", usersId)

+5
source

With Swift 3, use Array(usersId) instead of usersId .

0
source

All Articles