Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 661
brandonscript Avatar asked Oct 18 '25 13:10

brandonscript


2 Answers

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 {}
    }
like image 195
Yun CHEN Avatar answered Oct 21 '25 04:10

Yun CHEN


Oh, silly me.

NSData is not Data

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

...makes Xcode happy.

like image 43
brandonscript Avatar answered Oct 21 '25 05:10

brandonscript