Re: Migrating to keyed archiving...?
Re: Migrating to keyed archiving...?
- Subject: Re: Migrating to keyed archiving...?
- From: Drew McCormack <email@hidden>
- Date: Fri, 14 Nov 2003 11:22:40 +0100
On Nov 13, 2003, at 9:05 PM, Ben Kennedy wrote:
Hi folks,
I feel like a bit of a dunce here, but I have a fundamental
implementation problem. Thanks to Panther's release, it's now time to
bring my app up to a minimum requirement of Jaguar. :) Accordingly,
I'm
migrating all of my objects' archiving to use the keyed archiving
mechanism.
The thing I can't figure out is how to alter my code such that it will
properly un-archive both old-school and new-school archive formats.
NSUnarchiver chokes on a file written out with NSKeyedArchiver
(understandably), and NSKeyedUnarchiver balks on old-school files
written
out with NSUnarchiver. How do I handle pre-existing old-style files?
This is the hack that I'm now using, which seems to work:
ZDBSong *song;
NSData *d = [NSData dataWithContentsOfFile:filename];
if(!bcmp([d bytes], "\004\013typedstream", 13)) // old-school or
new-
school?
song = [NSUnarchiver unarchiveObjectWithData:d]; // oldschool
else song = [NSKeyedArchiver unarchiveObjectWithData:d]; //
newschool
[song retain];
This is ugly though. There must be a saner way. What am I missing?
(And please don't tell me I should be declaring a new file type, new
file
extension, etc. for the new-school format...)
-ben
Hi Ben,
Join the club. I had this very problem. In the end, what I did was try
to read the archive with plain NSUnarchiver first. If this failed,
returning nil for an object, for example, I then tried the
NSKeyedUnarchiver. This was the best solution I could find.
When I tried the keyed archiver first, I got a low level exception that
I couldn't recover from whenever an old archive was read. That is why I
now try reading with the un-keyed unarchiver first.
Hope this helps,
Drew
_______________________________________________
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.