I've Got Those NSKeyedUnarchiver Blues!
I've Got Those NSKeyedUnarchiver Blues!
- Subject: I've Got Those NSKeyedUnarchiver Blues!
- From: Stuart Rogers <email@hidden>
- Date: Sun, 04 Dec 2011 19:17:19 +0000
I'm having a problem decoding a keyed archive, and I would appreciate
it if some kind soul here can swing the relevant clue bat...
My workspace contains three OS X projects:
* A static library: 'Splits Image Lib'. This contains, inter alia,
a class called SplitsTestSet, which conforms to NSCoding.
* A document-based app: 'Splits Test Set Creator'. This creates a
SplitsTestSet object as its model, and saves it to a file using
NSKeyedArchiver via the document's -dataOfType:error:.
* A document-based app: 'Splits Trainer'. This imports the files
written by Splits Test Set Creator (but not via the document's
-readFromData:ofType:error:).
The Creator app writes and reads its documents correctly - I can
write the data to file, restart the app, and read the data file back
in without problem.
The Trainer app is where things fall over. It reads the file into
an NSData object and tries to decode it with NSKeyedUnarchiver's
-unarchiveWithData: but this results in:
*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (SplitsTestSet)
I really don't understand why this is happening. I'm convinced it's
not a problem with library linking, as these apps have no problem
instantiating various objects sourced from the library. (Almost all
my Googling about this exception seem to be down to bad linking.)
For the record, both apps have 'Splits Image Lib' listed in 'Target
Dependencies' and its '.a' file in 'Link Binary with Libraries'.
So what am I doing wrong...? I suspect it's something subtly trivial.
And now for some code. The Splits Test Set Creator reads and writes
using the following code (with error handling removed for clarity):
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:self.testSet];
return archive;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
SplitsTestSet* ts = [NSKeyedUnarchiver unarchiveObjectWithData:data];
self.testSet = ts;
return YES;
}
The Splits Trainer reads it after prompting the user for its data file:
- (void)chooseExamples:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
// ... openPanel's configuration snipped...
void (^handler)(NSInteger result) = ^(NSInteger result)
{
if (result == NSFileHandlingPanelOKButton)
{
NSURL *theUrl = [[openPanel URLs] objectAtIndex:0];
[openPanel orderOut:nil];
NSData *filecontents = [NSData dataWithContentsOfURL:theUrl];
SplitsTestSet *testSet = [NSKeyedUnarchiver unarchiveObjectWithData:filecontents];
self.examples = testSet;
}
};
[openPanel beginSheetModalForWindow:[(NSPathControl*)sender window] completionHandler:handler];
}
These apps are written on and for OS X 10.6...
System: XCode 4.0.2 on Mac OS X 10.6.7 Base SDK 10.6
Stuart
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden