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
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
CSC509 Lecture 11
javiergs
PRO
0
180
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Better Code Design in PHP
afilina
PRO
0
120
C++でシェーダを書く
fadis
6
4.1k
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
It's Worth the Effort
3n
183
27k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
GraphQLとの向き合い方2022年版
quramy
43
13k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Why Our Code Smells
bkeepers
PRO
334
57k
Bash Introduction
62gerente
608
210k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
How to train your dragon (web standard)
notwaldorf
88
5.7k
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