Swift – The data couldn’t be read because it isn’t in the correct format

💡 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 Array but found a Dictionary instead.

So you follow the issue and see that you should replace:

decoder.decode([Root].self, from: data!)

with:

decoder.decode(Root.self, from: data!)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)