macos: use securecoding for codablebridge to prevent warning

Thanks Matt Polzin!
This commit is contained in:
Mitchell Hashimoto
2023-12-23 18:49:21 -08:00
parent a8568306c9
commit c3b89fa215

View File

@ -8,10 +8,7 @@ class CodableBridge<Wrapped: Codable>: NSObject, NSSecureCoding {
static var supportsSecureCoding: Bool { return true }
required init?(coder aDecoder: NSCoder) {
// TODO: This outputs a warning with deprecation on decode. I don't know how to
// fix that yet but there must be something we can change with the encode/decode here
// to resolve it.
guard let data = aDecoder.decodeData() else { return nil }
guard let data = aDecoder.decodeObject(of: NSData.self, forKey: "data") as? Data else { return nil }
guard let archiver = try? NSKeyedUnarchiver(forReadingFrom: data) else { return nil }
guard let value = archiver.decodeDecodable(Wrapped.self, forKey: "value") else { return nil }
self.value = value
@ -20,6 +17,6 @@ class CodableBridge<Wrapped: Codable>: NSObject, NSSecureCoding {
func encode(with aCoder: NSCoder) {
let archiver = NSKeyedArchiver(requiringSecureCoding: true)
try? archiver.encodeEncodable(value, forKey: "value")
aCoder.encode(archiver.encodedData)
aCoder.encode(archiver.encodedData, forKey: "data")
}
}