【发布时间】:2018-02-15 10:08:50
【问题描述】:
我希望实现类似于Notification.Name 的模式,任何人都可以稍后通过extension 添加一个模式,如下所示:
斯威夫特 4
public protocol Foo {
var bar: String { get }
}
public struct FooImpl: Foo {
public let bar: String
}
public extension Foo {
public static let baz: Foo = FooImpl(bar: "baz")
}
// Ideal usage:
someFuncThatTakesAFoo(.baz)
这对我来说似乎很好,但是在编译时我遇到了一个令人困惑的错误:
/path/to/main.swift:24:23: error: static stored properties not supported in generic types
public static let baz: Foo = FooImpl(bar: "baz")
~~~~~~ ^
这是怎么回事,有什么解决办法?
【问题讨论】:
-
比较:Extension may not contain stored property but why is static allowed。我同意在这种情况下诊断效果不佳。
-
相关 Swift 错误:SR-5856
标签: swift generics compiler-errors swift-extensions