brandonscript
brandonscript

Reputation: 72885

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?

Upvotes: 4

Views: 3644

Answers (2)

Yun CHEN
Yun CHEN

Reputation: 6648

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 {}
    }

Upvotes: 7

brandonscript
brandonscript

Reputation: 72885

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.

Upvotes: 3

Related Questions