Re: Help with simple EncodeObject & DecodeObject
Re: Help with simple EncodeObject & DecodeObject
- Subject: Re: Help with simple EncodeObject & DecodeObject
- From: Marcel Weiher <email@hidden>
- Date: Tue, 28 May 2002 09:02:40 +0200
On Tuesday, May 28, 2002, at 07:16 Uhr, email@hidden wrote:
I have a simple document that contains a number of integers and strings
(stored internally as NSString & NSNumber). To implement
dataRepresentationOfType, I use NSArchiver and encode Object thus:
NSMutableData *a_data = nil;
NSArchiver *a_archiver = [[[NSArchiver alloc]
initForWritingWithMutableData: [NSMutableData data]] autorelease];
if (a_archiver) {
[a_archiver encodeObject:someStr];
[a_archiver encodeObject:someNumber];
a_data = [a_archiver archiverData];
}
return a_data;
You can't "push" individual data items into an archiver like this. You
have to encode a single 'root' object (probably your document) and then
let the (un)archiver call your -encodeWithCoder: and -initWithCoder:
methods.
I would also recommend putting your actual model-code in a class that is
separate from the kit-supplied "Document" class.
-dataRepresentationOfType:
{
return [NSArchiver archivedDataWithRootObject:[self model]];
}
The archiver will, at some point, send the 'model' object the
encodeWithCoder: message, where you can put your specific coding
routines.
-(void)encodeWithCoder:(NSCoder*)aCoder
{
//--- there may need to be a [super encodeWithCoder:aCoder]
here, depending on your inheritance chain
[aCoder encodeObject:someStr];
[aCoder encodeObject:someNumber];
//--- in order to facilitate XML coding, I use MPWFoundation
coding-macros
// encodeVar( aCoder, someStr );
// encodeVar( aCoder, someNumber );
}
To implement loadDataRepresentation, I am using NSUnarchiver,
decodeObject, and some already created Set methods:
if (data) {
NSUnarchiver *a_unarchiver = [NSUnarchiver
unarchiveObjectWithData:data];
if (a_unarchiver) {
NSString* a_str = [a_unarchiver decodeObject];
NSNumber* a_number = [a_unarchiver decodeObject];
if ([a_unarchiver isAtEnd]) {
[self setMyString: a_str]
[self setMyNumber: a_number];
return YES;
}
}
}
return NO
-loadDataRepresentation...
{
[self setModel:[NSUnarchiver unarchiveObjectWith
Data:data]];
}
-initWithCoder:(NSCoder*)aCoder
{
//--- again, there may need to be a self = [super
initWithCoder:aCoder] here
someStr=[[aCoder decodeObject] retain];
someNumber=[[aCoder decodeObject] retain];
//--- not 100% sure about the retains, because I always use
// decodeVar( aCoder, someStr );
// decodeVar( aCoder, someNumber );
return self;
}
What am I doing wrong? Does anyone have source code examples of
saving simple structures. Notice the data is NOT in a collections nor
an array (I do not want to store it that way for a number of internal
reasons).
There is no need to store your variables in a collection. Quite the
contrary, they should be part of a "model" object that is referred to by
the Document object.
I thought the archivers/unarchivers were supposed to store the encoded
information into a data object, to be passed to & from the file IO. I
do not want to store the internal format of the object (ie. information
regarding what type of object it is).
Then archiving is not for you. Archiving takes a snapshot of the
object-graph.
I just want to store the internal data (the bytes of number or text).
Then you may be better off with a property list.
Marcel
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
_______________________________________________
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.