泛型绕过约束

时间:2018-03-19 21:36:07

标签: swift generics

这是我的功能:

func get<T: Decodable>(type: T.Type, subclass: T){}

我有一组随机对象。符合协议Decodable的对象应该能够调用此函数。这就是我的意思:

class MasterClass{
    func get<T: Decodable>(type: T.Type, subclass: T){}
}

class A: MasterClass, Decodable{}

class B: MasterClass{}

func x(){
    let array: [MasterClass] = [A(), B()]
    for obj in array{
       if let conformer = obj as? Decodable{
           obj.get(type: type(of: conformer), subclass: conformer)
           //doesn't work
       }
   }
}

我没有这个工作,但我不明白为什么。

  

无法使用类型'的参数列表调用'get'(类型:   Decodable.Type,subclass:Decodable)'

0 个答案:

没有答案