Skip to content

Instantly share code, notes, and snippets.

@steveriggins
Created October 1, 2017 00:04
Show Gist options
  • Save steveriggins/10dfd72fe11f698e5c3452cf42ce92b7 to your computer and use it in GitHub Desktop.
Save steveriggins/10dfd72fe11f698e5c3452cf42ce92b7 to your computer and use it in GitHub Desktop.
Using Dictionary with Codable
// 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