Mat*_*att 7 xcode nskeyedunarchiver swift swift3
我有以下代码用于在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
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,自从我在Xcode 8.0中升级到Swift 3后,我有以下错误消息:
'unarchiveTopLevelObjectWithData' is unavailable in Swift: Use 'unarchiveTopLevelObjectWithData(_:) throws' instead这几乎是一样的,对吧?所以我对如何解决这个问题感到非常困惑.这是Xcode中的错误吗?
Rob*_*Rob 10
在NSKeyedUnarchiver仍然期待NSData:
let array = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data as NSData) as! NSArray
Run Code Online (Sandbox Code Playgroud)
在Swift 4中已经解决了这个问题
| 归档时间: |
|
| 查看次数: |
3175 次 |
| 最近记录: |