是否可以在泛型中使用泛型?
我有这个协议
public protocol ListViewModelProtocol {
typealias ViewModel
typealias Cell
func titleForHeaderInSection(section: Int) -> String?
func numberOfSections() -> Int
func numberOfRowsInSection(section: Int) -> Int
func viewModelAtIndexPath(indexPath: NSIndexPath) -> ViewModel
}
我也有ListViewModel
实现这个协议的基础
public class BaseListViewModel<T, U> : ListViewModelProtocol {
}
但是已经在这里它说我ListViewModelProtocol
的没有实现。如何将 T 和 U 设置为协议内的特定类?因为如果我在协议中写这个
typealias ViewModel: CustomClass
typealias Cell: CustomCell
它仍然无法正常工作。
我的目标是子BaseListViewModel
类化
public class TestListViewModel : BaseListViewModel<TestCellViewModel, TestTableViewCell> {
}
然后我可以在我的BaseViewController
public class BaseViewController<T: ListViewModelProtocol>: UITableViewController {
}
在一些子类ViewController
中这样做:
public class CustomViewController: BaseViewController<TestListViewModel> {
}
这样CustomViewController
会“得到”TestCellViewModel
和TestTableViewCell
(实际上是它的 BaseViewController)。
但是,这当然不像我预期的那样工作。我错过了什么?或者我必须在每个实现它或将它用作泛型类型的类中typealias
定义?ListViewModelProtocol
这意味着我必须在类和类中定义ViewModel
and Cell
of ,但这不是那么通用,因为我只想将它们的基类型放在协议中,仅此而已。ListViewModelProtocol
BaseListViewModel
BaseViewController
或者也许我的方法有问题,我应该以不同的方式实施?
任何建议都是有用的。谢谢
编辑
我已经设法解决了这个问题,但我还有另一个问题。
public class BaseViewController<T: ListViewModelProtocol>: UITableViewController {
var dataSource: T?
}
这个数据源通过调用它自己的方法在 UITableViewDataSource 方法中使用(参见 ListViewModelProtocol 方法)。一切正常,但是当一些自定义控制器:
Controller: BaseViewController<TestListViewModel>
正在取消初始化我收到 EXC_BAD_ACCESS 错误。如果我把
deinit {
self.dataSource = nil
}
它有效,但我想知道为什么我需要将其设置为零。
谢谢。