通用扩展 class
Extension on a Generic class
我有一个class
class MyClass<T> { /***/ }
我想扩展:
extension MyClass where T: Codable & RawRepresentable & CaseIterable { /***/ }
我想限制对 String 类型的 RawValues 的扩展,所以我正在尝试做类似的事情:
extension MyClass where T: Codable & RawRepresentable & CaseIterable, RawValue == String {
func doSomething() {
print("doing something with strings!")
}
}
当然这行不通,但有没有办法让它起作用?
尽可能明确会有所帮助
你的语法有点错误。 RawValue
属于 T
,所以你需要这样引用它:
extension MyClass where T: Codable & RawRepresentable & CaseIterable,
T.RawValue == String { ... }
^^
我有一个class
class MyClass<T> { /***/ }
我想扩展:
extension MyClass where T: Codable & RawRepresentable & CaseIterable { /***/ }
我想限制对 String 类型的 RawValues 的扩展,所以我正在尝试做类似的事情:
extension MyClass where T: Codable & RawRepresentable & CaseIterable, RawValue == String {
func doSomething() {
print("doing something with strings!")
}
}
当然这行不通,但有没有办法让它起作用?
尽可能明确会有所帮助
你的语法有点错误。 RawValue
属于 T
,所以你需要这样引用它:
extension MyClass where T: Codable & RawRepresentable & CaseIterable,
T.RawValue == String { ... }
^^