我想过滤categoryId == value但我的谓词失败的地方。
这是我的代码。
categoryList是VenueCategory类的对象数组,categoryId是Category类的属性。
class Place: NSManagedObject {
class func GetPlaces(keys:[String],values:[String],forPlaceTab:Bool?,visitType:VisitType) -> [Place] {
//1
let managedContext = CoreDataStack.sharedStack().mainContext
var predicates = [NSPredicate]()
//2
let fetchRequest = NSFetchRequest(entityName:TABLE_PLACE)
for (index, key) in keys.enumerate() {
let value = values[index]
let predicateFormat = key != "categoryId" ? "\(key) == \(value)" : "ANY categoryList.categoryId == \(value)"
let resultPredicate = NSPredicate(format: predicateFormat)
predicates.append(resultPredicate)
}
if let forPlaceTab = forPlaceTab{
let predicateFormat = "isPlaceTab == \(forPlaceTab.boolValue)"
let resultPredicate = NSPredicate(format: predicateFormat)
predicates.append(resultPredicate)
}
fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
//3
//var error: NSError?
do {
if let results = try managedContext.executeFetchRequest(fetchRequest) as? [Place]{
return results
}
} catch let error as NSError {
print(error)
}
return []
}
}
答案 0 :(得分:0)
我遇到了问题。问题是categoryList中没有添加类别。谓词很好。