I want to retrieve an integer from a plist file, increment it, and write it back to the plist file. Inside the “Levels.plist” file is a row with the key LevelNumber, and with a value of 1.
I use this code to retrieve the value:
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Levels.plist" ofType:@"plist"];;
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
lvl = [[plistDict objectForKey:@"LevelNumber"]intValue];
NSLog(@"%i", [[plistDict objectForKey:@"LevelNumber"]intValue]);
When I run this, I get the console output of 0. Can someone tell me what I’m doing wrong?
My guess is that NSBundle is returning nil for the
pathForResource:ofType:call, unless you’ve actually named your file “Levels.plist.plist”.Keep in mind that if that method happens to return
nil, the rest of your code can still proceed. Given anilfile path, theNSMutableDictionarywill return nil, and subsequent calls to get objects out of the dictionary will also returnnil, hence your logging call showing an output of 0.