Created
April 11, 2020 20:18
-
-
Save DhavalDobariya86/99ef4a99cdb5a6429cf316d8b5bc5382 to your computer and use it in GitHub Desktop.
Codable with Key decoding strategy
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
import UIKit | |
struct Country: Codable { | |
let name: String | |
let capital: String | |
let callingCodes: [String] | |
} | |
let jsonData: Data = """ | |
{ | |
"name": "United States of America", | |
"capital": "Washington, D.C.", | |
"calling_codes": ["1"] | |
} | |
""".data(using: .utf8)! | |
let decoder = JSONDecoder() | |
// To convert from `snake-case` JSON key to `camelCase` parameter name | |
decoder.keyDecodingStrategy = .convertFromSnakeCase | |
let country = try! decoder.decode(Country.self, from: jsonData) | |
print("Country calling codes = \(country.callingCodes.description)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment