I have a pretty trivial Swift app that has a model class named DemoNote. An array of DemoNote instances is read/written via keyed archiving. This worked fine while DemoNote was included in the app.
But then I moved DemoNote.swift to a new custom framework called DemoSharedCode. Aside from making sure Xcode was using the framework in the app target, I made sure to
- Mark
DemoNoteand its vars and methods aspublicso they'd be visible outside of the framework - Add
import DemoSharedCodeto any classes that want to useDemoNote
So now the compiler is happy. But at run time the unarchiving fails with this error:
2015-02-17 12:12:53.417 DemoNotesSwift[70800:16504104] *** Terminating app due to
uncaught exception 'NSInvalidUnarchiveOperationException', reason:
'*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class
(DemoNotesSwift.DemoNote)'
In the above, DemoNotesSwift is the app name, DemoNote is the class name, and the line of code is attempting to unarchive objects from an NSData blob:
let savedObjects = NSKeyedUnarchiver.unarchiveObjectWithData(savedData) as? [(DemoNote)]
I'm guessing that moving DemoNote to the framework means its module name has changed, which breaks unarchiving, but I'm not sure of that. I'm also not sure what to do about it-- maybe I need to call +setClass:forClassName: on the unarchiver, but if so I don't know what the arguments would be.