Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DhavalDobariya86/5977f2c0207d707ac9a45a534f7a3dc6 to your computer and use it in GitHub Desktop.
Save DhavalDobariya86/5977f2c0207d707ac9a45a534f7a3dc6 to your computer and use it in GitHub Desktop.
Codable to exclude keys by removing it from CodingKeys enum
import UIKit
struct Country: Codable {
let name: String
let capital: String
let alternateSpellings: [String]
enum CodingKeys: String, CodingKey {
case name
case capital
case alternateSpellings = "altSpellings"
}
}
let jsonData: Data = """
{
"name": "United States of America",
"capital": "Washington, D.C.",
"callingCodes": ["1"],
"region": "Americas",
"subregion": "Northern America",
"population": 323947000,
"area": 9629091,
"borders": ["CAN", "MEX"],
"altSpellings": ["US", "USA","United States of America"]
}
""".data(using: .utf8)!
let country = try! JSONDecoder().decode(Country.self, from: jsonData)
print("Country name = \(country.name)")
print("Country capital = \(country.capital)")
print("Country alternate spellings = \(country.alternateSpellings.description)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment