クリぼっちの自分が信じられるのはもはや型のみである
by
Ryota Sakaguchi
×
Copy
Open
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Slide 1
Slide 1 text
Slide 2
Slide 2 text
Slide 3
Slide 3 text
Slide 4
Slide 4 text
{ "shop": { "id": 1, "name": "", "employees": [ { "id": 1, "job_id": 1, "name": "kuluna" } ] } }
Slide 5
Slide 5 text
import Foundation struct Employee: Decodable { let id: Int let job_id: Int let name: String } struct Shop: Decodable { let id: Int let name: String let employees: [Employee] } struct ShopResponse: Decodable { let shop: Shop }
Slide 6
Slide 6 text
let response = try! JSONDecoder().decode( ShopResponse.self, from: json.data(using: .utf8)! ) print(response.shop.name) // “”
Slide 7
Slide 7 text
☺
Slide 8
Slide 8 text
Slide 9
Slide 9 text
let shop = response.shop shop.id == shop.employees.first?.job_id
Slide 10
Slide 10 text
let newEmployee = Employee( id: shop.id, job_id: 0, name: "Sakaguchi" )
Slide 11
Slide 11 text
!
Slide 12
Slide 12 text
-
Slide 13
Slide 13 text
!
Slide 14
Slide 14 text
struct ShopId: Codable, Equatable { let value: Int init(_ value: Int) { self.value = value } public init(from decoder: Decoder) throws { self.value = try decoder.singleValueContainer().decode(Int.self) } public func encode(to encoder: Encoder) throws { var c = encoder.singleValueContainer() try c.encode(self.value) } }
Slide 15
Slide 15 text
struct Shop: Decodable { let id: ShopId let name: String let employees: [Employee] }
Slide 16
Slide 16 text
☺ ☺ Comparable