【发布时间】:2021-11-19 05:33:22
【问题描述】:
我想在协议中声明一个带有关联类型的协议。
我知道将其声明为类而不是协议可以解决问题。
但我想在协议中使用它。
有没有办法使用泛型或类型别名之类的东西在协议中使用具有关联类型的协议?
protocol A {
associatedtype T
}
protocol B {
var a: A { get } // error. protocol 'A' can only be used as a generic constraint because it has Self or associated type requirements
// But I want set a.T = Int
}
【问题讨论】:
-
B中的A也可以是关联类型,您可以扩展B其中A将是另一个协议
标签: swift