我在我的项目中使用一般的CoreData查询方法。Swift3:将参数传递到NSFetchRequest方法
func query(table: String, searchPredicate: NSPredicate) -> [AnyObject]
{
let context = app.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: table)
fetchRequest.predicate = searchPredicate
let results = try! context.fetch(fetchRequest)
return results
}
在Swift 3中这是行不通的。我发现这对苹果公司的网站:
func findAnimals()
{
let request: NSFetchRequest<Animal> = Animal.fetchRequest
do
{
let searchResults = try context.fetch(request)
... use(searchResults) ...
}
catch
{
print("Error with request: \(error)")
}
}
使用苹果的例子,我怎么会在通过动物的方法作为参数,使findAnimals更通用的?
其实如果我有机会,我张贴后花更多的时间与此:
与称它为我会意识到这是一直以来的正确答案。 – iphaaw