问题
I am migrating to Swift 3 and have come across a very strange error message while migrating abstract CoreData query code. entityName is passed to the following method:
func objects(entityName name:String)->[NSManagedObject]? {
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName:name)
var objects: [NSManagedObject]?
do {
objects = try managedObjectContext.fetch(fetchRequest)
} catch { ... }
}
This results in the following error:
Cannot invoke 'fetch' with an argument list of type '(NSFetchRequest<NSFetchRequestResult>)' Expected an argument list of type '(NSFetchRequest<NSFetchRequestResult>)'
The error is stating I can't use the type its expecting. Is it possible to make abstract calls to CoreData like this in Swift 3?
The post How to apply the type to a NSFetchRequest instance? is what lead me this far.
I tried to cast fetchRequest, but it didn't change anything.
managedObjectContext.fetch(fetchRequest as! NSFetchRequest<NSFetchRequestResult>)
回答1:
Try this:
do {
objects = try managedObjectContext.fetch(fetchRequest) as! [YourEntityName]
} catch {
print(error)
}
来源:https://stackoverflow.com/questions/39779846/swift-3-xcode-8-nsmanagedobjectcontext-fetch-error