NSKeyedUnarchiver cannot decode object of class NSKnownKeysDictionary1 NSKeyedUnarchiver cannot decode object of class NSKnownKeysDictionary1 swift swift

NSKeyedUnarchiver cannot decode object of class NSKnownKeysDictionary1


This is just a side note:

You can set class names for both the NSKeyedArchiver & NSKeyedUnarchiver.I had this problem without dealing with CoreData at all. The unarchiver did not find my own class anymore.

Setting the className for my class works as shown below:

For the archiver:

NSKeyedArchiver.setClassName("MyClass", for: MyClass.self)let data = NSKeyedArchiver.archivedData(withRootObject: root)

And the unarchiver:

NSKeyedUnarchiver.setClass(MyClass.self, forClassName: "MyClass")let root = NSKeyedUnarchiver.unarchiveObject(with: data)


I asked this on the Apple Developer forums and (for once) got a good answer from Apple Developer Relations. I'll quote the important bits:

NSKnownKeysDictionary1 is a Core Data voodoo that I do not understand. ... Clearly something is going wrong with its serialisation and deserialisation. Do you have Core Data up and running on both ends of the wormhole? Regardless, it might make more sense to do a deep copy of your bookmarks array (and anything else you get back from Core Data) so that you’re sending standard dictionaries across the ‘wire’ rather than Core Data stuff.

So my solution is either to add the Core Data framework to the extension or to do the deep copy. (I've temporarily done the former. The proper solution is probably the latter.)