协议“App”只能用作通用约束,因为它具有 Self 或关联的类型要求

时间:2021-04-06 04:27:51

标签: ios swift swiftui associated-types

一个常见的例子:

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 或关联类型要求

? 如何解决?

0 个答案:

没有答案