Search code examples
iosswiftswift3nskeyedarchivernskeyedunarchiver

Cannot retrieve correctly saved NSKeyArchived Object in Swift3


I cannot retrieve an object saved as a NSkeyed archive in Swift 3, and am scracthcing my head. The object is successfully saved as a plist, but returned as nil when loading back in.

Here is the code I use:

The class itself to be saved as an object is fairly easy:

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")
    }

}

The code to load and save the object:

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")
        }
    }

}

...and finally, this is what I do to call it all up:

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

Alas, I get this as output:

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

Note that this is my own output -- so I do (and have checked) that the file was created correctly. But it just won't load. Can anyone tell me what I am doing wrong?


Solution

  • The problem is with the path you pass to unarchiveObject(withFile:).

    Change:

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

    to:

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

    On a side note, you should use symmetric APIs for your writing and reading logic. When you write the data you archive a root object to a Data object and then write the Data object to a file. But when reading, you directly unarchive the object tree given a file path.

    Either change your writing code to use archiveRootObject(_:toFile:) or change the reading code to load the Data from a file and then unarchive the data. Your current code works (once you fix the path issue) but it's not consistent.