如何为NSFetchedResultsController定义具有通用类型的协议?

时间:2018-08-24 11:35:30

标签: ios swift generics protocols

这就是我现在的定义方式:

protocol FetchedResultsControlable {
    var fetchedResultsController: NSFetchedResultsController<NSManagedObject>? { get set }
}

为了遵循该协议,我需要做些什么:

var fetchedResultsController: NSFetchedResultsController<Thread>?
var fetchedResultsController: NSFetchedResultsController<Task>?
var fetchedResultsController: NSFetchedResultsController<Notification>?

我该如何实现?

这就是我要使用的方式。只需扩展其他协议即可:

protocol NotificationsViewModelable: FetchedResultsControlable {
    var notifications: [Notificationable] { get }
}
class NotificationsViewModel: NotificationsViewModelable {
    var notifications: [Notificationable] {
        return fetchedResultsController?.fetchedObjects ?? []
    }
    var fetchedResultsController: NSFetchedResultsController<Notification>?
}

0 个答案:

没有答案