Swift.DecodingError.typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array<Any> but found a dictionary instead."

Hello,

Can anybody help me with the following error?

Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil)): file JSonParcingTest.playground, line 22

this is the line of code that I am testing in Xcode swiftUI Playgroung

import UIKit

struct ProductRequest: Codable {

let dateRequested: String

let comments: String

}

let url = URL(string: "https://api.globaldooh.com/products/getproducts/f69c6f4d-0aff-45c5-8ccd-660b022f3557/90/gucci/11/2")!

URLSession.shared.dataTask(with: url) { data, _ , _ in

if let data = data {

let productrequest = try ! JSONDecoder().decode([ProductRequest]. self , from: data)

print(productrequest)

}

}.resume()

LOL. I just needed to post this to realize my own mistake .

This
let productrequest = try! JSONDecoder().decode([ProductRequest].self , from: data)
Should be like this.

let productrequest = try ! JSONDecoder().decode(ProductRequest.self , from: data)

no needed

I just needed to post this to realize my own mistake .

FYI, this is rubber duck debugging, and it works more often than you’d think (-:

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like

@mdf can you please edit the title to something short?

Terms of Service

Privacy Policy

Cookie Policy