0

我正在尝试在 Swift 3 中实现 fetchedresultsviewcontroller,并且在将控制器的委托属性设置为 self 时遇到以下错误:

无法将“SomeRootViewController”类型的值分配给“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
    }()

有人可以向我解释这个问题以及如何解决它吗?

4

1 回答 1

2

因为您将委托设置为 self,您还需要使 SomeRootViewController 符合 NSFetchedResultsControllerDelegate 协议,如下所示:

class SomeRootViewController: NSFetchedResultsControllerDelegate, UIViewController, UITableViewDataSource, UITableViewDelegate {
于 2017-02-27T19:07:40.290 回答