NSPredicate过滤多个字符串

时间:2015-11-12 02:37:26

标签: swift nspredicate

如何使NSPredicate同时过滤多个对象?以下工作适用于一个对象(在本例中为KDED),但我试图使用swift2同时获得KDED,KDAB和KOMN。

let fetchRequest = NSFetchRequest(entityName: "Airport")
let sortDescriptor = NSSortDescriptor(key: "code", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
let predicate = NSPredicate(format: "code ==%@", "KDED")
    fetchRequest.predicate = predicate

if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {

    fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
    fetchResultController.delegate = self

    do {
        try fetchResultController.performFetch()
        Airports = fetchResultController.fetchedObjects as! [Airport]
    } catch {
       print(error)
    }
}

1 个答案:

答案 0 :(得分:0)

使用IN运算符:

let predicate = NSPredicate(format: "code IN %@", ["KDED", "KDAB", "KOMN"])