Cannot invoke 'decode' with an argument list of type '(T, from: Data)'

后端 未结 1 1560
旧巷少年郎
旧巷少年郎 2021-01-06 20:45

I\'m trying to create a function that takes in a parameter of type \'Codable\' based on the custom JSON models being passed to it. The error :

 Cannot invoke         


        
相关标签:
1条回答
  • 2021-01-06 21:22

    The type of the type T is its metatype T.Type, therefore the function parameter must be declared as type: T.Type.

    You may also want to make the completion handle take a parameter of type T instead of Codable:

    static func updateDataModels <T : Codable> (url: serverUrl, type: T.Type,
             completionHandler:@escaping (_ details: T) -> Void) 
    

    When calling the function, use .self to pass the type as an argument:

    updateDataModels(url: ..., type: MainDataFamily.self) { ... }
    
    0 讨论(0)
提交回复
热议问题