NSKeyedUnarchiver cannot decode object of class NSKnownKeysDictionary1

I'm getting the following exception when trying to unarchive using Swift:

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (NSKnownKeysDictionary1) for key (NS.objects); the class may be defined in source code or a library that is not linked'

The context: I'm creating a "Share Links" extension. In my main app (written in Objective C) I write out an array of dictionaries with the information about the links 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:@"***"];


The values in the array are NSStrings and an NSDate.


In the bowels of MMWormhole you get:


  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:messageObject];


messageObject is just the bookmarks array without any intermediate processing.


In the extension I have:


  let wormhole = MMWormhole(applicationGroupIdentifier: "group.XX", optionalDirectory:nil)

  let bookmarks = wormhole.messageWithIdentifier("***") as? Array<Dictionary<String,AnyObject>>


messageWithIdentifier: ends up calling this ultimately:


  id messageObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];


The array is written out to the app group folder correctly -- I can read it using another extension, one written in Objective C.


This exception appears when I run in the Simulator. The code appears to work correctly when run on a 32-bit device (iPhone 5 and iPad 3). I don't have a 64-bit device to test on currently.


I imagine I'm missing an

import
or a framework, but which one(s)? It's currently linked against Foundation and CoreFoundation.


(Originally asked on Stack Overflow, but with no response.)

Accepted Reply

NSKnownKeysDictionary1 is a Core Data voodoo that I do not understand. Someone over in App Frameworks > Core Data might have more insight into it.

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.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

NSKnownKeysDictionary1 is a Core Data voodoo that I do not understand. Someone over in App Frameworks > Core Data might have more insight into it.

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.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Ah, I never even thought to consider that NSKnownKeysDictionary1 would be in anything other that (Core)Foundation. If I add the Core Data framwork to the extension it Just Works. (I think the "proper'" solution is the deep copy but this proves that you're right.)


Many thanks.