3
我使用夫特2.2和我聲明相關聯型如下的協議:夫特相關類型和協議繼承
protocol CollectionViewModeling {
associatedtype CellType
func cellAtIndexPath(indexPath: NSIndexPath) -> CellType
}
現在我有一個視圖模型的協議符合上述協議:
enum MyItemCell {
case MyItemCell1, MyItemCell2
}
protocol ItemsListViewModeling: CollectionViewModeling {
associatedtype CellType = MyCell
}
最後,在其他地方,我想聲明的是是符合勒協議ItemsListViewModeling一個變種:
var viewModel: ItemsListViewModeling
而且我得到這個錯誤:
協議「ItemsListViewModeling」只能作爲一種通用的約束,因爲它具有自我或相關類型的要求
但我可以很容易地創建一個類實現這個協議。
是否有可能將var聲明爲關聯的類型化協議?當我在協議ItemsListViewModeling中給出關聯類型的最終類型時,我不明白爲什麼我從編譯器中看到這個錯誤。
感謝