NSKeyedUnarchiver cannot decode an object of class NSKnownKeysDictionary1

I get the following exception when trying to unzip when using Swift:

Application termination due to the uncaught exception "NSInvalidUnarchiveOperationException", reason: "*** - [NSKeyedUnarchiver decodeObjectForKey:]: cannot decode the class object (NSKnownKeysDictionary1) for the key (NS.objects); the class can be defined in the source code or library, which is not connected "

Context: I am creating the "Share Links" extension. In my main application (written in Objective-C), I write out an array of dictionaries with link information using MMWormhole.

NSFetchRequest* bookmarkFetch = [NSFetchRequest fetchRequestWithEntityName:@"XX"]; bookmarkFetch.propertiesToFetch = @[ @"name", @"text", @"date", @"url" ]; bookmarkFetch.resultType = NSDictionaryResultType; NSArray* bookmarks = [moc executeFetchRequest:bookmarkFetch error:NULL]; [wormhole passMessageObject:bookmarks identifier:@"XXX"]; 

Values โ€‹โ€‹in the array: NSStrings and NSDate.

In the bowels of MMWormhole you get:

  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:messageObject]; 

messageObject is just an array of bookmarks without intermediate processing.

In extension I:

  let wormhole = MMWormhole(applicationGroupIdentifier: "group.XX", optionalDirectory:nil) let bookmarks = wormhole.messageWithIdentifier("XXX") as? Array<Dictionary<String,AnyObject>> 

messageWithIdentifier: ultimately calls this:

  id messageObject = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 

The matrix is โ€‹โ€‹correctly written to the application group folder - I can read it using another extension written in Objective C.

This exception appears when I run the simulator. When working on a 32-bit device (iPhone 5 and iPad 3), the code works correctly. I currently do not have a 64-bit device for testing.

I suppose I'm missing an import or framework, but which one?

+5
source share
2 answers

I asked about this on the Apple Developer Forums and (this time) got a good answer from Apple Developer Relations. I will give important bits:

NSKnownKeysDictionary1 is a voodoo Core Data that I donโ€™t understand .... Obviously, something is wrong with its serialization and deserialization. Do you have basic data at both ends of the wormhole? Regardless, maybe it makes sense to make a deep copy of your bookmark array (and everything else that you get from Core Data), so that you send standard dictionaries by "wiring" and not Core Data.

So my solution is to add a basic data structure to the extension or make a deep copy. (I temporarily made the first. The right decision, probably the last.)

+2
source

This is just a note:

You can set class names for NSKeyedArchiver and NSKeyedUnarchiver . I had this problem without dealing with CoreData. Unarchiver no longer found my own class.

Setting className for my class works as shown below:

For archiver :

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

And unarchiver :

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

Source: https://habr.com/ru/post/1233240/


All Articles