I have a large code that I try to update to change deprecated APIs.
In the former version, I used forWritingWith and forReadingWith
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encode(myObject, forKey: theKey)
if let data = NSMutableData(contentsOf: anURL) {
let unarchiver = NSKeyedUnarchiver(forReadingWith: data as Data)
let myObject = unarchiver.decodeObject(forKey: theKey) as! TheObjectType // <<-- returns the object
That I changed to
let data = NSMutableData()
let archiver = NSKeyedArchiver(requiringSecureCoding: true)
archiver.encode(myObject, forKey: theKey)
if let data = NSMutableData(contentsOf: anURL) {
do {
let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data as Data)
let myObject = unarchiver.decodeObject(forKey: theKey) as? TheObjectType // <<-- This returns nil
This builds correctly.
But on execution, unarchiver.decodeObject now returns nil.
I have searched extensively to find the cause to no avail.
I may probably change the design to avoid NSKeyedArchiver, but that would be a huge refactoring.
I probably miss something obvious.
Could someone hint at the possible cause ?