我正在使用 Swift 2.2,并声明了一个关联类型的协议,如下所示:
protocol CollectionViewModeling {
associatedtype CellType
func cellAtIndexPath(indexPath: NSIndexPath) -> CellType
}
现在我有一个符合上述协议的视图模型协议:
enum MyItemCell {
case MyItemCell1, MyItemCell2
}
protocol ItemsListViewModeling: CollectionViewModeling {
associatedtype CellType = MyCell
}
最后,在其他地方,我想声明一个符合 le 协议 ItemsListViewModeling 的 var:
var viewModel: ItemsListViewModeling
我收到了这个错误:
协议“ItemsListViewModeling”只能用作通用约束,因为它具有 Self 或关联的类型要求
但是我可以很容易地创建一个实现这个协议的类。
是否可以将 var 声明为关联的类型化协议?当我在协议 ItemsListViewModeling 中给出关联类型的最终类型时,我不明白为什么我会从编译器中看到这个错误。
谢谢