Arnie Schwarzvogel Arnie Schwarzvogel - 18 days ago 7
Swift Question

NSFetchedResultsController in iOS10 / Swift 3 - Still error message after conversion

I'm converting swift app to 3.0 and there is one issue left. I have read all instructions that NSFetchedResultController is now generic and modified the code but still getting the infamous "semicolon" error.

Original code:

fileprivate lazy var jobController:NSFetchedResultsController = { () -> <<error type>> in
let request = NSFetchRequest(entityName: AGJobData.entityName)
request.sortDescriptors = [NSSortDescriptor(key: "jobNumber", ascending: true), NSSortDescriptor(key: "name", ascending: true)]
let controller = NSFetchedResultsController(fetchRequest: request, managedObjectContext: AGDataManager.sharedManager.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
var error:NSError? = nil
controller.performFetch(&error)
controller.delegate = self
return controller
}()


Modified by me (manually, XCode convertor did not touch this piece of code):

fileprivate lazy var jobController:NSFetchedResultsController<AGJobData>= { () -> <<error type>> in
let request = NSFetchRequest<AGJobData>()
request.sortDescriptors = [NSSortDescriptor(key: "jobNumber", ascending: true), NSSortDescriptor(key: "name", ascending: true)]
let controller = NSFetchedResultsController(fetchRequest: request, managedObjectContext: AGDataManager.sharedManager.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
var error:NSError? = nil
controller.performFetch(&error)
controller.delegate = self
return controller
}()


Any further ideas?

Answer

The error handling has been changed, too.

fileprivate lazy var jobController : NSFetchedResultsController<AGJobData> = {        
   let request = NSFetchRequest<AGJobData>()
   request.sortDescriptors = [NSSortDescriptor(key: "jobNumber", ascending: true), NSSortDescriptor(key: "name", ascending: true)]
   let controller = NSFetchedResultsController(fetchRequest: request, managedObjectContext: AGDataManager.sharedManager.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
   controller.delegate = self
   do {
      try controller.performFetch(&error)
   } catch {
      print(error)
   }     
   return controller
}()

It could be that you have to change the NSFetchRequest line to

let request = NSFetchRequest<AGJobData>(entityName: "AGJobData")

The literal string must be the name of the entity.