【发布时间】:2019-06-11 17:14:16
【问题描述】:
我正在尝试覆盖 JSONDecoder 解码数据的方式。
我尝试了以下方法:
struct Response : Decodable {
init(from decoder: Decoder) throws {
print("Hello")
}
}
let result = try JSONDecoder().decode(Response.self, from: Data())
但是init(from:) 没有被调用。
基本上我希望JSONDecoder 在将空数据解码为空Response 对象时始终成功
【问题讨论】:
-
它没有被调用,因为你没有给它任何 JSON 数据来解码。