Created
June 15, 2018 09:04
-
-
Save onmyway133/e3be377721304538ebc4d96de0392c4c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public struct ImageWrapper: Codable { | |
public let image: Image | |
public enum CodingKeys: String, CodingKey { | |
case image | |
} | |
public init(image: Image) { | |
self.image = image | |
} | |
public init(from decoder: Decoder) throws { | |
let container = try decoder.container(keyedBy: CodingKeys.self) | |
let data = try container.decode(Data.self, forKey: CodingKeys.image) | |
guard let image = Image(data: data) else { | |
throw StorageError.decodingFailed | |
} | |
self.image = image | |
} | |
public func encode(to encoder: Encoder) throws { | |
var container = encoder.container(keyedBy: CodingKeys.self) | |
guard let data = image.cache_toData() else { | |
throw StorageError.encodingFailed | |
} | |
try container.encode(data, forKey: CodingKeys.image) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment