Ios Swift3:lazy var fetchrequestcontroller显示错误

时间:2016-10-25 11:29:50

标签: ios core-data controller swift3 fetch

我在表视图控制器中使用了一个lazy var来从CoreData获取数据。它在swift2中运行良好。但是当我升级到Swift3时,它显示如下错误。请帮我改变Swift3

  

错误:无法转换类型'错误'的值指定类型' NSFetchedResultsController'

lazy var fetchedResultsController: NSfetchedResultsController =
{
    let fetchRequest = NSFetchRequest(entityName: "EvtIvtTbl")
    let sortDescriptor = NSSortDescriptor(key: "bym_kol", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]

    let fetchedResultsController = NSfetchedResultsController(fetchRequest: fetchRequest,
                                                     managedObjectContext: self.managedObjectContext,
                                                     sectionNameKeyPath: nil,
                                                     cacheName: nil)
    fetchedResultsController.delegate = self
    return fetchedResultsController
}()

1 个答案:

答案 0 :(得分:1)

在Swift 3.0中,NSFetchedResultsControllerNSFetchRequest需要类型参数。假设EvtIvtTbl是您的NSManagedObject子类,您的代码应该如下所示:

lazy var fetchedResultsController: NSFetchedResultsController<EvtIvtTbl> = {
    let fetchRequest = NSFetchRequest<EvtIvtTbl>(entityName: "EvtIvtTbl")
    let sortDescriptor = NSSortDescriptor(key: "bym_kol", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]

    let fetchedResultsController = NSFetchedResultsController<EvtIvtTbl>(fetchRequest: fetchRequest,
                                                 managedObjectContext: self.managedObjectContext,
                                                 sectionNameKeyPath: nil,
                                                 cacheName: nil)
    fetchedResultsController.delegate = self
    return fetchedResultsController
}()