【发布时间】:2016-11-02 18:56:24
【问题描述】:
我需要声明两个协议,它们都有关联类型:
protocol MyView {
associatedtype DataType
associatedtype LayoutType : MyLayout
var data: DataType { get }
var layout: LayoutType { get }
func doLayout()
}
protocol MyLayout {
associatedtype DataType
func computeLayout(with data: DataType?)
}
根据当前的协议定义,MyView 的 associatedtype DataType 与 MyLayout 中的并不完全相同:
extension MyView {
func doLayout() {
layout.computeLayout(with: self.data)
^^^^^^^^^
Cannot convert value of type 'Self.DataType' to expected argument type '_?'
}
}
编译器告诉我们类型不一样。
有没有办法在两个协议之间共享关联类型来解决我的问题?谢谢。
【问题讨论】:
-
也许你的意思是制作一个继承自其他 2 的协议
-
@AlexanderMomchliov - 我已经尝试过了 (gist.github.com/anonymous/58fb6e95549081ba8eff9c7fc81d6fc2),但它失败并出现同样的错误。
标签: ios swift associated-types