根据 Vadian 的回答,如何 NSKeyedUnarchiver.unarchiveObject将已弃用的 NSKeyedUnarchiver.unarchiveObject 转换为在 Swift 5/iOS 12+ 中工作
我设法将存档部分转换为工作,但我仍然无法修复未存档部分,这是我的代码:
override func viewDidDisappear(_ animated: Bool)
{
super.viewDidDisappear(animated)
guard let packs = MGLOfflineStorage.shared.packs else { return }
for pack in packs
{
do
{
let userInfo = try? NSKeyedUnarchiver.unarchiveObject(ofClasses: [NSObject.self], from: pack.context) as? [String: String]
print("Suspending download of offline pack: “\(userInfo["name"] ?? "unknown")”")
}
catch
{
print("viewDidDisappear: KeyedUnarchiver Error")
}
pack.suspend()
}
}
这给了我一个错误:
无法使用类型为“(ofClasses:[NSObject.Type],来自:Data)”的参数列表调用“unarchiveObject”
这是原始的 iOS 12- 代码:
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// When leaving this view controller, suspend offline downloads.
guard let packs = MGLOfflineStorage.shared.packs else { return }
for pack in packs {
if let userInfo = NSKeyedUnarchiver.unarchiveObject(with: pack.context) as? [String: String] {
print("Suspending download of offline pack: “\(userInfo["name"] ?? "unknown")”")
}
pack.suspend()
}
}