• 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: Sherm Pendley <email@hidden>
  • Date: Thu, 15 Sep 2005 21:43:02 -0400

On Sep 15, 2005, at 4: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 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?

Well, you *could* do this:

NSData *theData = [NSData dataWithBytes:&xVectors length:sizeof (XVector)*16];

But honestly, I wouldn't do that. I like your original code better. It's more verbose, yes. But, cramming the whole array into a single NSData object is fragile. It's a direct copy of the data in memory, so if the app that reads the file was compiled with different endian and/or alignment options, it'll get garbage back. Issues such as that are always important, but they're doubly so right now, with the Intel switchover on the horizon.

So, I think you're on the right track with your original code - just add the array to your document dictionary, and store that in a less fragile text-based format like plist or xml.

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
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: Save struct to NSDictionary
  • Next by Date: Getting information out of a PDF with PDFKit
  • Previous by thread: Re: Save struct to NSDictionary
  • Next by thread: Re: Save struct to NSDictionary
  • Index(es):
    • Date
    • Thread