Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a NSFetchedResultsController in iOS 10 Swift 3

I can't get my NSFetchedResultsController initialized in iOS 10 using Swift 3 within CoreDataTableViewController from AECoreDataUI.

let request = NSFetchRequest<NasaPicture>(entityName:"NasaPicture")  
request.predicate = Predicate(format: "isPagedResult == YES")  
request.sortDescriptors = [SortDescriptor(key: "date", ascending: false)]  
fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)  

Compiler is complaining that

"Cannot convert value of type NSFetchedResultsController<NasaPicture> to expected type NSFetchedResultsController<_>"

The controller is now using generic type for swift, but it is not inferring the entity type correctly. I've tried explicitly:

fetchedResultsController = NSFetchedResultsController<NasaPicture>(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)  

No luck either.

Thanks!

like image 737
Jason C. Howlin Avatar asked Jun 26 '16 09:06

Jason C. Howlin


1 Answers

NSFetchRequest is now a generic. NSFetchedResultsController is a generic too. Therefore, when declaring variables, you have to use a generic declaration, e.g.:

var fetchedResultsController: NSFetchedResultsController<NasaPicture>?
like image 92
Sulthan Avatar answered Oct 18 '22 22:10

Sulthan