I'm starting to rewrite an application and I want to use Swift 4 Codable protocol to automatically convert json string to Objects and Structs.
Sometimes, specially at the beginning of coding, I encountered decoding problems, so I want to print these errors (whithout using always the debugger), in case some beans are not decoded correctly.
The problem is this:
As you can see, in the debugger, on "decodingError" object there is both:
My problem is that the only properties of that element, in the code, are errorDescription, failureReason, etc, that are ALL nil.
How can I print the values that are correctly displayed in the debugger?
DecodingError
is an enum. In your case you have to catch
the typeMismatch
case and print the type
and the context
.
catch let DecodingError.typeMismatch(type, context) {
print("Type '\(type)' mismatch:", context.debugDescription)
print("codingPath:", context.codingPath)
}
catch let error as DecodingError {
switch error {
case .typeMismatch(let key, let value):
print("error \(key), value \(value) and ERROR: \(error.localizedDescription)")
case .valueNotFound(let key, let value):
print("error \(key), value \(value) and ERROR: \(error.localizedDescription)")
case .keyNotFound(let key, let value):
print("error \(key), value \(value) and ERROR: \(error.localizedDescription)")
case .dataCorrupted(let key):
print("error \(key), and ERROR: \(error.localizedDescription)")
default:
print("ERROR: \(error.localizedDescription)")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With