Created
December 12, 2017 15:53
-
-
Save Winchariot/436972e7552beb4f33edb400cedf2f63 to your computer and use it in GitHub Desktop.
Codable types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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