1
我需要声明两个协议,它们都具有associatedtypes:如何在两个协议之间共享关联类型?
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 – Alexander
@AlexanderMomchliov继承的协议 - 我已经尝试过(https://gist.github.com/anonymous/58fb6e95549081ba8eff9c7fc81d6fc2),它失败,出现同样的错误。 – MartinMoizard