1

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 !

zarghol
  • 478
  • 5
  • 19
  • You could use an `AnyCodableType` wrapper, see for example https://stackoverflow.com/a/47496656/2976878. Although what exactly are you planning on doing with `decoded` after this? You don't know its concrete type. – Hamish Dec 04 '17 at 19:53

0 Answers0