Last active
March 26, 2019 23:14
-
-
Save dkw5877/70aea707514bda25aae5ef701eaddb65 to your computer and use it in GitHub Desktop.
Date Decoding Strategy Key Encoding and Decoding in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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