• 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: Save struct to NSDictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Save struct to NSDictionary


  • Subject: Re: Save struct to NSDictionary
  • From: Steve Christensen <email@hidden>
  • Date: Thu, 15 Sep 2005 18:38:24 -0700

On Sep 15, 2005, at 1:55 PM, Lorenzo wrote:

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 would like to transform xVectors[16] to a NSData object so I can put it
into a NSDictionary and save it together with the document file.
Actually I transform xVectors to a NSMutableArray then I add this array to
the document dictionary.


- (NSMutableArray*)GetXVector
{
 NSMutableArray         *vArray = [NSMutableArray array];
 NSMutableDictionary    *vDict;

int i, totVectors = 16;
for(i = 0; i < 16; i++){
vDict = [NSMutableDictionary dictionary];
[vDict setObject:[NSNumber numberWithFloat:xVectors[i].x] forKey:@"x"];
[vDict setObject:[NSNumber numberWithFloat:xVectors[i].y] forKey:@"y"];
[vDict setObject:[NSNumber numberWithBool:xVectors[i].on] forKey:@"on"];
[vArray addObject:vDict ];
}
return vArray ;
}


But I find it not so elegant.
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?

The benefit of the approach you show above is that all the XVector fields are separated out, so if, for example, you decide to add a new field to the structure, you can read in the remaining field values and initialize the new field(s) to default value(s).


If you want to just slam the whole thing out as a NSData blob, how about something like this:

[vDict setObject:[NSData dataWithBytes:&xVectors length:sizeof(xVectors)]
forKey:@"xVectors"];


Or if you'd prefer to have 16 separate NSData blobs, you could just do the same sort of thing except working on each XVector element separately.

steve

_______________________________________________
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


References: 
 >Save struct to NSDictionary (From: Lorenzo <email@hidden>)

  • Prev by Date: Re: Xcode custom new file templates?
  • Next by Date: Re: Save struct to NSDictionary
  • Previous by thread: Save struct to NSDictionary
  • Next by thread: Re: Save struct to NSDictionary
  • Index(es):
    • Date
    • Thread