Created
March 26, 2019 23:07
-
-
Save dkw5877/9bec3f94e8cfd6660084743f3f6a8231 to your computer and use it in GitHub Desktop.
Key Encoding and Decoding in Swift
This file contains hidden or 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 = ["keyOne":"Bengal tiger", "keyTwo":"Siberian tiger", "keyThree ":"white rhino", "keyFour":"African elephant"] | |
let encoder = JSONEncoder() | |
encoder.keyEncodingStrategy = .convertToSnakeCase | |
encoder.outputFormatting = [.sortedKeys, .prettyPrinted] | |
let encoded = try? encoder.encode(object) | |
print(String(data: encoded!, encoding: .utf8)!) | |
//output | |
{ | |
"key_four" : "African elephant", | |
"key_one" : "Bengal tiger", | |
"key_three " : "white rhino", | |
"key_two" : "Siberian tiger" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment