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 as public
so they'd be visible outside of the framework
- Add
import DemoSharedCode
to any classes that want to use DemoNote
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…