Swift 3/XCode 8 NSManagedObjectContext.fetch error

谁都会走 提交于 2019-12-13 07:05:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!