Hi! I've just witnessed a possible bug in decoding JSON from the server.
public struct MyNotification: Equatable, Decodable {
var notificationId: String
var title: String
var content: String
var date: Date
var status: MyNotificationStatus
private enum CodingKeys: String, CodingKey {
case notificationId = "id"
case title = "titulo"
case content = "conteudo"
case status = "status"
case date = "dataCriacao"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
notificationId = try container.decode(String.self, forKey: .notificationId)
title = try container.decode(String.self, forKey: .title)
content = try container.decode(String.self, forKey: .content)
Does NOT work properly:
date = try container.decode(Date.self, forKey: .date)
Does parse the correct date:
let dateInt = try container.decode(Int.self, forKey: .date)
date = Date(timeIntervalSince1970: TimeInterval(dateInt))
If I try to parse directly to a Date object a normal epoch timestamp eg.: 1606314559 it parses as 2051-11-26 and using the Int approach it parses correctly ( 2020-11-25 14:29:19 +0000 )
Am I missing something here? Shouldn't date = try container.decode(Date.self, forKey: .date)
already detect the correct date format and parse accordingly?