I'm working on a small struct that handles the parsing of JSON. Up until now it is all working except I am wanting to pass a custom struct as a structure to use on the decode in JSONDecoder().decode(type.self, from: data)
but this is throwing the following error:
Cannot convert value of type 'Codable' (aka 'Decodable & Encodable') to expected argument type 'T.Type'
private func parseJson(data: Data, type: Codable) -> Codable? {
do {
let decoded = try JSONDecoder().decode(type.self, from: data)
return decoded
} catch {
print("JSON decode error: \(error.localizedDescription)")
}
return nil
}
Is there a way that I can pass a struct into this method to use as the type for the decode()
function? If I directly set the type I'm trying to pass into the function on the decode() function the code works as expected, it only errors out when I attempt to pass it in.