💡 Troubleshoot coding/decoding errors
When working with codables, instead of printing .localizedDescription, try to print out the error itself! so the compiler describes where the issue exactly is!
do {
let decoder = JSONDecoder()
let decoded = try decoder.decode([Root].self, from: data!)
} catch {
// print(error.localizedDescription) // <- ⚠️ Don't use this!
print(String(describing: error)) // <- ✅ Use this for debuging!
}
In your case
it will point out that:
- Decoder tried to decode the root object to an
Arraybut found aDictionaryinstead.
So you follow the issue and see that you should replace:
decoder.decode([Root].self, from: data!)
with:
decoder.decode(Root.self, from: data!)