NSKeyedUnarchiver 不适用于 Swift 3

NSKeyedUnarchiver not working with Swift 3

我有以下代码用于在我的 Mac 应用程序中取消存档文件:

func tryOpen(_ filePath: String) throws -> NSArray {
    if #available(OSX 10.11, *) {
        do {
            if let data = try? Data(contentsOf: URL(fileURLWithPath: filePath)) {
                let array = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as! NSArray
                return array
            } else {
                throw NSError(domain: NSExceptionName.invalidUnarchiveOperationException.rawValue, code: 0, userInfo: nil)
            }
        } catch let ex {
            throw ex
        }
    } else {
        // Fallback on earlier versions
        let dat = try? Data(contentsOf: URL(fileURLWithPath: filePath))
        let unarchiver = NSKeyedUnarchiver(forReadingWith: dat!)
        if let array = unarchiver.decodeObject(forKey: "root") as? NSArray {
            return array
        } else {
            throw NSException(name: NSExceptionName.invalidArgumentException, reason: "Unable to unarchive file", userInfo: nil) as! Error
        }
    }
}

但是,自从我在 Xcode 8.0 中升级到 Swift 3 后,出现以下错误消息: 'unarchiveTopLevelObjectWithData' is unavailable in Swift: Use 'unarchiveTopLevelObjectWithData(_:) throws' instead,这几乎是一回事,对吧?所以我对如何解决这个问题感到非常困惑。这是 Xcode 中的错误吗?

NSKeyedUnarchiver 仍在等待 NSData:

let array = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data as NSData) as! NSArray

这已在 Swift 4

中得到补救