Re: Moving to Keyed Archiving
Re: Moving to Keyed Archiving
- Subject: Re: Moving to Keyed Archiving
- From: Justin Anderson <email@hidden>
- Date: Tue, 26 Aug 2003 11:58:36 -0400
There's one idea provided by Apple in the Cocoa Docs at
<
http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/
Tasks/convertingclasses.html>.
What you're looking for is down towards the bottom of the page, right
before the sample code. The gist of it is that you should test for
-containsValueForKey: (available in both NSKeyedUnarchiver and
NSCoder). Apple's sample code on there is for NSCoder's -initWithCoder,
but here's basically what you'd need to do for your
-loadDataRepresentation: ofType:
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
NSKeyedUnarchiver *keyedUnarchiver = [[[NSKeyedUnarchiver alloc]
initForReadingWith
Data:data] autorelease];
NSUnarchiver *unarchiver = [[[NSUnarchiver alloc]
initForReadingWith
Data:data] autorelease];
id correctUnarchiver = ([keyedUnarchiver
containsValueForKey:@"UsesKeyedCoding"]) ?
keyedUnarchiver : archiver;
[someObject release];
someObject = [[correctUnarchiver decodeObject] retain];
return YES;
}
There are better ways of writing that method, and that doesn't actually
check if the computer you're on is capable of key value coding in the
first place. Apple's docs recommend making an extra key such as
@"UsesKeyedCoding" to check for this sort of thing, but I don't see why
you can't just check for any old key if you already know what the data
is.
And unless you're going to provide the option to save in the old
format, you can turn -dataRepresentationOfType: into a purely keyed
archiver.
Justin Anderson
On Tuesday, August 26, 2003, at 09:57 AM, Drew McCormack wrote:
>
I would like to move my code over to keyed archiving, but can't seem to
>
find a good way to upgrade the NSDocument subclass I have such that it
>
is backwards compatible. In particular, I can't think of a good way to
>
know whether the data being loaded is in unkeyed or keyed format.
>
>
I basically have this:
>
>
- (NSData *)dataRepresentationOfType:(NSString *)aType {
>
NSMutableData *data = [NSMutableData data];
>
NSArchiver *archiver = [[[NSArchiver alloc]
>
initForWritingWithMutableData:data] autorelease];
>
[archiver encodeObject:someObject];
>
return data;
>
}
>
>
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
>
{
>
NSUnarchiver *unarchiver = [[[NSUnarchiver alloc]
>
initForReadingWithData:data] autorelease];
>
[someObject release];
>
someObject = [[unarchiver decodeObject] retain];
>
return YES;
>
}
>
>
How do I update this to keyed archiving, while maintaining backward
>
compatibility? In particular, how do I identify what data is being fed
>
in?
>
>
Drew
>
>
----------------------------------
>
Dr. Drew McCormack
>
Trade Strategist (www.trade-strategist.com)
>
Stock Market strategy design platform for Mac OS X.
>
_______________________________________________
>
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.
_______________________________________________
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.