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

Ios 无法在Swift3中检索正确保存的NSKeyArchived对象,ios,swift,swift3,nskeyedarchiver,nskeyedunarchiver,Ios,Swift,Swift3,Nskeyedarchiver,Nskeyedunarchiver,我无法在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 C

我无法在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(with file:)
的路径

更改:

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

另一方面,您应该使用对称API来编写和读取逻辑。写入数据时,将根对象归档到
数据
对象,然后将
数据
对象写入文件。但是在读取时,如果给定文件路径,则直接取消归档对象树


将编写代码更改为使用
archiveRootObject(:toFile:)
,或将读取代码更改为从文件加载
数据,然后取消归档数据。当前代码可以正常工作(一旦解决了路径问题),但不一致。

问题在于传递给
unarchiveObject(with file:)
的路径

更改:

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

另一方面,您应该使用对称API来编写和读取逻辑。写入数据时,将根对象归档到
数据
对象,然后将
数据
对象写入文件。但是在读取时,如果给定文件路径,则直接取消归档对象树

将编写代码更改为使用
archiveRootObject(:toFile:)
,或将读取代码更改为从文件加载
数据,然后取消归档数据。您当前的代码可以工作(一旦您解决了路径问题),但并不一致