I'm trying to implement fetchedresultsviewcontroller in Swift 3 and am running into the following error when setting the delegate property of the controller to self:
Cannot assign value of type 'SomeRootViewController' to type 'NSFetchedResultsControllerDelegate?'
SomeRootViewController.swift
@available(iOS 10.0, *)
@objc class SomeRootViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var delegate: SomeRootViewControllerDelegate?
public var context: NSManagedObjectContext!
private let persistentContainer = NSPersistentContainer(name: "Accessory")
fileprivate lazy var fetchedResultsController: NSFetchedResultsController<Accessory> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<Accessory> = Accessory.fetchRequest() as! NSFetchRequest<Accessory>
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "AccessoryAttributes.name", ascending: true)]
// Create Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
// Configure Fetched Results Controller
fetchedResultsController.delegate = self //<<-- this is where error occurs
return fetchedResultsController
}()
Can somebody explain the problem to me and how I can fix it?
Because you are setting delegate to self, you'll also need to make SomeRootViewController conform to the NSFetchedResultsControllerDelegate protocol, like this:
class SomeRootViewController: NSFetchedResultsControllerDelegate, UIViewController, UITableViewDataSource, UITableViewDelegate {