如何制作同时过滤多个对象的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)
    }
}

最佳答案

使用IN运算符:

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

关于swift - NSPredicate过滤多个字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33663467/

10-13 09:30