Skip to content

Instantly share code, notes, and snippets.

@DhavalDobariya86
Created April 11, 2020 20:23
Show Gist options
  • Save DhavalDobariya86/581b810253930d10f1206b1c80849e7f to your computer and use it in GitHub Desktop.
Save DhavalDobariya86/581b810253930d10f1206b1c80849e7f to your computer and use it in GitHub Desktop.
Codable to exclude keys by removing it from parameter list
import UIKit
struct Country: Codable {
let name: String
let capital: String
}
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"]
}
""".data(using: .utf8)!
let country = try! JSONDecoder().decode(Country.self, from: jsonData)
print("Country name = \(country.name)")
print("Country capital = \(country.capital)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment