Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
値オブジェクトのCodable対応
Search
maguhiro
September 06, 2019
Programming
4
1.3k
値オブジェクトのCodable対応
iOSDC Japan 2019のルーキーズLT枠で発表した資料です。
maguhiro
September 06, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
AWSのLambdaで PHPを動かす選択肢
rinchoku
2
390
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
200
AWS re:Invent 2024個人的まとめ
satoshi256kbyte
0
100
良いユニットテストを書こう
mototakatsu
11
3.6k
Androidアプリの One Experience リリース
nein37
0
1.2k
Jaspr Dart Web Framework 박제창 @Devfest 2024
itsmedreamwalker
0
150
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.4k
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
400
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
770
ドメインイベント増えすぎ問題
h0r15h0
2
560
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
940
Amazon Nova Reelの可能性
hideg
0
200
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
32
6.4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
240
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
230
52k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
For a Future-Friendly Web
brad_frost
176
9.5k
How to Ace a Technical Interview
jacobian
276
23k
Code Review Best Practice
trishagee
65
17k
Raft: Consensus for Rubyists
vanstee
137
6.7k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Transcript
J04%$!NHVIJSP ΦϒδΣΫτͷ $PEBCMFରԠ
ࣗݾհ ‣ ࠇതོ!NBHVIJSP ‣ גࣜձࣾΧΧΫίϜ ‣ ৯ϩά৯ϩά৽نࣄۀ ‣ J04ΞϓϦΤϯδχΞ
ࠓ͢͜ͱ 4XJGUͰಋೖ͞Εͨ$PEBCMFΛར༻ͯ͠ ΦϒδΣΫτΛؚΉܕʹ+40/σίʔυ ͨ͠ࡍͷରԠํ๏Λ͓͠·͢
αϯϓϧίʔυ
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
ࣄྫ Ϣʔβʔ*%Λ୯७ͳ*OUܕͰఆٛͯ͠͠· ͏ͱɺҾʹϢʔβʔ*%Λؔ͢Ͱ ޡͬͯಉ͡*OUܕͰ͋ΔଞͷΛͯ͠͠ ·͏ڪΕ͕͋Γ·͢
class UserRepositoryImpl { func find(_ userID: Int) -> User? {
// Կ͔͠Β͔ΒσʔλΛऔಘͯ͠ฦ٫ return user } } let userID = 1234 let photoID = 1000 let user = UserRepositoryImpl().find(photoID) // ↑ޡͬͯࣸਅIDΛҾʹͯ͠͠·͍ͬͯΔ
ͭΒ͍
ͲͪΒಉ͡ܕͳͷͰϏϧυ ΤϥʔɾϥϯλΠϜΤϥʔʹ ͳΒͳ͍
Ͳ͏ͳΕ خ͍͠ʁ
ϏϧυΤϥʔʹͳͬͯ͘Ε Δͱخ͍͠Ͱ͢ΑͶʁ
ͭ·Γ
Ϣʔβʔ*%Λද͢ܕ͕*OUܕͰ ͳ͘ɺϢʔβʔ*%Ͱ͋Δࣄ Λࣔ͢ಠࣗͷܕͰఆ͍ٛͨ͠
ΦϒδΣΫτΛ ಋೖ͠Α͏ʂ
ΦϒδΣΫτͱʁ υϝΠϯۦಈઃܭ %%% ͷઓज़తઃܭͷ ཁૉͷҰͭɻಛͱͯ͠ҎԼͷΑ͏ͳ ͷ͕͋Δɻ ‣ ෆมͰ͋Δ ‣ ಉ࢜Ͱൺֱ͕Մೳ
‣ ަՄೳ
ઌఔͷίʔυʹ ద༻ͯ͠ΈΑ͏
struct UserID: Codable { let value: Int init(value: Int) {
self.value = value } } struct User: Codable { let id: UserID let name: String let age: Int }
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'
ޡͬͨར༻ʹ͙͢ ؾ͚ͮΔͧʂ
Ͱɺαϯϓϧͷ +40/σίʔυ ͯ͠ΈΑ͏ʂ
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))
Τϥʔ͕ൃੜ
ΦϒδΣΫτͱͯ͠6TFS*% ܕΛఆٛͨ͠ࣄͰɺσʔλߏ ͷ֊͕ਂ͘ͳͬͯ͠·ͬ ͨͨΊ
Ͳ͏͢Εʁ
TJOHMF7BMVF$POUBJOFSΛ͓͏ %FDPEFS &ODPEFSϓϩτίϧͰఆٛ͞ Ε͍ͯΔؔ ΦϑΟγϟϧυΩϡϝϯτʹҎԼͷΑ ͏ʹهࡌ͞Ε͍ͯΔɻ l୯ҰͷϓϦϛςΟϒΛอ࣋͢Δͷʹద ͨ͠σίʔυɾΤϯίʔυίϯςφz
ͬͯΈΑ͏ʂ
ΦϒδΣΫτΛ ද͢ϓϩτίϧͷ ࡞
protocol ValueObject: Codable, CustomStringConvertible, Equatable { associatedtype Value: Codable, CustomStringConvertible,
Equatable var value: Value { get } init(value: Value) }
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 } }
6TFS*%ܕద༻
struct UserID: ValueObject { let value: Int init(value: Int) {
self.value = value } }
let json = """ { "id" : 1234, "name" :
"maguhiro", "age" : 34 } """.data(using: .utf8)! let user = try! JSONDecoder() .decode(User.self, from: json) print(“\(user.id)”) // 1234
·ͱΊ ‣ ୯ҰͷΛΤϯίʔυɾσίʔυ͢Δ ߹ʹɺTJOHMF7BMVF$POUBJOFSΛ ͍·͠ΐ͏ʂ ‣ ΦϒδΣΫτͷΤϯίʔυɾσίʔ υΛ࣮ݱ͠ޡͬͨར༻Λ͝͏ʂ
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ IUUQTHJUIVCDPNNBHVIJSP$PEBCMF4VQQPSU'PS7BMVF0CKFDU