Skip to content

Instantly share code, notes, and snippets.

@jrBordet
Last active November 13, 2018 08:36
Show Gist options
  • Save jrBordet/3f772ef3552e67942bd108eb89b6ce5a to your computer and use it in GitHub Desktop.
Save jrBordet/3f772ef3552e67942bd108eb89b6ce5a to your computer and use it in GitHub Desktop.
// MARK: - Protocols
protocol User {
var name: String { get }
var email: String { get }
var address: String { get }
var line1: String? { get }
var line2: String? { get }
}
// MARK: - Models
struct MyUser: Codable, User {
var line2: String?
var line1: String?
var address: String
var email: String
var vat_number: String?
var name: String
enum CodingKeys: String, CodingKey {
case line2 = "line_two"
case line1
case address
case email
case vat_number
case name = "title"
}
}
// MARK: - User
let userJSON = """
{
"title": "jean",
"address": "indirizzo",
"email": "[email protected]",
"status": "single",
"line_two": "reg. Brognalle",
"status": "married"
}
"""
do {
let result = try JSONDecoder().decode(MyUser.self, from: userJSON.data(using: .utf8)!)
dump(result)
} catch {
dump(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment