谓词不适用于NSSet

时间:2016-09-22 10:31:37

标签: ios swift core-data nspredicate

我想过滤categoryId == value但我的谓词失败的地方。 这是我的代码。
categoryList是VenueCategory类的对象数组,categoryId是Category类的属性。

Place

 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 []
}
}

1 个答案:

答案 0 :(得分:0)

我遇到了问题。问题是categoryList中没有添加类别。谓词很好。