gpt4 book ai didi

ios - NSCompoundPredicate 的 NSPredicate 数组

转载 作者:行者123 更新时间:2023-11-28 06:37:05 24 4
gpt4 key购买 nike

请帮助我,我想在 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)

此外,您需要适本地初始化您的预测:

            var predicte: [NSPredicate] = []

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com