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
密集、ドキュメントのコロケーション with AWS Lambda
satoshi256kbyte
0
190
Spring gRPC について / About Spring gRPC
mackey0225
0
220
ソフトウェアエンジニアの成長
masuda220
PRO
10
1.3k
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
910
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
350
もう僕は OpenAPI を書きたくない
sgash708
5
1.8k
Djangoアプリケーション 運用のリアル 〜問題発生から可視化、最適化への道〜 #pyconshizu
kashewnuts
1
250
Bedrock Agentsレスポンス解析によるAgentのOps
licux
3
840
一休.com のログイン体験を支える技術 〜Web Components x Vue.js 活用事例と最適化について〜
atsumim
0
510
AIの力でお手軽Chrome拡張機能作り
taiseiue
0
170
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
130
お前もAI鬼にならないか?👹Bolt & Cursor & Supabase & Vercelで人間をやめるぞ、ジョジョー!👺
taishiyade
6
4k
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
137
6.8k
We Have a Design System, Now What?
morganepeng
51
7.4k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
Rebuilding a faster, lazier Slack
samanthasiow
80
8.8k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
630
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Faster Mobile Websites
deanohume
306
31k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
9
440
Building Flexible Design Systems
yeseniaperezcruz
328
38k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
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