Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chaonic

macrumors newbie
Original poster
Feb 22, 2008
12
0
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:

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

macrumors member
Jul 28, 2008
56
0
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

macrumors newbie
Sep 14, 2008
3
0
Did you manage to get it working?

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

macrumors 65816
Sep 20, 2002
1,193
814
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

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors newbie
Original poster
Feb 22, 2008
12
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.