views:

56

answers:

2

Hello,

Im sure this is a really easy to answer question but I'm still new to cocoa. I need to save my applications data. The app has 4 text fields and each field needs to be saved into one file. Then when you open the file it needs to know what goes in what field. Im really stuck with this. Also, I do know how to use the save panel. Thanks in advance.

+1  A: 

A convenient way would be to use PLists:

NSDictionary *arr = [NSDictionary dictionaryWithObjectsAndKeys:
                      string1, @"Field1", string2, @"Field2", nil];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:arr
                format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];

NSSavePanel *panel = [NSSavePanel savePanel];

NSInteger ret = [panel runModal];
if (ret == NSFileHandlingPanelOKButton) {
    [data writeToURL:[panel URL] atomically:YES];
}

For deserialization:

NSData       *data = [NSData dataWithContentsOfURL:urlOfFile];
NSDictionary *dict = [NSPropertyListSerialization propertyListFromData:data
                       mutabilityOption:NSPropertyListImmutable
                       format:nil errorDescription:nil];
NSString *string1 = [dict objectForKey:@"Field1"];
// ... etc.
Georg Fritzsche
Thanks but what Im not sure how to do would be retrieve each piece of data then
happyCoding25
Awesome that will work perfectly! Thanks
happyCoding25
A: 

The question is unclear... does "each field needs to be saved into one file" mean 4 files or 1 file? And is this like a document-based app, with different files for different data sets?

JWWalker
There needs to be 1 file.
happyCoding25