【发布时间】:2019-06-15 21:06:20
【问题描述】:
我在 Playground 中有以下示例代码。如果结果符合Decodable 协议,我想解码网络请求的结果。
知道为什么这段代码不起作用吗?
protocol APIRequest {
associatedtype Result
}
func execute<T: APIRequest>(request: T) {
if let decodableResult = T.Result.self as? Decodable {
try JSONDecoder().decode(decodableResult, from: Data())
}
}
我在这一行收到错误Cannot invoke 'decode' with an argument list of type '(Decodable, from: Data)':try JSONDecoder().decode(decodableResult, from: Data())
非常感谢任何输入!
【问题讨论】:
标签: swift generics associated-types jsondecoder