Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Advanced Codable
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
hmhm
April 23, 2018
31
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Advanced Codable
Codableの使い方
hmhm
April 23, 2018
More Decks by hmhm
See All by hmhm
EnumのRawValueで好きな型を使いたい
hmhmsh
0
86
Featured
See All Featured
30 Presentation Tips
portentint
PRO
1
430
Speed Design
sergeychernyshev
33
2.2k
New Earth Scene 8
popppiees
4
2.7k
How to make the Groovebox
asonas
2
2.5k
Navigating Weather and Climate Data
rabernat
0
560
Believing is Seeing
oripsolob
1
240
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
320
Side Projects
sachag
456
43k
A designer walks into a library…
pauljervisheath
211
25k
sira's awesome portfolio website redesign presentation
elsirapls
0
460
Transcript
Advanced Codable 2018.02.15 1
Codableとは Codableとは public typealias Codable = Decodable & Encodable ▸
型エイリアス ▸ 右辺を左辺の名前として定義する 2
Codableとは Codableとは ▸ Encodable ▸ エンコード:データを⼀定の規則に基づいて変換 ▸ Decodable ▸ デコード:エンコードしたデータをもとに戻す
3
struct User: Codable { let id: Int let name: String
let address: String } let data = """ { "id":0, "name": "name", "address": "address" } """.data(using: .utf8)! 4 準備 Codableとは
do { let user = try JSONDecoder().decode(User.self, from: data) }
catch { } Decodable(JSON -> 構造体) Codableとは 5
do { let user = try JSONDecoder().decode(User.self, from: data) let
data = try JSONEncoder().encode(user) let json = String(data: data, encoding: .utf8)! } catch { } Encodable(構造体 -> JSON) Codableとは 6
Codableを使って 保存 7
do { let user = try JSONDecoder().decode(User.self, from: data) let
data = try JSONEncoder().encode(user) } catch { } Data型が基本 8 Codableを使って保存
保存 Codableを使って保存 9 func save(key: String) { do { let
data = try JSONEncoder().encode(self) UserDefaults.standard.set(data, forKey: key) } catch let error { print(error) } }
読み込み 10 Codableを使って保存 static func load(key: String) -> Self? {
guard let data = UserDefaults.standard.data(forKey: key) else { return nil } do { return try JSONDecoder().decode(Self.self, from: data) } catch let error { print(error) return nil } }
Protocolを作成 protocol Saveable: Codable { func save(key: String) static func
load(key: String) -> Self? } 11 Codableを使って保存
Protocolを作成 extension Saveable { func save(key: String) { do {
let data = try JSONEncoder().encode(self) UserDefaults.standard.set(data, forKey: key) } catch let error { print(error) } } static func load(key: String) -> Self? { guard let data = UserDefaults.standard.data(forKey: key) else { return nil } do { return try JSONDecoder().decode(Self.self, from: data) } catch let error { print(error) return nil } } } 12 Codableを使って保存
Protocolを適⽤ struct User: Saveable { let id: Int let name:
String let address: String } let key = "user" let user = User(id: 0, name: "name", address: "address") user.save(key: key) let user_ = User.load(key: key) 13 Codableを使って保存
Codableを使って 辞書型を構造体へ 14
struct User{ let id: Int let name: String let address:
String } let dictionary: [String: Any] = ["id": 0, "name": "name", "address": "address"] 構造体 <-> 辞書型 Codableを使って辞書型を構造体へ 15
辞書型 -> 構造体 16 static func decode(dictionary:[String: Any]) -> Self?
{ do { let data = try JSONSerialization.data(withJSONObject: dictionary, options: []) return try JSONDecoder().decode(Self.self, from: data) } catch let error { print(error) return nil } } Codableを使って辞書型を構造体へ
構造体 -> 辞書型 17 func encode() -> [String: Any]? {
do { let data = try JSONEncoder().encode(self) return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] } catch let error { print(error) return nil } } Codableを使って辞書型を構造体へ
protocol Dictionaryable: Codable { func encode() -> [String: Any]? static
func decode(dictionary:[String: Any]) -> Self? } Protocolを作成 18 Codableを使って辞書型を構造体へ
extension Dictionaryable { func encode() -> [String: Any]? { do
{ let data = try JSONEncoder().encode(self) return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] } catch let error { print(error) return nil } } static func decode(dictionary:[String: Any]) -> Self? { do { let data = try JSONSerialization.data(withJSONObject: dictionary, options: []) return try JSONDecoder().decode(Self.self, from: data) } catch let error { print(error) return nil } } } Protocolを作成 19 Codableを使って辞書型を構造体へ
struct User: Dictionaryable { let id: Int let name: String
let address: String } let dictionary: [String: Any] = ["id": 0, "name": "name", "address": "address"] let user = User.decode(dictionary: dictionary) let user_ = User(id: 0, name: "name", address: "address") let dictionary_ = user_.encode() Protocolを適⽤ 20 Codableを使って辞書型を構造体へ
Codableを使って みて 21
感想 ▸ 簡単にJSON <-> 構造体 <-> 辞書の変換が可能 ▸ 構造体のプロパティの型を意識しなくて良い ▸
idはInt型、nameはString型だが変換時に特に意識してい ない ▸ KeyChainにtokenを保存するときにも使⽤可能 22 Codableを使ってみて
Thank you for listening 23
おまけ 24
struct User: Codable { let id: Int let name: String
let address: String } let data = """ { "user_id":0, "user_name": "name", "mail-address": "address" } """.data(using: .utf8)! keyが違う・表せない 25 おまけ
struct User: Codable { let id: Int let name: String
let address: String enum CodingKeys: String, CodingKey { case id = "user_id" case name = "user_name" case address = "mail-address" } } CodingKey機能 26 おまけ
struct User: Codable { let id: Int } let data
= "{"user_id":0}".data(using: .utf8)! do { let user = try JSONDecoder().decode(User.self, from: data) } catch let error { print(error) // keyNotFound } Error 27 おまけ
struct User: Codable { let id: Int? } let data
= "{"user_id":0}".data(using: .utf8)! do { let user = try JSONDecoder().decode(User.self, from: data) } catch let error { print(error) } Error 28 おまけ
struct User: Codable { let id: Int } let data
= "{"id":"id"}".data(using: .utf8)! do { let user = try JSONDecoder().decode(User.self, from: data) } catch let error { print(error) // typeMismatch } Error 29 おまけ