I have a Codable
struct myObj
:
public struct VIO: Codable {
let id:Int?;
...
var par1:Bool = false; //default to avoid error in parsing
var par2:Bool = false;
}
When I do receive JSON, I don't have par1
and par2
since these variables are optional. During parsing I get an error:keyNotFound(CodingKeys(stringValue: \"par1\", intValue: nil)
How to solve this?
If you have local variables you have to specify the
CodingKeys
Edit:
If
par1
andpar2
should be also decoded optionally you have to write a custom initializerThis is Swift: No trailing semicolons