加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift关联类型和协议继承

发布时间:2020-12-14 04:54:47 所属栏目:百科 来源:网络整理
导读:我正在使用 Swift 2.2并且我声明了一个具有相关类型的协议如下: protocol CollectionViewModeling { associatedtype CellType func cellAtIndexPath(indexPath: NSIndexPath) - CellType} 现在我有一个符合上述协议的视图模型协议: enum MyItemCell { case
我正在使用 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

我收到这个错误:

Protocol ‘ItemsListViewModeling’ can only be used as a generic constraint because it has Self or associated type requirements

但我可以轻松创建一个实现此协议的类.

是否可以将var声明为关联的类型协议?因为我在协议ItemsListViewModeling中给出了相关类型的最终类型,所以我不明白为什么我从编译器中看到这个错误.

谢谢

解决方法

看那里 stackoverflow.com

You can’t treat protocols with associated types like regular protocols and declare them as standalone variable types.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读