Guest User

Untitled

a guest
Dec 14th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. //It is generally nicest to make the variables in your codable types optional, so your decoding can still succeed
  2. // even if the JSON is missing 1 or more fields
  3.  
  4. struct MyCodableType: Codable {
  5. var myVar1: String?
  6. var myVar2: [Bool]?
  7. var myVar3: SomeOtherCodableType?
  8.  
  9. //A CodingKeys enum is required if your variable names don't match the JSON names, case-sensitive.
  10. // The cases here should match your clientside variable names, and the associated values should match the JSON
  11. enum CodingKeys: String, CodingKey {
  12. case myVar1 = "JSONThing"
  13. case myVar2 = "OtherJSONThing"
  14. case myVar4 = "YetAnother"
  15. }
  16. }
  17.  
  18. struct SomeOtherCodableType: Codable {
  19. var bestFood: String?
  20. }
Add Comment
Please, Sign In to add comment