Upgrade to Pro — share decks privately, control downloads, hide ads and more …

サーバー側の仕様変更にCodableで対応する

Sato Takeshi
February 23, 2018

 サーバー側の仕様変更にCodableで対応する

ROPPONGI.swift 第1回
https://visits.connpass.com/event/78975/
で発表した内容です。

Sato Takeshi

February 23, 2018
Tweet

More Decks by Sato Takeshi

Other Decks in Programming

Transcript

  1. 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
  2. Τϯίʔυ 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
  3. 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
  4. Τϯίʔυ 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
  5. 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
  6. 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
  7. encodeIfPresent͸nilͳΒΩʔ͝ͱ ࡟আ { "device": { "model_name": "iPhoneX", "uuid": null }

    } encode͸஋͕nullʹͳΔ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 27
  8. 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
  9. Codableʹ४ڌͤ͞Δ enum ModelName: String, Codable { case iPhoneX case iPhone8

    case iPhone8Plus = "iPhone8 Plus" case iPadPro = "iPad Pro" } αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 31
  10. Τϯίʔυ 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
  11. ݁Ռ { "device_token": "aaaa", "model_name": "iPad Pro", "uuid": "AAAA" }

    αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 33
  12. ॳظ஋ͱ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
  13. Τϯίʔυ 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
  14. ·ͱΊ • JSONߏ଄͕มΘͬͨͱ͖ • ܕΛ࡞Δ͔ϚχϡΞϧΤϯίʔυΛ͢Δ • σʔλ͕nilͳΒjsonͷΩʔΛ֎͢ • ϚχϡΞϧΤϯίʔυͰencodeIfPresentΛ࢖͏ •

    Codableʹ४ڌ͠ͳ͍ܕΛ͍࣋ͬͯΔ࣌ • Τϯίʔυ͢ΔͳΒ४ڌ͢Δ • Τϯίʔυ͠ͳ͍ͳΒCodingKeysͰྻڍΛ֎͢ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 38