NSKeyedUnarchiver and "root" key
NSKeyedUnarchiver and "root" key
- Subject: NSKeyedUnarchiver and "root" key
- From: Manfred Lippert <email@hidden>
- Date: Wed, 31 Dec 2003 03:00:12 +0100
Hi,
I tried to subclass NSKeyedUnarchiver because I want to add some
instance variables, e.g. a "document version".
There is no chance to extend the class by overriding
"unarchiveObjectWithData" because this method is a class method (there
are no instance variables at this point). So I tried it with
"initForReadingWithData". I wrote a subclass of NSKeyedUnarchiver, e.g.
named MyUnarchiver, and added some method
"initForReadingWith
Data:myExtensions:..." where I can add some
parameters (e.g. document version) that are then stored as instance
variables in my MyArchiver object.
Then I replaced my previous decoding code:
myObject = [[NSKeyedUnarchiver unarchiveObjectWith
Data:data] retain];
with this new version:
MyUnarchiver *unarchiver = [[MyUnarchiver alloc]
initForReadingWith
Data:data myExtensions:...];
myObject = [[unarchiver decodeObject] retain];
[unarchiver release];
I did this a while ago with a normal NSArchiver (not keyed) and there
this works. But not this time with the keyed archiver. Every time I got
back a <null> object. After debugging a while, I found out that the
NSKeyedArchiver (that generates the archive) encodes the root object as
keyed object with a key named "root"! So I have to write this:
MyUnarchiver *unarchiver = [[MyUnarchiver alloc]
initForReadingWith
Data:data myExtensions:...];
myObject = [[unarchiver decodeObjectForKey:@"root"] retain];
[unarchiver release];
This works like a charm. :-)
BUT: Is it somewhere documented, that the root object of keyed archiver
data has a key named "root"?!? Can I be sure Apple does not change it
sometime? I searched the documentation and could not find an answer to
this. I had to write a subclass of NSKeyedArchiver and override
"encodeObject:ForKey:" and print out the key names to find out that the
root key is named "root" ... not a very obvious. ;-) So now I want to
get sure that this is specified somewhere to be also true for future
Cocoa versions.
Or is there some better way to add some parameters (e.g. document
version) to NSKeyedUnarchiver?
The objects that implement the NSCoding protocol only gets references
to the NSCoder subclass (normally the used NSKeyedUnarchiver or own
subclass of it), so in my solution they can ask the NSCoder for the
parameters. Is this bad design? What's a better way?
Thanks,
Mani
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.