• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSKeyedUnarchiver and "root" key
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 "initForReadingWithData: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 unarchiveObjectWithData:data] retain];

with this new version:

MyUnarchiver *unarchiver = [[MyUnarchiver alloc] initForReadingWithData: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] initForReadingWithData: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.

  • Prev by Date: Re: writeToFile: causes app to crash when save on quit.
  • Next by Date: Re: People who develop with MySQL and/or other GPL based code
  • Previous by thread: Re: writeToFile: causes app to crash when save on quit.
  • Next by thread: Display text suggestions
  • Index(es):
    • Date
    • Thread