为什么我不能声明NSFetchResultsController?

时间:2016-08-19 23:00:42

标签: ios swift xcode

我正在使用Xcode版本8.0 beta 6.我正在尝试声明获取结果控制器,但它一直显示错误 “引用泛型类型”NSFetchedResultsController“需要< ...>

中的参数
   var fetchedResultsController : NSFetchedResultsController!

Xcode建议我使用

修复它
var fetchedResultsController : NSFetchedResultsController<AnyObject>!

也会抛出错误“Type'AnyObject'不符合协议'NSFetchRequestResult'

1 个答案:

答案 0 :(得分:0)

这里是我通常如何宣布我的:

lazy var fetchedResultsController: NSFetchedResultsController = {
    let fetchRequest = NSFetchRequest(entityName: "Friend")
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "lastMessage.timestamp", ascending: false)]
    let predicate = NSPredicate(format: "lastMessage.timestamp != nil")
    fetchRequest.predicate = predicate
    let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
    frc.delegate = self
    return frc

}()