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
Sato Takeshi
February 23, 2018
Programming
3
1.1k
サーバー側の仕様変更にCodableで対応する
ROPPONGI.swift 第1回
https://visits.connpass.com/event/78975/
で発表した内容です。
Sato Takeshi
February 23, 2018
Tweet
Share
More Decks by Sato Takeshi
See All by Sato Takeshi
Swift愛好会 の 思い出
satotakeshi
0
62
Xcode 15, Swift 5.9で変わる開発体験
satotakeshi
3
2.8k
Meet passkeys
satotakeshi
2
320
What's new in Vision
satotakeshi
0
1.3k
Swift Concurrency入門
satotakeshi
10
4.8k
複数端末のつらさを乗り越えてiOS UITestを実行
satotakeshi
1
360
Xcodegenを個人アプリに導入
satotakeshi
3
690
SwiftUIで作る開閉式メニュー
satotakeshi
2
2.9k
swift-snapshot-testingでVisual Testingを効率化
satotakeshi
0
1.1k
Other Decks in Programming
See All in Programming
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
810
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
200
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.3k
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
4
790
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
110
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
4
1.1k
Outline View in SwiftUI
1024jp
1
300
Click-free releases & the making of a CLI app
oheyadam
2
110
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
CSC509 Lecture 09
javiergs
PRO
0
140
Identifying User Idenity
moro
6
9.7k
距離関数を極める! / SESSIONS 2024
gam0022
0
140
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Practical Orchestrator
shlominoach
186
10k
RailsConf 2023
tenderlove
29
900
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
The Language of Interfaces
destraynor
154
24k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
A better future with KSS
kneath
238
17k
Building Adaptive Systems
keathley
38
2.3k
Transcript
αʔόʔଆͷ༷มߋʹ CodableͰରԠ͢Δ ROPPONGI.swift ୈ1ճ 20182݄23 ࠤ౻߶࢜ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 1
Who am I ࠤ౻߶࢜ (@hatakenokakashi) MAMORIOגࣜձࣾͰiOSΤϯδχΞ͠ ͍ͯ·͢ɻ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 2
Swift4͔ΒCodable͕ Ճ͞Ε·ͨ͠ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 3
Codableͱ • σʔλͷγϦΞϥΠζɺσγϦΞϥΠζΛܕ҆શͰѻ͑Δϓ ϩτίϧ • Ҏલ͔Β߹ͬͨNSCodingJSONSerializationɺ PropertyListSerializationͱҟͳΓܕΛͦͷ··ѻ͑Δ = more Swifty
! αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 4
Codableͷ͓͞Β͍ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 5
CodableରԠͷܕΛ࡞Δ struct DeviceRequestData: Codable { var uuid: String? var deviceToken:
String? var modelName: String //JSONͷKeyʹରԠ //ࣗಈΤϯίʔυɺσίʔυʹͤΔͳΒ`CodingKeys`ͱ͍͏໊લʹ͢Δ enum CodingKeys: String, CodingKey { case uuid case deviceToken = "device_token" case modelName = "model_name" } } αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 6
Τϯίʔυ let deviceRequestData = DeviceRequestData(uuid: "AAAA", deviceToken: "aaaaaa", modelName: "iPhoneX")
let jsonData = try JSONEncoder().encode(deviceRequestData) let jsonString = String(data: jsonData, encoding: .utf8)! print(jsonString) αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 7
JSON͕࡞͞ΕΔ { "device_token": "aaaaaa", "model_name": "iPhoneX", "uuid": "AAAA" } αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ
8
αʔόʔνʔϜΑΓ༷ มߋͷ͓Βͤ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 9
deviceͱ͍͏ΩʔΛτοϓʹՃ { "device": { "device_token": "aaaaaa", "model_name": "iPhoneX", "uuid": "AAAA"
} } αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 10
͠·͠ΐ͏ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 11
ରԠํ๏1 ܕΛ৽͘͠࡞Δ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 12
DeviceܕΛՃ struct Device: Codable { var uuid: String? var deviceToken:
String? var modelName: String enum CodingKeys: String, CodingKey { case uuid case deviceToken = "device_token" case modelName = "model_name" } } struct DeviceRequestData: Codable { var device: Device } αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 13
ܕͷϓϩύςΟ ᴹᴹ JSONKey αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 14
Τϯίʔυ let device = Device(uuid: "AAAA", deviceToken: "aaaaaa", modelName: "iPhoneX")
let deviceRequestData = DeviceRequestData(device: device) let jsonData = try JSONEncoder().encode(deviceRequestData) let jsonString = String(data: jsonData, encoding: .utf8)! print(jsonString) αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 15
ରԠํ๏2 ϚχϡΞϧΤϯίʔυ࣮͢Δ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 16
encode(to:)ϝιουΛ࣮͢Δ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 17
αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 18
αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 19
αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 20
αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 21
αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 22
σʔλ͕nilͳΒjsonͷΩ ʔΛ֎ͯ͠΄͍͠ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 23
ରԠํ๏ ϚχϡΞϧΤϯίʔυ & encodeIfPresentΛ͏ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 24
encodeϝιουͰencodeIfPresent ͰΤϯίʔυ func encode(to encoder: Encoder) throws { var container
= encoder.container(keyedBy: RootKeys.self) var nestObject = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .device) try nestObject.encode(uuid, forKey: .uuid) try nestObject.encodeIfPresent(deviceToken, forKey: .deviceToken) try nestObject.encode(modelName, forKey: .modelName) } αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 25
nilͷΛೖΕΔ let deviceRequestData = DeviceRequestData(uuid: nil, deviceToken: nil, modelName: "iPhoneX")
let jsonData = try JSONEncoder().encode(deviceRequestData) let jsonString = String(data: jsonData, encoding: .utf8)! print(jsonString) αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 26
encodeIfPresentnilͳΒΩʔ͝ͱ আ { "device": { "model_name": "iPhoneX", "uuid": null }
} encode͕nullʹͳΔ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 27
طଘͷܕΛCodableʹର Ԡ͢Δ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 28
Codableʹ४ڌ͍ͯ͠ͳ͍ܕΛ࣋ͬͯ ͍Δ struct DeviceRequestData: Codable { var uuid: String? var
deviceToken: String? var modelName: ModelName //Codableʹ४ڌ͍ͯ͠ͳ͍ܕ enum CodingKeys: String, CodingKey { case uuid case deviceToken = "device_token" case modelName = "model_name" } } αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 29
ରԠํ๏ Τϯίʔυ͍ͨ࣌͠ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 30
Codableʹ४ڌͤ͞Δ enum ModelName: String, Codable { case iPhoneX case iPhone8
case iPhone8Plus = "iPhone8 Plus" case iPadPro = "iPad Pro" } αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 31
Τϯίʔυ let deviceRequestData = DeviceRequestData(uuid: "AAAA", deviceToken: "aaaa", modelName: ModelName.iPadPro)
let jsonData = try JSONEncoder().encode(deviceRequestData) let jsonString = String(data: jsonData, encoding: .utf8)! print(jsonString) αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 32
݁Ռ { "device_token": "aaaa", "model_name": "iPad Pro", "uuid": "AAAA" }
αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 33
ରԠํ๏ Τϯίʔυ͠ͳͯ͘Α͍࣌ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 34
ॳظͱCodingKeysʹΩʔΛؚΊͳ ͍ struct DeviceRequestData: Codable { var uuid: String? =
"" var deviceToken: String? = "" var modelName: ModelName = ModelName.iPhoneX enum CodingKeys: String, CodingKey { case uuid case deviceToken = "device_token" } } αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 35
Τϯίʔυ let deviceRequestData = DeviceRequestData(uuid: "AAAA", deviceToken: "aaaa", modelName: ModelName.iPadPro)
let jsonData = try JSONEncoder().encode(deviceRequestData) let jsonString = String(data: jsonData, encoding: .utf8)! print(jsonString) αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 36
݁Ռ { "device_token": "aaaa", "uuid": "AAAA" } αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 37
·ͱΊ • JSONߏ͕มΘͬͨͱ͖ • ܕΛ࡞Δ͔ϚχϡΞϧΤϯίʔυΛ͢Δ • σʔλ͕nilͳΒjsonͷΩʔΛ֎͢ • ϚχϡΞϧΤϯίʔυͰencodeIfPresentΛ͏ •
Codableʹ४ڌ͠ͳ͍ܕΛ͍࣋ͬͯΔ࣌ • Τϯίʔυ͢ΔͳΒ४ڌ͢Δ • Τϯίʔυ͠ͳ͍ͳΒCodingKeysͰྻڍΛ֎͢ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 38
͍͞͝ʹ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 39
ੵۃతʹϝϯόʔืूதʂ https://www.wantedly.com/companies/mamorio • iOSΤϯδχΞ • AndroidΤϯδχΞ • UI/UXσβΠφʔ αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 40
MAMORIOגࣜձࣾtry! SwiftΛεϙϯαʔ υ͍ͯ͠·͢! αʔόʔଆͷ༷มߋʹCodableͰରԠ͢Δ 41