简体   繁体   English

传递 Codable 结构时无法推断通用参数“T”

[英]Generic parameter 'T' could not be inferred when passing Codable struct

Please go through the code first for better understanding.请先通读代码以便更好地理解。

func getType<T: Decodable>(urlString: String, header: HTTPHeaders, completion: @escaping (T?) -> Void){

} }

let networkManager = DataManager()

 networkManager.getType(urlString: kGetMyDayTaskDetails + strId, header: header) { (MyDayAndTaskDetails) in

}

Error:错误:

Generic parameter 'T' could not be inferred

I am trying to pass the struct as parameter using Generics to use a common method for api calling.我正在尝试使用泛型将结构作为参数传递以使用通用方法进行 api 调用。 But not getting how to call correctly.但不知道如何正确调用。 Please guide.请指导。

Consider that the parameter in the closure is an instance , not a type.考虑到闭包中的参数是一个实例,而不是一个类型。 For example like例如像

networkManager.getType(urlString: kGetMyDayTaskDetails + strId, 
                          header: header) { details in

}

To specify the generic type annotate it and according to the signature it must be optional要指定泛型类型对其进行注释并根据签名它必须是可选的

networkManager.getType(urlString: kGetMyDayTaskDetails + strId, 
                          header: header) { (details : MyDayAndTaskDetails?) in 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM