Created
March 26, 2019 23:05
-
-
Save dkw5877/45c38720081bb93dd8939cceecbe7f63 to your computer and use it in GitHub Desktop.
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
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