【问题标题】:Provide concrete type for Generic Protocol implementation为通用协议实现提供具体类型
【发布时间】:2016-03-17 11:31:39
【问题描述】:

是否可以做到以下几点:

protocol A: class {
    typealias T: AnyObject
}
extension A {
    func testA(a:Self, _ t:T)->Void{
        print(a, t)
    }
}
class B:A {
    typealias T = String
}

换句话说,我有协议并且想在符合它的类中提供具体类型。

【问题讨论】:

    标签: swift oop swift2 swift-protocols


    【解决方案1】:

    应该没问题。您拥有的代码的唯一问题是 String 不是 AnyObject

    你只需要:

    protocol A: class {
        typealias T
    }
    extension A {
        func testA(a:Self, _ t:T)->Void{
            print(a, t)
        }
    }
    class B:A {
        typealias T = String
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      相关资源
      最近更新 更多