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
Jakarta EE meets AI
ivargrimstad
0
230
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
150
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
360
モバイルアプリにおける自動テストの導入戦略
ostk0069
0
110
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
240
42 best practices for Symfony, a decade later
tucksaun
1
180
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
2
460
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
Music & Morning Musume
bryan
46
6.2k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Code Reviewing Like a Champion
maltzj
520
39k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Statistics for Hackers
jakevdp
796
220k
How GitHub (no longer) Works
holman
311
140k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Side Projects
sachag
452
42k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Agile that works and the tools we love
rasmusluckow
328
21k
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