Do you help resolve coding errors? I've Googled and checked the Forum, but can't find an answer.
Coding Error
This is the struct...
struct Blueline: Decodable {
let ctatt: Ctatt?
}
struct Ctatt: Decodable {
let tmst: String?
let errCD: String?
let errNum: String?
let eta: [eta?]
enum CodingKeys: String, CodingKey {
case tmst
case errCD = "errCd"
case errNum
case eta
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.tmst = try container.decode(String.self, forKey: .tmst)
self.errCD = try container.decode(String.self, forKey: .errCD)
self.errNum = try container.decodeIfPresent(String.self, forKey: .errNum)
self.eta = try container.decode(Array.self, forKey: .eta)
}
}
struct eta: Decodable {
let staNm: String?
let stpDe: String?
let rn: String?
let rt: String?
let destSt: String?
let destNm: String?
let prdt: String?
let arrT: String?
let lat: String?
let lon: String?
enum CodingKeys: String, CodingKey {
case staNm
case stpDe
case rn
case rt
case destSt
case destNm
case prdt
case arrT
case lat
case lon
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.staNm = try container.decode(String.self, forKey: .staNm)
self.stpDe = try container.decode(String.self, forKey: .stpDe)
self.rn = try container.decode(String.self, forKey: .rn)
self.rt = try container.decode(String.self, forKey: .rt)
self.destSt = try container.decode(String.self, forKey: .destSt)
self.destNm = try container.decode(String.self, forKey: .destNm)
self.prdt = try container.decode(String.self, forKey: .prdt)
self.arrT = try container.decode(String.self, forKey: .arrT)
self.lat = try container.decode(String.self, forKey: .lat)
self.lon = try container.decode(String.self, forKey: .lon)
}
}
struct Blueline: Decodable {
let ctatt: Ctatt?
}
struct Ctatt: Decodable {
let tmst: String?
let errCD: String?
let errNum: String?
let eta: [eta?]
enum CodingKeys: String, CodingKey {
case tmst
case errCD = "errCd"
case errNum
case eta
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.tmst = try container.decode(String.self, forKey: .tmst)
self.errCD = try container.decode(String.self, forKey: .errCD)
self.errNum = try container.decodeIfPresent(String.self, forKey: .errNum)
self.eta = try container.decode(Array.self, forKey: .eta)
}
}
struct eta: Decodable {
let staNm: String?
let stpDe: String?
let rn: String?
let rt: String?
let destSt: String?
let destNm: String?
let prdt: String?
let arrT: String?
let lat: String?
let lon: String?
enum CodingKeys: String, CodingKey {
case staNm
case stpDe
case rn
case rt
case destSt
case destNm
case prdt
case arrT
case lat
case lon
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.staNm = try container.decode(String.self, forKey: .staNm)
self.stpDe = try container.decode(String.self, forKey: .stpDe)
self.rn = try container.decode(String.self, forKey: .rn)
self.rt = try container.decode(String.self, forKey: .rt)
self.destSt = try container.decode(String.self, forKey: .destSt)
self.destNm = try container.decode(String.self, forKey: .destNm)
self.prdt = try container.decode(String.self, forKey: .prdt)
self.arrT = try container.decode(String.self, forKey: .arrT)
self.lat = try container.decode(String.self, forKey: .lat)
self.lon = try container.decode(String.self, forKey: .lon)
}
}
This is the error...
API status: 200
valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "ctatt", intValue: nil), CodingKeys(stringValue: "eta", intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1), CodingKeys(stringValue: "lat", intValue: nil)], debugDescription: "Expected String value but found null instead.", underlyingError: nil))
API status: 200
valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "ctatt", intValue: nil), CodingKeys(stringValue: "eta", intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1), CodingKeys(stringValue: "lat", intValue: nil)], debugDescription: "Expected String value but found null instead.", underlyingError: nil))
Take a careful look at the error. Specifically, this:
gives you the path to the problem, namely ctatt \> eta \> \[1\] \> lat. And this:
tells you what went wrong. Now look at the code at the end of that path:
The .decode(_:forKey:) method throws if the property is missing. You want the .decodeIfPresent(_:forKey:) variant.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Code Block codingPath: [ CodingKeys(stringValue: "ctatt", intValue: nil), CodingKeys(stringValue: "eta", intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1), CodingKeys(stringValue: "lat", intValue: nil) ]
gives you the path to the problem, namely ctatt \> eta \> \[1\] \> lat. And this:
Code Block "Expected String value but found null instead."
tells you what went wrong. Now look at the code at the end of that path:
Code Block self.lat = try container.decode(String.self, forKey: .lat)
The .decode(_:forKey:) method throws if the property is missing. You want the .decodeIfPresent(_:forKey:) variant.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"