在 Swift 3 中实现 fetchedresultsviewcontroller
Posted
技术标签:
【中文标题】在 Swift 3 中实现 fetchedresultsviewcontroller【英文标题】:Implementing fetchedresultsviewcontroller in Swift 3 【发布时间】:2017-02-27 18:09:18 【问题描述】:我正在尝试,在将控制器的委托属性设置为 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
()
有人可以向我解释一下这个问题以及如何解决它吗?
【问题讨论】:
【参考方案1】:因为您将委托设置为 self,您还需要使 SomeRootViewController 符合 NSFetchedResultsControllerDelegate 协议,如下所示:
class SomeRootViewController: NSFetchedResultsControllerDelegate, UIViewController, UITableViewDataSource, UITableViewDelegate
【讨论】:
以上是关于在 Swift 3 中实现 fetchedresultsviewcontroller的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Swift 3 for iOS 在 ViewController.swift 中实现 UICollectionView