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

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

53e2d354b3299d64a54af680865516d5?s=47 Sato Takeshi
February 23, 2018

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

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

53e2d354b3299d64a54af680865516d5?s=128

Sato Takeshi

February 23, 2018
Tweet

Transcript

  1. αʔόʔଆͷ࢓༷มߋʹ CodableͰରԠ͢Δ ROPPONGI.swift ୈ1ճ 2018೥2݄23೔ ࠤ౻߶࢜ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 1

  2. Who am I ࠤ౻߶࢜ (@hatakenokakashi) MAMORIOגࣜձࣾͰiOSΤϯδχΞ͠ ͍ͯ·͢ɻ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 2

  3. Swift4͔ΒCodable͕ ௥Ճ͞Ε·ͨ͠ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 3

  4. Codableͱ͸ • σʔλͷγϦΞϥΠζɺσγϦΞϥΠζΛܕ҆શͰѻ͑Δϓ ϩτίϧ • Ҏલ͔Β߹ͬͨNSCoding΍JSONSerializationɺ PropertyListSerializationͱ͸ҟͳΓܕΛͦͷ··ѻ͑Δ = more Swifty

    ! αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 4
  5. Codableͷ͓͞Β͍ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 5

  6. 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
  7. Τϯίʔυ 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
  8. JSON͕࡞੒͞ΕΔ { "device_token": "aaaaaa", "model_name": "iPhoneX", "uuid": "AAAA" } αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ

    8
  9. αʔόʔνʔϜΑΓ࢓༷ มߋͷ͓஌Βͤ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 9

  10. deviceͱ͍͏ΩʔΛτοϓʹ௥Ճ { "device": { "device_token": "aaaaaa", "model_name": "iPhoneX", "uuid": "AAAA"

    } } αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 10
  11. ௚͠·͠ΐ͏ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 11

  12. ରԠํ๏1 ܕΛ৽͘͠࡞Δ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 12

  13. 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
  14. ܕͷϓϩύςΟ ᴹᴹ JSONKey αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 14

  15. Τϯίʔυ 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
  16. ରԠํ๏2 ϚχϡΞϧΤϯίʔυ࣮૷͢Δ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 16

  17. encode(to:)ϝιουΛ࣮૷͢Δ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 17

  18. αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 18

  19. αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 19

  20. αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 20

  21. αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 21

  22. αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 22

  23. σʔλ͕nilͳΒjsonͷΩ ʔΛ֎ͯ͠΄͍͠ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 23

  24. ରԠํ๏ ϚχϡΞϧΤϯίʔυ & encodeIfPresentΛ࢖͏ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 24

  25. 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
  26. 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
  27. encodeIfPresent͸nilͳΒΩʔ͝ͱ ࡟আ { "device": { "model_name": "iPhoneX", "uuid": null }

    } encode͸஋͕nullʹͳΔ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 27
  28. طଘͷܕΛCodableʹର Ԡ͢Δ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 28

  29. 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
  30. ରԠํ๏ Τϯίʔυ͍ͨ࣌͠ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 30

  31. Codableʹ४ڌͤ͞Δ enum ModelName: String, Codable { case iPhoneX case iPhone8

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

    αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 33
  34. ରԠํ๏ Τϯίʔυ͠ͳͯ͘Α͍࣌ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 34

  35. ॳظ஋ͱ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
  36. Τϯίʔυ 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
  37. ݁Ռ { "device_token": "aaaa", "uuid": "AAAA" } αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 37

  38. ·ͱΊ • JSONߏ଄͕มΘͬͨͱ͖ • ܕΛ࡞Δ͔ϚχϡΞϧΤϯίʔυΛ͢Δ • σʔλ͕nilͳΒjsonͷΩʔΛ֎͢ • ϚχϡΞϧΤϯίʔυͰencodeIfPresentΛ࢖͏ •

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

  40. ੵۃతʹϝϯόʔืूதʂ https://www.wantedly.com/companies/mamorio • iOSΤϯδχΞ • AndroidΤϯδχΞ • UI/UXσβΠφʔ αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 40

  41. MAMORIOגࣜձࣾ͸try! SwiftΛεϙϯαʔ υ͍ͯ͠·͢! αʔόʔଆͷ࢓༷มߋʹCodableͰରԠ͢Δ 41