I tried to pass a type in a enum to store it, and decode it when data of this type arrive. I face a problem to pass the type to the decoder.
enum RetrieveInfoPolicy {
case decoding(Decodable.Type)
case no
}
struct Foo: Codable {
let str: String
}
let policy = RetrieveInfoPolicy.decoding(Foo.self)
let decoder = JSONDecoder()
let dataTest = "ldghglhezlkhgzeklh".data(using: .utf8)!
switch policy {
case .no:
break
case .decoding(let decodableType):
do {
let decoded = try decoder.decode(decodableType, from: dataTest)
} catch {
print("error : \(error)")
}
break
}
The error I got was :
Cannot invoke 'decode' with an argument list of type '((Decodable.Type), from: Data)'
Can you help me with this ? Thanks !