PDA

View Full Version : Saving/Loading...


CarbonX
2004.09.24, 01:59 AM
I don't know anything about how to do saving or loading in Cocoa, or do I know much about fread/fwrite... if somebody could give me some code or point me in the right direction it would be much appreciated :)

basically I'm trying to save a 64 x 64 array of ints representing the tile map, and the locations of enemies and the player. I am trying to write it so when you load a level the standard OS X load/save panels appear. thanks again ;)

Taxxodium
2004.09.24, 04:31 AM
To show up the Save and Open panels use NSSavePanel and NSOpenPanel respectively. If your array was made as an NSArray you can save it by using the method writeToFile:atomically:, if not then you'll need to use the filemanager I think, or convert your array to an NSArray (that would be the easiest).

Read the documentation at the apple site: Cocoa Docs (http://developer.apple.com/documentation/Cocoa/Cocoa.html)

reubert
2004.09.24, 04:35 AM
If you're going to do it the cocoa way, you'll have to get all those ints into an NSArray or NSDictionary. [NSNumber numberWithInt:myInt] will allow you to store an int in an NSNumber which can then be added to an NSArray. Once you have an array you have a number of options of how you store that data. Have a look at the docs for NSArchiever and NSCoder.

I like to use xml plists to store my data... see source code below.

For opening / saving, you just get the filename using NSOpenPanel and NSSavePanel.

David

- (NSArray *) loadArrayFromXMLFile:(NSString *)fileName
{
NSData *plistData;
NSString *error = [[NSString alloc] initWithFormat:@"Error reading xml file: %s",fileName];
NSPropertyListFormat format;
NSArray *plist;
plistData = [NSData dataWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:fileName
ofType:@"xml"]];

plist = [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&error];
if(!plist)
{
NSLog(error);
[error release];
return NULL;
}

return plist;
}

- (void) writeArray:(NSArray *)plist toXMLFile:(NSString *)fileName
{
NSData *xmlData;
NSString *error = [[NSString alloc] initWithString:@"error writing to xml file"];

xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(xmlData)
{
NSLog(@"No error creating XML data.");
[xmlData writeToFile:fileName atomically:YES];
}
else
{
NSLog(error);
[error release];
}
}

DoG
2004.09.24, 06:22 AM
You should look up the NSArchiver and NSCoder types, those are made to store and restore binary data and freeze-dried objects. Going with a plist is only useful if you want a human-readable file format.

Steven
2004.09.24, 10:15 AM
You could also serialize the numbers directly into an NSData and store it from there.

blobbo
2004.09.24, 11:47 PM
you should just make a document-based cocoa application. truly, it makes your life a whole lot easier.

Steven
2004.09.25, 11:47 AM
Yes, it does :p