Skip to content

Instantly share code, notes, and snippets.

@dkw5877
Last active March 26, 2019 23:14
Show Gist options
  • Save dkw5877/70aea707514bda25aae5ef701eaddb65 to your computer and use it in GitHub Desktop.
Save dkw5877/70aea707514bda25aae5ef701eaddb65 to your computer and use it in GitHub Desktop.
Date Decoding Strategy Key Encoding and Decoding in Swift
struct TestDecode:Codable {
let updated:Date
}
let json = """
{
"updated":"2019-2-21T02:02:55-08:00"
}
"""
let data = Data(json.utf8) //convert to data
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let decoded = try decoder.decode(TestDecode.self, from: data)
print(type(of: decoded.updated))
print(decoded.updated)
//output
Date
2019-02-21 10:02:55 +0000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment