Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

ios - Added a custom framework, now Swift can't unarchive data

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...