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
C#/.NETのこれまでのふりかえり
tomokusaba
1
160
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
480
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
23k
Realtime API 入門
riofujimon
0
110
外部システム連携先が10を超えるシステムでのアーキテクチャ設計・実装事例
kiwasaki
1
230
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
870
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
210
RailsのPull requestsのレビューの時に私が考えていること
yahonda
5
1.7k
Pinia Colada が実現するスマートな非同期処理
naokihaba
2
160
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
0
160
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
1k
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
140
Featured
See All Featured
For a Future-Friendly Web
brad_frost
175
9.4k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Git: the NoSQL Database
bkeepers
PRO
425
64k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
How to train your dragon (web standard)
notwaldorf
88
5.7k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
504
140k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
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