1

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>)

Community
  • 1
  • 1
dmorrow
  • 5,152
  • 5
  • 20
  • 31

1 Answers1

4

Try this:

do {
    objects = try managedObjectContext.fetch(fetchRequest) as! [YourEntityName]
  } catch {
  print(error)
}
Karanveer Singh
  • 961
  • 12
  • 27
  • Thank you for this Karanveer. I was hung up on the error message, and didn't think to look at the obvious casting issue in front of me. Bug report has been submitted to Apple. https://openradar.appspot.com/radar?id=5049582866137088 – dmorrow Sep 30 '16 at 15:23
  • @dmorrow Good. There are other bugs too like there are layout issues. You may have to change something in your Storyboard file. Like in File Inspector, you may have to change an option from Xcode 8.0 to Xcode 7.x – Karanveer Singh Oct 01 '16 at 05:51