Created
June 18, 2017 18:45
-
-
Save Palleas/645b6c30306603feaab148c9e167a307 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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