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
65
Xcode 15, Swift 5.9で変わる開発体験
satotakeshi
3
2.8k
Meet passkeys
satotakeshi
2
320
What's new in Vision
satotakeshi
0
1.4k
Swift Concurrency入門
satotakeshi
10
4.8k
複数端末のつらさを乗り越えてiOS UITestを実行
satotakeshi
1
360
Xcodegenを個人アプリに導入
satotakeshi
3
710
SwiftUIで作る開閉式メニュー
satotakeshi
2
2.9k
swift-snapshot-testingでVisual Testingを効率化
satotakeshi
0
1.1k
Other Decks in Programming
See All in Programming
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
410
Security_for_introducing_eBPF
kentatada
0
100
命名をリントする
chiroruxx
1
350
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
760
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
4
860
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
190
fs2-io を試してたらバグを見つけて直した話
chencmd
0
120
バグを見つけた?それAppleに直してもらおう!
uetyo
0
120
PipeCDの歩き方
kuro_kurorrr
4
150
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
180
Cursorでアプリケーションの追加開発や保守をどこまでできるか試したら得るものが多かった話
drumnistnakano
0
310
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
A better future with KSS
kneath
238
17k
A Philosophy of Restraint
colly
203
16k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Building Adaptive Systems
keathley
38
2.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Why Our Code Smells
bkeepers
PRO
335
57k
Building an army of robots
kneath
302
44k
Adopting Sorbet at Scale
ufuk
73
9.1k
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