0
votes

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

1 Answers

3
votes

Try this:

do {
    objects = try managedObjectContext.fetch(fetchRequest) as! [YourEntityName]
  } catch {
  print(error)
}