Created
October 1, 2017 00:04
-
-
Save steveriggins/10dfd72fe11f698e5c3452cf42ce92b7 to your computer and use it in GitHub Desktop.
Using Dictionary with Codable
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
// Example of encoding and decoding a Swift Dictionary | |
let d = [ | |
"Casey": 35, | |
"Erin": 34, | |
"Declan": 2 | |
] | |
print("Original Dictionary: \(d)") | |
let encoder = JSONEncoder() | |
if let encoded = try? encoder.encode(d) | |
{ | |
let decoder = JSONDecoder() | |
if let decoded = try? decoder.decode(type(of: d), from: encoded) { | |
print("Decoded Dictionary: \(decoded)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment