Search code examples
iosarraysswiftcore-datanspredicate

NSPredicate use array as filter


I'm trying to use an array as a filter to search Core Data. The values that the fetchRequest returns should have an ID that is in the predicate's array.

let arrayOfFavoriteStampIDS = [302,2300,2309]
fetchRequest.predicate = NSPredicate(format: "itemID IN %i", argumentArray: arrayOfFavoriteStampIDS)

Solution

  • Almost

    fetchRequest.predicate = NSPredicate(format: "itemID IN %@", arrayOfFavoriteStampIDS)
    

    argumentArray means something else for example

    fetchRequest.predicate = NSPredicate(format: "itemID == %i OR itemID == %i OR itemID == %i", argumentArray:arrayOfFavoriteStampIDS)
    

    In this case each item in the array must match one %i placeholder.