如何使用泛型创建泛型NSFetchedResultsController?

时间:2018-09-07 12:53:02

标签: ios swift generics nsfetchedresultscontroller

我有一个符合协议FetchedResultsControlable的类:

protocol FetchedResultsControlable {
    var entityType: AnyClass
}

现在我需要创建 NSFRC

class A: FetchedResultsControlable {
    var entityType: AnyClass {
        return Order.classForCoder()
    }
    func setupFetchedResultsController(delegate: NSFetchedResultsControllerDelegate) {
        let fetchRequest = NSFetchRequest<entityType>(entityName: "\(entityType)")
        let identifierDescriptor = NSSortDescriptor(key: "identifier", ascending: false)
        fetchRequest.sortDescriptors = [identifierDescriptor]
        fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: storage.context, sectionNameKeyPath: nil, cacheName: nil)
        fetchedResultsController?.delegate = delegate
    }
}

我需要使用通用类型创建frc,但这不起作用;) 如何使用泛型来实现该功能?

0 个答案:

没有答案