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?

brandonscript
  • 68,675
  • 32
  • 163
  • 220

2 Answers2

7

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 {}
    }
Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
3

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.

brandonscript
  • 68,675
  • 32
  • 163
  • 220