[英]Swift associated types and protocol inheritance
我正在使用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中給出了相關類型的最終類型,所以我不明白為什么我從編譯器中看到這個錯誤。
謝謝
您不能將協議類型的協議視為常規協議,並將它們聲明為獨立變量類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.