This is Problem-unarchiving-Storage-class in view mode; [Up]
Date: Sun 11-Dec-1991 18:43:25 From: pmarcos@netcom.COM (Paul Marcos) Subject: Problem unarchiving Storage class Hi everyone, I'm having trouble archiving a Storage object. I have simplified it down to a simple app that has two fields, inField & outField. I create a storage object to store a single 10-character item using: id myStorage; myStorage = [[Storage alloc] initCount: 1 elementSize: 10 description: "*"]; I can then type something into inField which is type char *, and add it to my storage objectusing: [myStorage replace: inField at: 1]; (I don't have my printout in front of me so syntax may be wrong) Anyway, it works and I can get the contents of the item using: [outField setStringValue: [myStorage elementAt: 1]]; And everything works fine. So now I try to write it off to a typed stream NXTypedStream *myStream; myStream = NXOpenTypedStreamForFile( myFile, NX_WRITEONLY ); NXWriteRootObject( myStream, myStorage ); NXCloseTypedStream( myStream ); I get the file written out and it looks OK to me. When I try to unarchive the object I use: id newStorage; myStream = NXOpenTypedStreamForFile( myFile, NX_READONLY ); newStorage = NXReadObject( myStream ); NXCloseTypedStream( myStream ); I would expect the same code that read the item out of myStorage could read the item out of newStorage but all I get is two characters that are garbage. If I query the object for it's count, elementSize and description they are all OK. So what am I doing wrong. I'd like to keep using the Storage object since it's so simple. Any help? Thanks. Paul Marcos
Date: Sun 12-Dec-1991 17:00:28 From: bedney@monolith.lanl.gov (William J. Edney) Subject: Re: Problem unarchiving Storage class In article <1991Dec11.184325.8063pmarcos@netcom.COM> pmarcos@netcom.COM (Paul Marcos) writes: > > Hi everyone, > > I'm having trouble archiving a Storage object. I have simplified it > down to a simple app that has two fields, inField & outField. I create > a storage object to store a single 10-character item using: > > id myStorage; > myStorage = [[Storage alloc] initCount: 1 elementSize: 10 description: "*"]; > > I can then type something into inField which is type char *, and add it > to my storage objectusing: > > [myStorage replace: inField at: 1]; (I don't have my printout in front > of me so syntax may be wrong) > > Anyway, it works and I can get the contents of the item using: > > [outField setStringValue: [myStorage elementAt: 1]]; > > And everything works fine. So now I try to write it off to a typed stream > using: > > NXTypedStream *myStream; > myStream = NXOpenTypedStreamForFile( myFile, NX_WRITEONLY ); > NXWriteRootObject( myStream, myStorage ); > NXCloseTypedStream( myStream ); > > I get the file written out and it looks OK to me. When I try to unarchive > the object I use: > > id newStorage; > myStream = NXOpenTypedStreamForFile( myFile, NX_READONLY ); > newStorage = NXReadObject( myStream ); > NXCloseTypedStream( myStream ); > > I would expect the same code that read the item out of myStorage could > read the item out of newStorage but all I get is two characters that are > garbage. If I query the object for it's count, elementSize and description > they are all OK. > > So what am I doing wrong. I'd like to keep using the Storage object since > it's so simple. Any help? Thanks. > > Paul Marcos Paul - Welcome to the world of NeXTstep, where even though most of the time the appkit is great, other times it falls flat on its face. The archiving methods in Storage are brain-damaged and will not write data contained therein properly. You must do this manually. I faced this problem and, with a lot of help from Dick Phillips (thanks, Dick!), won! Now, observe closely (I'm actually archiving a struct here, which makes things a bit more complex): Here's some declarations I make in my .h: typedef struct _ListElement { char *descriptor; char *description; } ListElement; #define LISTTYPE ListElement * Here's how I initialize the object in my init: listList = [[Storage alloc] initCount:0 elementSize:sizeof(LISTTYPE) description:@encode(LISTTYPE)]; Here's how I write it: - write:(NXTypedStream *)stream { int i,theCount; LISTTYPE *theElement; [super write:stream]; theCount = [listList count]; NXWriteType(stream,"i",&theCount); for (i=0;i<theCount;i++) { theElement = (LISTTYPE *)[listList elementAt:i]; NXWriteType(stream,"*",&((*theElement)->descriptor)); NXWriteType(stream,"*",&((*theElement)->description)); } return self; } Here's how I read it: - read:(NXTypedStream *)stream { int i,numElements; char *theDescriptor,*theDescription; [super read:stream]; NXReadType(stream,"i",&numElements); listList = [[Storage alloc] initCount:0 elementSize:sizeof(LISTTYPE) description:@encode(LISTTYPE)]; for (i=0; i<numElements; i++ ) { NXReadType(stream,"*",&theDescriptor); NXReadType(stream,"*",&theDescription); [self addListElement:theDescriptor :theDescription]; } return self; } Hope this helps!! Good luck!!
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.