4
votes

I am getting an exception when trying to create NSPredicate object that goes like:

let searchPredicate = NSPredicate(format: "addonCategoryId IN \(idsArray)")

where idsArray is made up of Int objects in swift array. Exception is raised at this line itself. Console says:

2015-10-08 08:21:42.878 bkApp[2569:510538] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "addonCategoryId IN [5404, 5406]"'

What could be the issue? Thanks for taking a look. :)

1

1 Answers

6
votes

NSPredicate is still expecting Objective-C style format string. You used string interpolation.

let searchPredicate = NSPredicate(format: "addonCategoryId IN \(idsArray)")
// result: addonCategoryId IN [5404, 5406]

Try this:

let searchPredicate = NSPredicate(format: "addonCategoryId IN %@", idsArray)
// result: addonCategoryId IN {5404, 5406}

The difference is subtile [] vs. {} but if NSPredicate doesn't like it, it doesn't like it.