2

I would like to create a Decoder that can deserialize some types from the data. I would like it to be able to automatically deserialize all the types, that have init?(data: Data) initializer using this init method.

protocol DataInitializable: Decodable {
    init?(data: Data)
}
extension DataInitializable {
    // ???
}
extension UIImage: DataInitializable {}
let decoder = DataDecoder()
let image = try? decoder.decode(UIImage.self, from: data)

I have tried various things, but I can't find a proper way to implement this kind of decoder. Also I'm not sure how to implement Decodable protocol in DataInitializable extension so that it works using init(data:).

I know that I could use wrapper class for the image, or something similar, but I would like to have this kind of decoder instead.

Damian Dudycz
  • 2,622
  • 19
  • 38

0 Answers0