Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why my CustomType cannot conform to Decodable even I conformed it?

I have a CustomType that conforms to Decodable, when I want use it as needed value for my function, Xcode complain that CustomType does not conform to Decodable! Should I explicitly make CustomType conformation happen, I also did it, but it did not solved the issue! What I am missing here?

Error:

Type ‘CustomType.Type’ cannot conform to ‘Decodable’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let stringOfJSON: String = """
{ "name": "SwiftPunk", "age": 35 }
"""

let dataOfJSON: Data? = stringOfJSON.data(using: String.Encoding.utf8)

struct CustomType: Decodable {
    
    enum Codingkeys: String, CodingKey {
        case name, age
    }
    
    var name: String
    var age: Int
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: Codingkeys.self)
        name = try container.decode(String.self, forKey: .name)
        age = try container.decode(Int.self, forKey: .age)
    }
    
    
}

func decoderFunction<T: Decodable>(dataOfJSON: Data?, customType: T, decodedValue: (T) -> Void) {
    
    if let unwrappedDataOfJSON: Data = dataOfJSON {
        
        let dataJSONDecoder: JSONDecoder = JSONDecoder()
        
        do {
            let value: T = try dataJSONDecoder.decode(T.self, from: unwrappedDataOfJSON)
            decodedValue(value)
        } catch {
            print("The Data could not be decoded!")
        }
    }
}

use case:

decoderFunction(dataOfJSON: dataOfJSON, customType: CustomType.self, decodedValue: { value in
 
})

>Solution :

The type of the customType parameter is incorrect. It should be the metatype of T (T.Type), not T.

func decoderFunction<T: Decodable>(dataOfJSON: Data?, customType: T.Type, decodedValue: (T) -> Void) {
    ...
}

The method was expecting a value of type T, which should conform to Decodable, but you were giving it a value of type CustomType.Type, which doesn’t conform to Decodable (but note that CustomType does).

Also note that you don’t need the customType parameter at all. T can be inferred if you specify the type in the closure:

func decoderFunction<T: Decodable>(dataOfJSON: Data?, decodedValue: (T) -> Void) {
    ...
}

decoderFunction(dataOfJSON: dataOfJSON, decodedValue: { (value: CustomType) in
 
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading