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

后端 未结 1 1120
Happy的楠姐
Happy的楠姐 2021-01-18 05:29

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 w

1条回答
  •  耶瑟儿~
    2021-01-18 06:07

    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.

    0 讨论(0)
提交回复
热议问题