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

Cannot retrieve correctly saved NSKeyArchived Object in Swift3

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

这是我使用的代码:

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

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 对象写入文件。但是在读取的时候,直接给定一个文件路径解压object tree。

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