请帮助我,我想在Swift 2.2 NSPredicate数组中为NSCompoundPredicate创建coredata get函数

public func loadData(entityName: String, filters: [String: String] = [:], dataFault: Bool = true) -> [NSManagedObject] {
    var listItems: [NSManagedObject]?
    let delegate = UIApplication.sharedApplication().delegate as? AppDelegate

    if let context = delegate?.managedObjectContext {
        let fetchRequist = NSFetchRequest(entityName: entityName)
        if (filters.count > 0) {
            var predicte: [NSPredicate]
            for (key, value) in filters {
                let p1:NSPredicate = NSPredicate(format: "%K == %@", key, value as NSObject)
                predicte.appendContentsOf(p1)
            }
            fetchRequist.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicte)
        }
        fetchRequist.returnsObjectsAsFaults = dataFault
        do{
            let results = try context.executeFetchRequest(fetchRequist)
            listItems = results as? [NSManagedObject]
        }
        catch let err {
            print(err)
        }

    }
    else{
        print("context Error")
    }
    return listItems!

}

在predicte.appendContentsOf(p1)上显示错误

最佳答案

在您的代码中,p1不是CollectionType,因此您需要使用append,而不是appendContentsOf:

                predicte.append(p1)

另外,您需要适当地初始化predicte:
            var predicte: [NSPredicate] = []

关于ios - 用于NSCompoundPredicate的NSPredicate数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38858484/

10-09 16:31