所以我们的 API 中有两种类型的 JSON 响应:
{
"data": { } // some object in here
"meta": { } // an object in here
}
和
{
"data": [ ] // array of objects
"meta": { } // an object in here
}
为了解码它们,我们使用 JSONDecoder() 和以下通用响应结构:
public struct Response<T: Codable>: Codable {
public let data: T
public let meta: Meta?
}
这适用于 Swift 4.0 使用.map(Response<[MyObject]>.self)
or.map(Response<MySingleObject>.self)
但由于某种原因,这不再适用于 Swift 4.1 和 Xcode 9.3。看起来它根本没有映射“数据”,因此认为列表[MyObject]
位于第一级。
dataCorrupted: Swift.DecodingError.Context
▿ codingPath: 3 elements
- CodingKeys(stringValue: "data", intValue: nil)
▿ _JSONKey(stringValue: "Index 0", intValue: 0)
- stringValue: "Index 0"
▿ intValue: Optional(0)
- some: 0
- CodingKeys(stringValue: "creationDate", intValue: nil)
- debugDescription: "Date string does not match format expected by formatter."
请注意,“creationDate”是 的属性MyObject
。日期格式绝对正确(.formatted(customFormatter)
在解码器中设置为),因为它适用于 Xcode 9.2 中的 Swift 4.0
我们怎样才能在 Swift 4.1 中保持相同的行为?此处的目标不是为每个 API 响应创建类型化Response
类型,而是使用泛型,因为唯一的区别是响应对象类型,有时它返回一个列表,有时返回一个下面的对象data
。
也相关:有没有办法强制执行,如果我们使用Response<[MyObject]>.self
它也MyObject
必须符合Codable
?
提前致谢。
编辑:
下面的代码在 Xcode 9.2 和 Swift 4 中正确映射,但在 Xcode 9.3 和 Swift 4.1 中没有映射(创建 nil)
public struct MyObject: Codable {
public let creationDate: Date
enum CodingKeys: String, CodingKey {
case creationDate = "creation_date"
}
}
public struct Response<T: Codable>: Codable {
public let data: T
public let meta: Meta?
}
public struct Meta: Codable {
public let count: Int?
}
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-mm-dd HH:mm:ss"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(formatter)
let jsonDataSingle: Data = """
{
"data": { "creation_date": "2018-04-29 18:00:11" },
"meta": null
}
""".data(using: .utf8)!
let jsonDataList: Data = """
{
"data": [{ "creation_date": "2018-04-10 17:00:11" }, { "creation_date": "2018-04-29 18:00:11" }],
"meta": null
}
""".data(using: .utf8)!
let singleObject = try? decoder.decode(Response<MyObject>.self, from: jsonDataSingle)
dump(singleObject)
let listOfObjects = try? decoder.decode(Response<[MyObject]>.self, from: jsonDataList)
dump(listOfObjects)