Skip to content

Instantly share code, notes, and snippets.

@Winchariot
Created December 12, 2017 15:53
Show Gist options
  • Save Winchariot/436972e7552beb4f33edb400cedf2f63 to your computer and use it in GitHub Desktop.
Save Winchariot/436972e7552beb4f33edb400cedf2f63 to your computer and use it in GitHub Desktop.
Codable types
//It is generally nicest to make the variables in your codable types optional, so your decoding can still succeed
// even if the JSON is missing 1 or more fields
struct MyCodableType: Codable {
var myVar1: String?
var myVar2: [Bool]?
var myVar3: SomeOtherCodableType?
//A CodingKeys enum is required if your variable names don't match the JSON names, case-sensitive.
// The cases here should match your clientside variable names, and the associated values should match the JSON
enum CodingKeys: String, CodingKey {
case myVar1 = "JSONThing"
case myVar2 = "OtherJSONThing"
case myVar4 = "YetAnother"
}
}
struct SomeOtherCodableType: Codable {
var bestFood: String?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment