john doe
john doe

Reputation: 9660

NSCoding Not Working in Swift

I am trying to use NSKeyedUnarchiver class to unarchive my custom object and keep getting the error.

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (HelloAppleWatch.Note)'

The code for the Note class is shown below:

import UIKit

class Note: NSObject,NSCoding {

    var title :String?

    override init() {}

    required init(coder aDecoder: NSCoder)  {

        self.title = aDecoder.decodeObjectForKey("title") as String?

    }

    func encodeWithCoder(aCoder: NSCoder) {

        aCoder.encodeObject(self.title, forKey: "title")
    }

}

The code for Unarchiving is shown below:

 let savedData = NSData(contentsOfURL: newURL)
            let note = NSKeyedUnarchiver.unarchiveObjectWithData(savedData!) as Note?

UPDATE:

 func createNote() {

        let note = Note()
        note.title = self.noteTextField?.text

        // archive the note object

       let fileCoordinator = NSFileCoordinator(filePresenter: self)

        fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in

            let saveData = NSKeyedArchiver.archivedDataWithRootObject(note)
            let success = saveData.writeToURL(newURL, atomically: true)

        }


    }

The strange part is that it does not even go to the implementation of the Note.swift class when the unarchive decodeObjectForKey is triggered.

Upvotes: 1

Views: 385

Answers (1)

AdamM
AdamM

Reputation: 4440

Your NSCoding code looks alright, however your archiving method looks different from mine. Try using this code sample to archive your data. Below is an code sample I use which stores an mutablearray of a custom class I have created in swift

 func saveCustomData(data : NSMutableArray)
    {
        var filemgr : NSFileManager
        var docsDir : String
        var dirPaths : NSArray

        filemgr = NSFileManager.defaultManager()

        dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
        docsDir = dirPaths[0] as NSString
        var dataFilePath = docsDir.stringByAppendingPathComponent("data.archive")

        NSKeyedArchiver.archiveRootObject(data, toFile: dataFilePath)
    }

Then when you wish to unarchive it, use this

func loadCustomData() -> NSMutableArray
    {
        var filemgr : NSFileManager
        var docsDir : String
        var dirPaths : NSArray

        filemgr = NSFileManager.defaultManager()
        dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
        docsDir = dirPaths[0] as NSString

        var dataFilePath = docsDir.stringByAppendingPathComponent("data.archive")
        if(filemgr.fileExistsAtPath(dataFilePath))
        {
            var journeyData = NSKeyedUnarchiver.unarchiveObjectWithFile(dataFilePath) as NSMutableArray
            return journeyData
        }
        else
        {
            var emptyArray = NSMutableArray()
            return NSMutableArray()
        }
    }

Hope that helps, if you have any questions, feel free to ask

Upvotes: 1

Related Questions