I have the following struct…
struct Photo: Codable {
let hasShadow: Bool
let image: UIImage?
enum CodingKeys: String, CodingKey {
case `self`, hasShadow, image
}
init(hasShadow: Bool, image: UIImage?) {
self.hasShadow = hasShadow
self.image = image
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
hasShadow = try container.decode(Bool.self, forKey: .hasShadow)
// This fails
image = try container.decode(UIImage?.self, forKey: .image)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(hasShadow, forKey: .hasShadow)
// This also fails
try container.encode(image, forKey: .image)
}
}
Encoding a Photo
fails with …
Optional does not conform to Encodable because UIImage does not conform to Encodable
Decoding fails with…
Key not found when expecting non-optional type Optional for coding key \"image\""))
Is there a way to encode Swift objects that include NSObject
subclass properties that conform to NSCoding
(UIImage
, UIColor
, etc)?
Remember, Swift's String , Int , and Bool are all Codable ! Earlier I wrote that your structs, enums, and classes can conform to Codable . Swift can generate the code needed to extract data to populate a struct's properties from JSON data as long as all properties conform to Codable .
The NSCoding protocol declares the two methods that a class must implement so that instances of that class can be encoded and decoded. This capability provides the basis for archiving (where objects and other structures are stored on disk) and distribution (where objects are copied to different address spaces).
Codable means they can be decoded from and encoded into another representation. Decodable means it can be decoded, but not encoded.
Codable allows you to insert an additional clarifying stage into the process of decoding data into a Swift object. This stage is the “parsed object,” whose properties and keys match up directly to the data, but whose types have been decoded into Swift objects.
Thanks to @vadian pointing me in the direction of encoding/decoding Data
…
class Photo: Codable {
let hasShadow: Bool
let image: UIImage?
enum CodingKeys: String, CodingKey {
case hasShadow, imageData
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
hasShadow = try container.decode(Bool.self, forKey: .hasShadow)
if let imageData = try container.decodeIfPresent(Data.self, forKey: .imageData) {
image = NSKeyedUnarchiver.unarchiveObject(with: imageData) as? UIImage
} else {
image = nil
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(hasShadow, forKey: .hasShadow)
if let image = image {
let imageData = NSKeyedArchiver.archivedData(withRootObject: image)
try container.encode(imageData, forKey: .imageData)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With