Skip to content

Instantly share code, notes, and snippets.

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