Re: Save struct to NSDictionary
Re: Save struct to NSDictionary
- Subject: Re: Save struct to NSDictionary
- From: Dustin Voss <email@hidden>
- Date: Thu, 15 Sep 2005 19:40:31 -0700
On 15 Sep 2005, at 1:55 PM, Lorenzo wrote:
Hi,
I defined a simple struct like this
typedef struct
{
float x;
float y;
BOOL on;
}
XVector;
And I have an array of this struct: XVector xVectors[16];
...
I presume there should be a way to transform xVectors to an NSData
in one
shot only, then add this NSData to my dictionary I will save to a
file.
Do you know how to convert xVectors[16] to a NSData?
This may work:
NSValue *xVectorValue = [NSValue valueWithBytes:xVectors
objCType:@encode(xVectors)];
...or maybe it should be @encode(XVector[16]).
You can then use Cocoa serialization to read/write the NSValue,
rather than using an additional NSData object. My main concern here
is endian-ness. I am not sure that NSValue will be encode or decode
each field in an endian-aware fashion.
One thing that you should not do is this:
NSData *xVectorData = [NSData dataWithBytes:xVectors length:sizeof
(xVectors)];
Apple explains why here:
file:///Developer/ADC Reference Library/documentation/Cocoa/
Conceptual/Archiving/Tasks/codingctypes.html#//apple_ref/doc/uid/
20001294-96941-BBCDIHEF
Though if the file is for the application's own temporary use only, I
suppose the NSData technique will work just fine.
_______________________________________________
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