PDA

View Full Version : saving to plist




chaonic
Aug 4, 2008, 02:46 PM
Hi. I'm trying to read and save to Info.plist. I'm having no problem reading the file... and it would appear that I'm having no problem saving the file after editing one of its values. But when I relaunch the program, the key that I edited, "savedLevelNumber," reverts back to its original value. Here's my code:

// read plist
NSData *plistData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]];
NSString *readError;
NSPropertyListFormat format;
id plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListMutableContainers format:&format errorDescription:&readError];
if (!plist) {
NSLog(readError);
[readError release];
} else {
int levelNumber = [[note object] intValue];
[plist setValue:[NSString stringWithFormat:@"%i", levelNumber] forKey:@"savedLevelNumber"];
}

// save plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
NSData *xmlData;
NSString *saveError;
xmlData = [NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListXMLFormat_v1_0 errorDescription:&saveError];
if (xmlData) {
NSLog(@"No error creating XML data.");
[xmlData writeToFile:path atomically:YES];
} else {
NSLog(saveError);
[saveError release];
}


I'd greatly appreciate if anyone can help! Thanks!



Taum
Aug 4, 2008, 04:21 PM
Hi,

Actually, sending the message writeToFile:atomically: does not mean you succeeded in writing the file. These kind of things can fail, which is why the answer to writeToFile:atomically: is a BOOL indicating success or failure.

But from glancing at your code, it seems you're simply trying to save an integer. Is there a particular reason for you wanting to write it into Info.plist ? I think you should use NSUserDefaults instead.

PaulieBoy
Sep 23, 2008, 04:11 PM
Hi

Did you manage to get your iPhone to save it's settings to a pList in the end?

I have just managed to do so, if you are still looking to use that method, I can post a code example?

Cheers...

DipDog3
Sep 24, 2008, 08:07 AM
Hi

Did you manage to get your iPhone to save it's settings to a pList in the end?

I have just managed to do so, if you are still looking to use that method, I can post a code example?

Cheers...

I could use that example if you would post it.

PhoneyDeveloper
Sep 24, 2008, 08:23 AM
You can't write to your own bundle, at least not on the phone. It is almost certainly write protected and would probably affect the code signing of the app. You need to write to the Documents folder.

Also, you're doing this the hard way. Just build an NSArray or NSDictionary and write that out and read it in.

chaonic
Sep 24, 2008, 11:50 AM
Hi PaulieBoy, thanks for the code offer. I actually ended up abandoning the plist idea in favor of what I saw Apple doing in their DrillDownSave example. They use "NSUserDefaults," and the implementation is very straight forward.