【发布时间】:2016-02-19 16:13:05
【问题描述】:
我有一个类型别名:
typealias BeaconId = [String: NSObject]
我想通过以下方式扩展它:
extension BeaconId {}
但这会引发编译错误:
约束扩展必须在非特化泛型类型“字典”上声明,约束由“where”子句指定
所以我最终做了:
extension Dictionary where Key: StringLiteralConvertible, Value: NSObject {}
有没有更清洁的方法来做到这一点?
【问题讨论】:
-
我试图清理你的代码,实际上得到了这个错误:
constrained extension must be declared on the unspecialized generic type 'Dictionary' with constraints specified by a 'where' clause -
是的,我也明白了,您可以在下面查看我的完整答案。基本上它看起来不可能扩展指定的泛型类型,只有那些尚未设置泛型类型的类型。
-
@Robert 我希望在 Swift 3 中看到的不仅仅是对协议和继承的约束,还有对值的约束,例如
extension SomeEnum where Self == .MyCase。这样,功能只能在特定的枚举案例中定义。
标签: swift extend type-alias