Skip to content

Instantly share code, notes, and snippets.

@rintoandrews90
Created July 27, 2019 17:03
Show Gist options
  • Save rintoandrews90/cd5edf2be3660e380ce7879625757ad5 to your computer and use it in GitHub Desktop.
Save rintoandrews90/cd5edf2be3660e380ce7879625757ad5 to your computer and use it in GitHub Desktop.
struct UserInfo {
var userId: String
var jobTitleName: String
var firstName: String
var lastName: String
var preferredFullName: String
var employeeCode: String
var region: String
var phoneNumber: String
var emailAddress: String
var salary: Double
enum CodingKeys: String, CodingKey {
case userId
case jobTitleName
case preferredFullName
case employeeCode
case region
case phoneNumber
case emailAddress
case salary
case name
}
enum NameKeys: String, CodingKey {
case firstName
case lastName
}
}
extension UserInfo: Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
userId = try values.decode(String.self, forKey: .userId)
jobTitleName = try values.decode(String.self, forKey: .jobTitleName)
preferredFullName = try values.decode(String.self, forKey: .preferredFullName)
employeeCode = try values.decode(String.self, forKey: .employeeCode)
region = try values.decode(String.self, forKey: .region)
phoneNumber = try values.decode(String.self, forKey: .phoneNumber)
emailAddress = try values.decode(String.self, forKey: .emailAddress)
salary = try values.decode(Double.self, forKey: .salary)
let name = try values.nestedContainer(keyedBy: NameKeys.self, forKey: .name)
firstName = try name.decode(String.self, forKey: .firstName)
lastName = try name.decode(String.self, forKey: .lastName)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment