一个常见的例子:
protocol Foo {
}
extension Foo {
static func abc() {
// ...
}
}
class Bar : Foo {
static func abc() {
// do something.
(self as Foo.Type).abc() // it works well
}
}
但是如果我在协议 Foo 中添加关联类型(如 SwiftUI.App),它会报告错误:
protocol Foo {
associatedtype Body = Scene
}
extension Foo {
static func abc() {
// ...
}
}
class Bar : Foo {
static func abc() {
// do something.
(self as Foo.Type).abc() // it reports error
}
}
Protocol 'Foo' 只能用作通用约束,因为它具有 Self 或关联类型要求
? 如何解决?