在 Swift 3 中实现 fetchedresultsviewcontroller
Implementing fetchedresultsviewcontroller in Swift 3
我正在尝试在 Swift 3 中实现 fetchedresultsviewcontroller,但在将控制器的委托 属性 设置为自身时 运行 出现以下错误:
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
}()
有人可以向我解释这个问题以及我该如何解决吗?
因为您将委托设置为自己,所以您还需要使 SomeRootViewController 符合 NSFetchedResultsControllerDelegate 协议,如下所示:
class SomeRootViewController: NSFetchedResultsControllerDelegate, UIViewController, UITableViewDataSource, UITableViewDelegate {
我正在尝试在 Swift 3 中实现 fetchedresultsviewcontroller,但在将控制器的委托 属性 设置为自身时 运行 出现以下错误:
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
}()
有人可以向我解释这个问题以及我该如何解决吗?
因为您将委托设置为自己,所以您还需要使 SomeRootViewController 符合 NSFetchedResultsControllerDelegate 协议,如下所示:
class SomeRootViewController: NSFetchedResultsControllerDelegate, UIViewController, UITableViewDataSource, UITableViewDelegate {