gpt4 book ai didi

ios - 无法在 Swift3 中检索正确保存的 NSKeyArchived 对象

转载 作者:行者123 更新时间:2023-11-28 12:27:37 26 4
gpt4 key购买 nike

我无法检索在 Swift 3 中保存为 NSkeyed 存档的对象,我正在摸索。该对象已成功保存为 plist,但在重新加载时返回为 nil。

这是我使用的代码:

要将类本身保存为对象相当容易:

import Foundation

class ItemList:NSObject, NSCoding {

var name: String = "" //Name of the Item list
var contents: [Int] = [] //Ints referencing the CoreData PackItems

init (listname:String, ContentItems:[Int]) {
self.name=listname
self.contents=ContentItems
}

//MARK: NSCoding
public convenience required init?(coder aDecoder: NSCoder) {
let thename = aDecoder.decodeObject(forKey: "name") as! String
let thecontents = aDecoder.decodeObject(forKey: "contents") as! [Int]
self.init(listname: thename,ContentItems: thecontents)
}

func encode(with aCoder: NSCoder) {
aCoder.encode(self.name,forKey:"name")
aCoder.encode(self.contents, forKey: "contents")
}

}

加载和保存对象的代码:

class FileHandler: NSObject {

class func getDocumentsDirectory() -> URL {
let filemgr = FileManager.default
let urls = filemgr.urls(for: .documentDirectory, in: .userDomainMask)
let result:URL = urls.first!
return result
}

///This returns the contents of the handed file inside the Documents directory as the object it was saved as.
class func getFileAsObject(filename:String) -> AnyObject? {

let path = getDocumentsDirectory().appendingPathComponent(filename)

if let result = NSKeyedUnarchiver.unarchiveObject(withFile: path.absoluteString) {
//Success
print("Loaded file '"+filename+"' from storage")
return result as AnyObject?
} else {
print("Error: Couldn't find requested object '"+filename+"' in storage at "+path.absoluteString)
return nil
}
}

///This saves the handed object under the given filename in the App's Documents directory.
class func saveObjectAsFile(filename:String, Object:AnyObject) {
let data = NSKeyedArchiver.archivedData(withRootObject: Object)
let fullPath = getDocumentsDirectory().appendingPathComponent(filename)

do {
try data.write(to: fullPath)
print("Wrote file '"+filename+"' to storage at "+fullPath.absoluteString)
} catch {
print("Error: Couldn't write file '"+filename+"' to storage")
}
}

}

...最后,这就是我所做的一切:

let testobject:ItemList = ItemList.init(listname: "testlist", ContentItems: [0,0,1,2])

FileHandler.saveObjectAsFile(filename:"Test.plist",Object:testobject)
let tobi = FileHandler.getFileAsObject(filename:"Test.plist") as! ItemList

唉,我得到这个作为输出:

Wrote file 'Test.plist' to storage at file:///…/data/Containers/Data/Application/6747B038-B0F7-4B77-85A8-9EA02BC574FE/Documents/Test.plist
Error: Couldn't find requested object 'Test.plist' in storage at file:///…/data/Containers/Data/Application/6747B038-B0F7-4B77-85A8-9EA02BC574FE/Documents/Test.plist

请注意,这是我自己的输出——所以我确实(并已检查)该文件已正确创建。但它只是不会加载。谁能告诉我我做错了什么?

最佳答案

问题在于您传递给 unarchiveObject(withFile:) 的路径。

改变:

if let result = NSKeyedUnarchiver.unarchiveObject(withFile: path.absoluteString) {

到:

if let result = NSKeyedUnarchiver.unarchiveObject(withFile: path.path) {

附带说明一下,您应该将对称 API 用于您的写入和读取逻辑。当您写入数据时,您将根对象存档到 Data 对象,然后将 Data 对象写入文件。但是在读取时,你直接解压给定文件路径的对象树。

要么更改您的编写代码以使用archiveRootObject(_:toFile:),要么更改读取代码以从文件加载Data,然后取消归档数据。您当前的代码有效(一旦您解决了路径问题),但它不一致。

关于ios - 无法在 Swift3 中检索正确保存的 NSKeyArchived 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42913260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com