How to save C++ objects using Cocoa?



Hi

I need to save arrays of custom C++ class objects in to a binary file.
Unfortunately _all_ the example code I can find assumes that data is
already cocoa-ized, with NSArrays and NSMutableDictionary. I'm finding
it impossible to untangle the cocoa containers from the cocoa saving
code.

I have an existing Obj-C++ project and have solved all the other
problems with Obj-C <-> C++, and I already know how to save files as
text using cocoa.

I am having to use C++ containers for cross compatibility.
(I'm actually using STL containers, but for the sake of clarity I'll
remove all abstraction and just use simple arrays)

I have an array 10,000 C++ objects that, once processed, I want to
save as a single binary file:
MyCPPClass *myClassObject = new MyCPPClass[10000];

No keys, no dictionaries, just the very simplest case.
I'm aware I have to have:

- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject: (???Don'tKnow???) forKey:@"???Don'tKnow???"];
}

and then something like:
- (void) saveDataToDisk
{
NSString * path = [self pathForDataFile]; //-- I know how to handle
this part
NSMutableDictionary * rootObject;
rootObject = [NSMutableDictionary dictionary];
[rootObject setValue: [self ???Don'tKnow???] forKey:@"???
Don'tKnow???"];
[NSKeyedArchiver archiveRootObject: rootObject toFile: path];
}

.... and I guess I do have to procure some NSArrays and NSDictionary
before saving:

- (id) prepareMyData
{
if (self = [super init])
{
NSArray * keys = [NSArray arrayWithObjects: ???
Don'tKnow??? ];
NSArray * values = [NSArray arrayWithObjects: ???
Don'tKnow??? ];
properties = [[NSMutableDictionary alloc] initWithObjects:
values forKeys: keys];
}
return self;
}

Can anyone help me to fill in the missing pieces, it looks easy in my
books but I've got completely stuck.

Thanks

Steve
.



Relevant Pages

  • Re: binary file
    ... a text file and converting the text data obtained into a binary file. ... You've declared two arrays, each of which has only one element. ... you can pass them to fwrite. ...
    (comp.lang.c)
  • Re: How to read binary data into a STRUCT?
    ... Dennis JD Myrén ... > I'm trying to figure out how to read a formatted binary file into a ... > in the structure definition. ... The above example is using arrays in the STRUCT definition. ...
    (microsoft.public.dotnet.general)
  • Re: Fortran Basics
    ... Well, the binary file eliminates the text file and unless the sizes of the data arrays are _quite_ large, I can't imagine performance would suffer noticeably, or at least to the point of being a real bottleneck. ... you've written the entire array (in column major order, just like Matlab expects). ... I'm sure there are many that have language-sensitive features that they can recommend; Brief does and I've modified the built-in feature macros fairly extensively over the course of almost 30 years so as long as it continues to function I'll not be looking elsewhere; in fact given my essentially retired state I'm sure I'll simply quit rather than switch if it were ever to come to that ...
    (comp.lang.fortran)
  • Re: Performance of element access in Vector
    ... through all containers? ... arrays in 95% of cases and recommended as a "default" array-like ... class program entity is crucial here. ... Database Access Library for Ada: ...
    (comp.lang.ada)
  • Re: Problem Compiling .DLL
    ... How about arrays of class objects instead of UDTs... ... I am having problems when I try to compile it. ... >> on two separate maching and I am getting similar ...
    (microsoft.public.vb.general.discussion)