Re: Help with simple EncodeObject & DecodeObject
Re: Help with simple EncodeObject & DecodeObject
- Subject: Re: Help with simple EncodeObject & DecodeObject
- From: Carlos Weber <email@hidden>
- Date: Mon, 27 May 2002 19:44:52 -1000
On Monday, May 27, 2002, at 07:16 , email@hidden wrote:
I admit it, I am having problems with the simplest of Cocoa document
tasks, saving and opening data file.
What is the difference between using decodeObject/encodeObject and the
various other flavors of the call (decodeDataObject, decodeRootObject,
decodeNXObject, etc.)?
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;
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;
When I save, it seems to work, but when I read, I get various errors:
2002-05-28 01:11:48.605 d20 Edit[1278] *** +[NSUnarchiver
unarchiveObjectWithData:]: extra data discarded
2002-05-28 01:11:48.611 d20 Edit[1278] *** -[NSshortNumber
decodeObject]: selector not recognized
2002-05-28 01:11:48.651 d20 Edit[1278] *** -[NSshortNumber
decodeObject]: selector not recognized
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). 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). I just want to store
the internal data (the bytes of number or text).
Any ideas what I am missing?
If you have access to Aaron Hillegass' book, Cocoa Programming for Mac
OS X, he devotes a chapter (Chapter 6: Archiving) to this very topic.
Briefly, NSArchiver's class method, + (NSData
*)archivedDataWithRootObject: (id)rootObject is what you want, as long
as your root object adheres to the <NSCoding> protocol.
_______________________________________________
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.