How to save C++ objects using Cocoa?
- From: Steve555 <foursheds@xxxxxxxxxxxxxx>
- Date: Fri, 2 Jan 2009 02:16:31 -0800 (PST)
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
.
- Follow-Ups:
- Re: How to save C++ objects using Cocoa?
- From: Gregory Weston
- Re: How to save C++ objects using Cocoa?
- Next by Date: Re: How to save C++ objects using Cocoa?
- Next by thread: Re: How to save C++ objects using Cocoa?
- Index(es):
Relevant Pages
|