Upgrade to Pro — share decks privately, control downloads, hide ads and more …

値オブジェクトのCodable対応

Bde9a701cfdb94a6725e68a62921c051?s=47 maguhiro
September 06, 2019

 値オブジェクトのCodable対応

iOSDC Japan 2019のルーキーズLT枠で発表した資料です。

Bde9a701cfdb94a6725e68a62921c051?s=128

maguhiro

September 06, 2019
Tweet

Other Decks in Programming

Transcript

  1. J04%$!NHVIJSP ஋ΦϒδΣΫτͷ $PEBCMFରԠ

  2. ࣗݾ঺հ ‣ ໨ࠇതོ!NBHVIJSP ‣ גࣜձࣾΧΧΫίϜ ‣ ৯΂ϩά৯΂ϩά৽نࣄۀ ‣ J04ΞϓϦΤϯδχΞ

  3. ࠓ೔࿩͢͜ͱ 4XJGUͰಋೖ͞Εͨ$PEBCMFΛར༻ͯ͠ ஋ΦϒδΣΫτΛؚΉܕʹ+40/σίʔυ ͨ͠ࡍͷରԠํ๏Λ͓࿩͠·͢

  4. αϯϓϧίʔυ

  5. struct User: Codable { let id: Int let name: String

    let age: Int } let json = """ { "id" : 1234, "name" : "maguhiro", "age" : 34 } """.data(using: .utf8)! let user = try! JSONDecoder() .decode(User.self, from: json) print("\(user.id)") // 1234
  6. ໰୊ࣄྫ Ϣʔβʔ*%Λ୯७ͳ*OUܕͰఆٛͯ͠͠· ͏ͱɺҾ਺ʹϢʔβʔ*%Λ౉ؔ͢਺Ͱ ޡͬͯಉ͡*OUܕͰ͋Δଞͷ஋Λ౉ͯ͠͠ ·͏ڪΕ͕͋Γ·͢

  7. class UserRepositoryImpl { func find(_ userID: Int) -> User? {

    // Կ͔͠Β͔ΒσʔλΛऔಘͯ͠ฦ٫ return user } } let userID = 1234 let photoID = 1000 let user = UserRepositoryImpl().find(photoID) // ↑ޡͬͯࣸਅIDΛҾ਺ʹ౉ͯ͠͠·͍ͬͯΔ
  8. ͭΒ͍఺

  9. ͲͪΒ΋ಉ͡ܕͳͷͰϏϧυ ΤϥʔɾϥϯλΠϜΤϥʔʹ ͳΒͳ͍

  10. Ͳ͏ͳΕ͹
 خ͍͠ʁ

  11. ϏϧυΤϥʔʹͳͬͯ͘Ε Δͱخ͍͠Ͱ͢ΑͶʁ

  12. ͭ·Γ

  13. Ϣʔβʔ*%Λද͢ܕ͕*OUܕͰ ͸ͳ͘ɺϢʔβʔ*%Ͱ͋Δࣄ Λࣔ͢ಠࣗͷܕͰఆ͍ٛͨ͠

  14. ஋ΦϒδΣΫτΛ ಋೖ͠Α͏ʂ

  15. ஋ΦϒδΣΫτͱ͸ʁ υϝΠϯۦಈઃܭ %%% ͷઓज़తઃܭͷ ཁૉͷҰͭɻಛ௃ͱͯ͠ҎԼͷΑ͏ͳ΋ ͷ͕͋Δɻ ‣ ෆมͰ͋Δ ‣ ஋ಉ࢜Ͱൺֱ͕Մೳ

    ‣ ަ׵Մೳ
  16. ઌఔͷίʔυʹ
 ద༻ͯ͠ΈΑ͏

  17. struct UserID: Codable { let value: Int init(value: Int) {

    self.value = value } } struct User: Codable { let id: UserID let name: String let age: Int }
  18. class UserRepositoryImpl { func find(_ userID: UserID) -> User? {

    // Կ͔͠Β͔ΒσʔλΛऔಘͯ͠ฦ٫ return user } } let userID = UserID(value: 1234) let photoID = 1000 let user = UserRepositoryImpl().find(photoID) // ↑ޡͬͯࣸਅIDΛҾ਺ʹ౉͢ͱɺҎԼͷΑ͏ͳΤϥʔ͕ൃੜ͢Δ // Cannot convert value of type 'Int' to expected argument type 'UserID'
  19. ޡͬͨར༻ʹ͙͢ ؾ͚ͮΔͧʂ

  20. Ͱ͸ɺαϯϓϧͷ +40/σίʔυ ͯ͠ΈΑ͏ʂ

  21. let json = """ { "id" : 1234, "name" :

    "maguhiro", "age" : 34 } """.data(using: .utf8)! let user = try! JSONDecoder() .decode(User.self, from: json) // Swift.DecodingError.typeMismatch(Swift.Dictionary<Sw ift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "id", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a number instead.", underlyingError: nil))
  22. Τϥʔ͕ൃੜ

  23. ஋ΦϒδΣΫτͱͯ͠6TFS*% ܕΛఆٛͨ͠ࣄͰɺσʔλߏ ଄ͷ֊૚͕ਂ͘ͳͬͯ͠·ͬ ͨͨΊ

  24. Ͳ͏͢Ε͹ʁ

  25. TJOHMF7BMVF$POUBJOFSΛ࢖͓͏ %FDPEFS &ODPEFSϓϩτίϧͰఆٛ͞ Ε͍ͯΔؔ਺ ΦϑΟγϟϧυΩϡϝϯτʹ͸ҎԼͷΑ ͏ʹهࡌ͞Ε͍ͯΔɻ l୯ҰͷϓϦϛςΟϒ஋Λอ࣋͢Δͷʹద ͨ͠σίʔυɾΤϯίʔυίϯςφz

  26. ࢖ͬͯΈΑ͏ʂ

  27. ஋ΦϒδΣΫτΛ ද͢ϓϩτίϧͷ ࡞੒

  28. protocol ValueObject: Codable, CustomStringConvertible, Equatable { associatedtype Value: Codable, CustomStringConvertible,

    Equatable var value: Value { get } init(value: Value) }
  29. extension ValueObject { init(from decoder: Decoder) throws { let container

    = try decoder.singleValueContainer() let value = try container.decode(Value.self) self.init(value: value) } func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() try container.encode(value) } var description: String { return value.description } static func == (lhs: Self, rhs: Self) -> Bool { return lhs.value == rhs.value } }
  30. 6TFS*%ܕ΁ద༻

  31. struct UserID: ValueObject { let value: Int init(value: Int) {

    self.value = value } }
  32. let json = """ { "id" : 1234, "name" :

    "maguhiro", "age" : 34 } """.data(using: .utf8)! let user = try! JSONDecoder() .decode(User.self, from: json) print(“\(user.id)”) // 1234
  33. ·ͱΊ ‣ ୯Ұͷ஋ΛΤϯίʔυɾσίʔυ͢Δ ৔߹ʹ͸ɺTJOHMF7BMVF$POUBJOFSΛ࢖ ͍·͠ΐ͏ʂ ‣ ஋ΦϒδΣΫτͷΤϯίʔυɾσίʔ υΛ࣮ݱ͠ޡͬͨར༻Λ๷͝͏ʂ

  34. ͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ IUUQTHJUIVCDPNNBHVIJSP$PEBCMF4VQQPSU'PS7BMVF0CKFDU