Skip to content

Instantly share code, notes, and snippets.

@Palleas
Created June 18, 2017 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Palleas/645b6c30306603feaab148c9e167a307 to your computer and use it in GitHub Desktop.
Save Palleas/645b6c30306603feaab148c9e167a307 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Foundation
let userPayload = """
{
"id": 1337,
"username": "Palleas"
}
""".data(using: .utf8)!
struct ID: Decodable {
let raw: Int
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.raw = try container.decode(Int.self)
}
}
struct User: Decodable {
let id: ID
let username: String
}
let decoder = JSONDecoder()
let u = try! decoder.decode(User.self, from: userPayload)
print("Username = \(u.username)")
print("User #\(u.id)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment