saving to plist

Discussion in 'iPhone/iPad Programming' started by chaonic, Aug 4, 2008.

  1. macrumors newbie

    #1
    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!
     
  2. macrumors member

    #2
    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.
     
  3. macrumors newbie

    #3
    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...
     
  4. macrumors 6502a

    DipDog3

    #4
    I could use that example if you would post it.
     
  5. macrumors 68030

    PhoneyDeveloper

    #5
    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.
     
  6. macrumors newbie

    #6
    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.
     

Share This Page