说我有协议
protocol MyProtocol {
/// A bunch of useful static properties
}
extension MyProtocol {
static var allImplementors: [any MyProtocol.Type] {
[MyProtocolImplementorA.self, MyProtocolImplementorB.self]
}
}对我来说一切都很好。当我去使用它时,我有:
@State var currentSelected: any MyProtocol.Type = MyProtocolImplementorA.self但当我试图用:
@State var currentSelected: any MyProtocol.Type = .allImplementors.first!它失败的类型‘任何MyProtocol.Type’没有成员'allImplementors‘,即使是自动完成工程。
我该怎么做?或者我应该使用静态创建一个助手类:
class MyProtocolStatic {
static let allImplementors: [any MyProtocol.Type] = [MyProtocolImplementorA.self, MyProtocolImplementorB.self]
}发布于 2022-08-04 15:33:47
来自:Swift: Providing a default protocol implementation in a protocol extension
看来答案是否定的。我只是要用一个助手类。
https://stackoverflow.com/questions/73237260
复制相似问题