Search code examples
iosswiftxcodenskeyedunarchiver

NSKeyedUnarchiver.unarchiveTopLevelObjectWithData is obsoleted in Swift 4


I tried to implement a fork of AwesomeCache that implements unarchiveTopLevelObjectWithData in Swift 4:

if let data = NSData(contentsOfFile: path) {
    do {
        possibleObject = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data as NSData) as? CacheObject
    }
    catch {}
}

But Xcode is angry at me now, and says:

'unarchiveTopLevelObjectWithData' was obsoleted in Swift 4 (Foundation.NSKeyedUnarchiver)

Mean, imo, because it doesn't tell me what it's been replaced with (if anything?), and the documentation is rather... vacant.

So what do I use instead?


Solution

  • Agree with you, NSData is not Data, an improvement could be:

        if let nsData = NSData(contentsOfFile: path) {
            do {
                let data = Data(referencing:nsData)
                possibleObject = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? CacheObject
            }
            catch {}
        }