Skip to content

Instantly share code, notes, and snippets.

@dkw5877
Created March 26, 2019 23:05
Show Gist options
  • Save dkw5877/45c38720081bb93dd8939cceecbe7f63 to your computer and use it in GitHub Desktop.
Save dkw5877/45c38720081bb93dd8939cceecbe7f63 to your computer and use it in GitHub Desktop.
Key Encoding and Decoding in Swift
let object = ["key1":"Bengal tiger", "key2":"Siberian tiger", "key3 ":"white rhino", "key4":"African elephant"]
let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
let encoded = try? encoder.encode(object)
print(String(data: encoded!, encoding: .utf8)!)
//output
{
"key1" : "Bengal tiger",
"key2" : "Siberian tiger",
"key3" : "white rhino",
"key4" : "African elephant"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment