Slide 1

Slide 1 text

Codableͷinit(from:)Λ Ͳ͏ॻ͔͘ by. 2017/6/21 WWDC After Party 2017 @Ebisu 1

Slide 2

Slide 2 text

takasek iOS Developer ϑϦʔϥϯε(΄΅FiNC) @takasek OSS ActionClosurable Notifwift౳ 2

Slide 3

Slide 3 text

QiitaͰCodableؔ࿈ͷهࣄ ࿈౤ͯ͠·͢ 3

Slide 4

Slide 4 text

- લஔ͖ - Codable ஌ͬͯ·͔͢ʁ 4

Slide 5

Slide 5 text

Codable introduced in WWDC2017 https://developer.apple.com/videos/play/wwdc2017/212/ 5

Slide 6

Slide 6 text

Codable͸ϞςϞς ֤ॴWWDCৼΓฦΓձͰ౓ʑϐοΫΞοϓத 6/14 AKIBA.swift / by fromkkɹ6/15 eureka Meetup / by Muukiiɹ6/19 CA.swift / by inamiy !ͬ͘͟Γ֓ཁ௫ΊΔࢿྉɹɹ!DecodingErrorʹ͍ͭͯৄ͍͠ɹɹ!I/FɺίϯύΠϥڍಈʹ͍ͭͯ 6

Slide 7

Slide 7 text

7

Slide 8

Slide 8 text

8

Slide 9

Slide 9 text

9

Slide 10

Slide 10 text

- ຊ୊ -ɹ init(from:) ΛͲ͏ॻ͔͘ 10

Slide 11

Slide 11 text

Codable Philosophy 4 Error handling built-in 4 ૊Έࠐ·ΕͨΤϥʔ੍ޚ 4 Encapsulate encoding details 4 ΤϯίʔσΟϯάͷৄࡉΛΧϓηϧԽ 4 Abstract format from type 4 ந৅Խ͞ΕͨϑΥʔϚοτ 11

Slide 12

Slide 12 text

Codable Philosophy 4 Error handling built-in 4 ૊Έࠐ·ΕͨΤϥʔ੍ޚ 4 Encapsulate encoding details 4 ΤϯίʔσΟϯάͷৄࡉΛΧϓηϧԽ 4 Abstract format from type 4 ந৅Խ͞ΕͨϑΥʔϚοτ 12

Slide 13

Slide 13 text

CodableɺCoderɺContainer 13

Slide 14

Slide 14 text

4 (En|De)codable 4 CoderͱContainerΛ࢖͏͕ɺ ͦͷৄࡉ͸஌Βͳ͍ϐϡΞͳܕ 4 (En|De)coder 4 ࣗ෼ͷ۩ମܕʹ͍ͭͯCodable ʹ͸஌Βͤͳ͍ 4 Codableͷ఩ֶʮந৅Խ͞Εͨ ϑΥʔϚοτʯͷମݱऀ 4 (Keyed|Unkeyed|SingleValue) (En|De)codableContainer 4 σʔλΛอ࣋͠ɺϓϩύςΟͷ ม׵ํ๏Λఏڙ͢Δ 4 Codableͷ఩ֶʮΤϯίʔσΟ ϯάͷৄࡉΛΧϓηϧԽʯͷମ ݱऀ 14

Slide 15

Slide 15 text

KeyedDecodingContainer 15

Slide 16

Slide 16 text

UnkeyedDecodingContainer 16

Slide 17

Slide 17 text

SingleValueDecodingContainer 17

Slide 18

Slide 18 text

͋ͱɺܕͱͯ͠͸ Keyed... Unkeyed... ͕ͩɺ໾ׂͱͯ͠ Nested Containers ͱ͍͏ͷ΋͋Δ 18

Slide 19

Slide 19 text

֤Containerͷ ࢖͍ಓ 19

Slide 20

Slide 20 text

struct Hoge: Decodable { struct Fuga: Codable { let id: Int } let fuga: Fuga } ͋ΔJSONσʔλΛ struct Hoge ! ( Obejct Hoge -> Object Fuga -> Int id ) ΁ͱdecode͢Δࡍͷɺ ɾinputͱͯ͠ظ଴͞ΕΔJSON ɾinitͷίʔυ Λ͓ݟͤ͠·͢ɻ 20

Slide 21

Slide 21 text

JSON: Object -> Object -> Value { "fuga": { "id": 1 } } 21

Slide 22

Slide 22 text

{ "fuga": { "id": 1 } } !KeyedDecodingContainer Ͱdecode͢Δ init(from decoder: Decoder) throws { let container = try decoder .container(keyedBy: CodingKeys.self) fuga = try container.decode(Fuga.self, forKey: .fuga) } 22

Slide 23

Slide 23 text

JSON: Array -> Object -> Value [ { "id": 1 } ] 23

Slide 24

Slide 24 text

[ { "id": 1 } ] !UnkeyedDecodingContainer Ͱdecode͢Δ init(from decoder: Decoder) throws { var container = try decoder.unkeyedContainer() fuga = try container.decode(Fuga.self) } UnkeyedDecodingContainer ͸ [1,2] ! CGPoint(x: 1, y: 2) ͳͲʹ΋࢖ΘΕΔ 24

Slide 25

Slide 25 text

JSON: Object -> Value { "fuga": 1 } 25

Slide 26

Slide 26 text

{ "fuga": 1 } !SingleDecodingContainer Ͱdecode͢Δ init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) fuga = try container.decode(Fuga.self, forKey: .fuga) } // !͸ KeyedDecodingContainer ͱಉ͡Ͱɺ // "͕ͬͪ͜มΘΔ struct Fuga: Codable { let id: Int init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() id = try container.decode(Int.self) } } 26

Slide 27

Slide 27 text

JSON: Object -> Object -> Object -> Value { "hoge": { "fuga": { "id": 1 } } } 27

Slide 28

Slide 28 text

{ "hoge": { "fuga": { "id": 1 } } } !Nested Container (ࠓճ͸Keyed) Ͱdecode͢Δ ※ྫͱͯ͠ॻ͍͍ͯΔ͚ΕͲɺHogeͷσίʔυͷͨΊʹ౉͢JSON͕͜ͷߏ଄ͳͷ͸ྑ͘ͳ͍ͱࢥ͏ private enum HogeCodingKey: CodingKey { case hoge } init(from decoder: Decoder) throws { let container = try decoder .container(keyedBy: HogeCodingKey.self) .nestedContainer(keyedBy: CodingKeys.self, forKey: .hoge) fuga = try container.decode(Fuga.self, forKey: .fuga) } 28

Slide 29

Slide 29 text

ͳ͓ɺܧঝؔ܎ͷදݱʹ΋ Nested Container Λ࢖͏΂͖ͩͦ͏Ͱ͢ superDecoder ͸ɺNested Container( "super" ͱ͍͏ΩʔܾΊଧͪ) ͷDecoderɻ { "bestFriend": {}, "super": { "legCount": 88 } } 29

Slide 30

Slide 30 text

Ͳ͏΍ͬͯ࢖͍ಓͷద੾͞Λ೺Ѳ ͔ͨ͠ 4 WWDCͷηογϣϯΛؤுͬͯฉ͘ 4 ඪ४తͳॻ͖ํ͸ https://github.com/ apple/swift ͷιʔείʔυΛݕࡧ͢Ε͹೺ ѲͰ͖Δ 4 େମͷܕ͸Codableʹద߹͍ͯ͠ΔͷͰ 30

Slide 31

Slide 31 text

ͨͱ͑͹ URL ͷιʔε https://github.com/apple/swift/blob/c5ff1f2cac8da6a14330f4b033b94c7c926d2126/ stdlib/public/SDK/Foundation/URL.swift#L1210-L1236 extension URL : Codable { private enum CodingKeys : Int, CodingKey { case base case relative } public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) let relative = try container.decode(String.self, forKey: .relative) let base = try container.decodeIfPresent(URL.self, forKey: .base) guard let url = URL(string: relative, relativeTo: base) else { throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Invalid URL string.")) } self = url } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(self.relativeString, forKey: .relative) if let base = self.baseURL { try container.encode(base, forKey: .base) } } } 31

Slide 32

Slide 32 text

decodingͱvalidation ͷ౷߹ 32

Slide 33

Slide 33 text

Codable Philosophy 4 Error handling built-in 4 ૊Έࠐ·ΕͨΤϥʔ੍ޚ 4 Encapsulate encoding details 4 ΤϯίʔσΟϯάͷৄࡉΛΧϓηϧԽ 4 Abstract format from type 4 ந৅Խ͞ΕͨϑΥʔϚοτ 33

Slide 34

Slide 34 text

Error handling built-in SwiftͷܕγεςϜͰ͸දݱ͖͠Εͳ͍ υϝΠϯݻ༗ͷόϦσʔγϣϯΛ ΠχγϟϥΠζ࣌ʹ࣮ߦՄೳ 34

Slide 35

Slide 35 text

σίʔυͷϑΣʔζ 1. σίʔυ 1. όΠτσʔλ 2. ߏ଄Λ࣋ͭόΠτσʔλ (JSON౳) 3. ܕ෇͚͞Εͨσʔλ(Swift ͷܕ) 2. όϦσʔγϣϯ 1. Domain-specific validation 2. Graph-level validation 35

Slide 36

Slide 36 text

36

Slide 37

Slide 37 text

Domain-specific validation΋ initʹؚΊΒΕΔ 37

Slide 38

Slide 38 text

·ͱΊ: init(from:) ͷॻ͖ํ ɾContainerΛద੾ʹ࢖͍෼͚Δ ɾదٓDomain-specific validation΋ؚΊΔ ɾॻ͖ํʹ໎ͬͨΒެࣜϦϙδτϦΛݟΔ 38