Created
February 18, 2021 20:54
-
-
Save manas-sharma-1683/2f188f416b6cfcf04bde88e328366516 to your computer and use it in GitHub Desktop.
UIImage+Codable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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