【问题标题】:Extension on a Generic class泛型类的扩展
【发布时间】:2020-09-17 21:29:31
【问题描述】:

我有课

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!")
   }
}

当然这不起作用,但是有没有办法让它起作用?

尽可能明确会有所帮助

【问题讨论】:

    标签: ios swift generics associated-types


    【解决方案1】:

    你的语法有点错误。 RawValue 属于 T,所以你需要这样引用它:

    extension MyClass where T: Codable & RawRepresentable & CaseIterable,
                            T.RawValue == String { ... }
                            ^^
    

    【讨论】:

    • 我知道这很简单!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    相关资源
    最近更新 更多