Skip to content

Instantly share code, notes, and snippets.

@manas-sharma-1683
Created February 18, 2021 20:54
Show Gist options
  • Save manas-sharma-1683/2f188f416b6cfcf04bde88e328366516 to your computer and use it in GitHub Desktop.
Save manas-sharma-1683/2f188f416b6cfcf04bde88e328366516 to your computer and use it in GitHub Desktop.
UIImage+Codable.swift
extension UIImage: Codable {
// works
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(pngData()!)
}
// does not compile, "required' initializer must be declared directly in class 'UIImage' (not in an extension)"
public required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.init(data: try container.decode(Data.self))!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment