Does anybody know how I can solve this error that I'm getting? The error is received on the first line in the following chunk of code:
let fetchedResultsController: NSFetchedResultsController = {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Message")
let delegate = UIApplication.shared.delegate as! AppDelegate
let context = delegate.persistentContainer.viewContext
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
return frc
}()
Try adding the return type in the closure like this code:
let fetchedResultsController: NSFetchedResultsController = { () -> NSFetchedResultsController<NSFetchRequestResult> in
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Message")
let delegate = UIApplication.shared.delegate as! AppDelegate
let context = delegate.persistentContainer.viewContext
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
return frc
}()
The error message is a bit misleading, the problem is that you did not specify the generic placeholder type for the variable.
You can either add an explicit return type to the closure, as @Mukesh suggested, in that case the type annotation on the variable is not needed:
let fetchedResultsController = { () -> NSFetchedResultsController<NSFetchRequestResult> in
// ...
return frc
}()
Or fully specify the type of the variable, then the closure return type is inferred automatically:
let fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult> = {
// ...
return frc
}()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With