请帮助我!我陷入了困境,找不到出路。我正在尝试学习IOS编程工作,所以我想我将从他们的教程应用“ Meal list”应用开始。我现在应该开始保存持久性数据,现在编辑器使我陷入了永无休止的循环。我有一行代码...
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(meals, toFile: Meal.ArchiveURL.path)
这给了我一个警告,上面写着...
'archiveRootObject(_:toFile :)'在iOS 12.0中已被弃用:使用 + archivedDataWithRootObject:requiringSecureCoding:error:代替
好,所以我将代码行更改为...
let isSuccessfulSave = NSKeyedArchiver.archivedDataWithRootObject(meals)
那然后给我警告...
'archivedDataWithRootObject'已重命名为 'archivedData(withRootObject:)'
好,所以我将代码行更改为...
let isSuccessfulSave = NSKeyedArchiver.archivedData(withRootObject: meals)
哪个告诉我...
'archivedData(withRootObject :)'在iOS 12.0中已被弃用:使用 + archivedDataWithRootObject:requiringSecureCoding:error:代替
好的...所以...不建议使用archivedData,而我必须使用archivedDataWithRootObject,但是使用archivedDataWithRootObject已重命名为archivedData,但是不建议使用archivedData,因此请使用archivedDataWithRootObject,将其重命名为archivedData,不建议使用... ad infinitum
我尝试查看开发人员文档,但他们只是告诉我同样的事情,一个已被弃用,没有链接或任何内容,搜索google只是给了我一堆页面,向我展示了使用其中任何一个的语法。我仍然对IOS编程真的很陌生,不知道如何摆脱这种无休止的循环,从重命名到重命名到...
请帮助,我迷路了,不确定如何继续。谢谢。
答案 0 :(得分:5)
我正在关注the same example you are trying to do,并且想出了如何更新iOS 12中存储和检索值的方法,这应该对您有帮助:
//MARK: Private Methods
private func saveMeals() {
let fullPath = getDocumentsDirectory().appendingPathComponent("meals")
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: meals, requiringSecureCoding: false)
try data.write(to: fullPath)
os_log("Meals successfully saved.", log: OSLog.default, type: .debug)
} catch {
os_log("Failed to save meals...", log: OSLog.default, type: .error)
}
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
private func loadMeals() -> [Meal]? {
let fullPath = getDocumentsDirectory().appendingPathComponent("meals")
if let nsData = NSData(contentsOf: fullPath) {
do {
let data = Data(referencing:nsData)
if let loadedMeals = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? Array<Meal> {
return loadedMeals
}
} catch {
print("Couldn't read file.")
return nil
}
}
return nil
}
您还将发现您需要这样更新ViewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
// Use the edit button item provided by the table view controller.
navigationItem.leftBarButtonItem = editButtonItem
let savedMeals = loadMeals()
if savedMeals?.count ?? 0 > 0 {
meals = savedMeals ?? [Meal]()
} else {
loadSampleMeals()
}
}
我希望这会有所帮助,对我来说,该应用现在可以运行,存储和检索数据。
答案 1 :(得分:1)
iOS 12的一般解决方案是:
class SettingsArchiver {
static func setData(_ value: Any, key: String) {
let ud = UserDefaults.standard
let archivedPool = try? NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true)
ud.set(archivedPool, forKey: key)
}
static func getData<T>(key: String) -> T? {
let ud = UserDefaults.standard
if let val = ud.value(forKey: key) as? Data,
let obj = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(val) as? T {
return obj
}
return nil
}
}
答案 2 :(得分:0)
我想说,直接解决您问题的答案是使用在Meal.swift数据模型(认为是MVC模式)中定义的ArchiveURL,并使用建议的替代方法在MealTableViewController.swift控制器中重新实现saveMeals()
函数通过以下方式移至已弃用的archiveRootObject
方法:
private func saveMeals(){
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: meals, requiringSecureCoding: true)
try data.write(to: Meal.ArchiveURL)
}
catch {
print("Couldn't save to file")
}
}
尽管这个答案有点晚了:-)我希望它能对遇到这个问题的人有所帮助。
答案 3 :(得分:-1)
您需要
try {
let data = try NSKeyedArchiver.archivedData(withRootObject:meals,requiringSecureCoding:true)
try data.write(to:fullPath)
}
catch {
print(error)
}
在Docs中,这是IOS 11 +