• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: structs and cocoa & doc
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: structs and cocoa & doc


  • Subject: Re: structs and cocoa & doc
  • From: Lester Dowling <email@hidden>
  • Date: Thu, 13 Oct 2005 14:04:59 +1000

ADD has a page about encoding and decoding C data types. Assuming you have a standard Developer installation, then try:

file:///Developer/Documentation/Cocoa/Conceptual/Archiving/Tasks/ codingctypes.html

However, to quote: "The best technique for archiving a structure is to archive the fields independently and chose the appropriate type of encoding/decoding method for each."

However, if you wish to encode with encodeValueOfObjCType as in your source code, the @encode directive should have the name of your tagged structure, not the pointer to that structure. Plus a few other changes, like this:

- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeValueOfObjCType:@encode( /* name of the structure */ struct _d2sData )
at: /* no ampersand here because d2s_struct is a pointer */ d2s_struct ];
}


- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super init]) {
d2s_struct = malloc( sizeof( struct _d2sData ) ); /* Allocate some memory to receive the decoded value. */
[coder decodeValueOfObjCType:@encode( /* name of the structure */ struct _d2sData )
at: /* no ampersand here because d2s_struct is a pointer */ d2s_struct ];
}
return self;
}


The important conceptual difference is between encoding a structure and encoding a pointer to that structure. The encoder will not follow a pointer to the block of memory that is the structure it points to. The encoder will simply write out the actual memory address which is the value of the pointer.

Encoding a pointer is mostly useless because the value of a pointer is only the memory address for a block of memory. When the pointer is decoded in the next incarnation of the application, it will have the same value that it had previously, however, the block of memory that that points to will be completely different. The previous block of memory which had all your data would not be saved and would be lost.

Therefore, you want to encode the whole structure: that is, write out the whole block of memory, not just its address. You can encode the whole structure with encodeValueOfObjCType however the same caveat about following pointers applies. Should there be pointers within your structure that contain the address of other structures, then the encoder will not follow those pointers. You have to code for that behaviour yourself.

Lester
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Newbie Q: layout in NSMenus
  • Next by Date: Newbie Q: layout in NSMenu
  • Previous by thread: structs and cocoa & doc
  • Next by thread: Re : Newbie Q: equipping an NSDocument subclass with a window controller (2)
  • Index(es):
    • Date
    • Thread