如何使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)
}
}
答案 0 :(得分:0)
使用IN
运算符:
let predicate = NSPredicate(format: "code IN %@", ["KDED", "KDAB", "KOMN"])