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
DemoNote
and its vars and methods aspublic
so they'd be visible outside of the framework - Add
import DemoSharedCode
to 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.
Moving DemoNote
from the app to a framework did change the module name, which meant that NSKeyedUnarchiver
couldn't find instances of the archived class due to a name mismatch. The fix was to add this line before unarchiving:
NSKeyedUnarchiver.setClass(DemoNote.self, forClassName: "DemoNotesSwift.DemoNote")
In this case, DemoNote.self
gets the current full class name, and DemoNotesSwift.DemoNote
is what the class used to be called when it was part of the app.
This was only necessary because I had previously existing data that I wanted to keep.
来源:https://stackoverflow.com/questions/28569468/added-a-custom-framework-now-swift-cant-unarchive-data