I'm trying to increment a value in a plist in objective-c (using cocos2d).
I'm loading data using this:
documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* filePath2 = [documentsDir stringByAppendingPathComponent:@"weaponchallenges.plist"];
weaponChallengesList = [[NSMutableArray arrayWithContentsOfFile:filePath2] retain];
I believe this works as it is not erroring, although I'm not sure the best way to test this.
Inside the plist are various Dictionary items, within each items various keys + integers.
For example the first item in the plist has an item key 'kills' with an int value of 0.
I'm then using:
NSDictionary * weaponC = [weaponChallengesList obj开发者_StackOverflowectAtIndex:0];
int killsTotal = [[weaponC valueForKey:@"kills"] intValue];
[weaponC setValue:[NSNumber numberWithInt:(killsTotal + 1)] forKey:@"kills"];
[weaponChallengesList replaceObjectAtIndex:0 withObject:weaponC];
But killsTotal always seems to be 0, any thoughts on what I could be doing incorrectly?
Check each step to see if the value is nil
. Routines like arrayWithContentsOfFile:
do not generate errors. They just return nil
. So you'll wind up with weaponChallengesList
is nil
, weaponC
is nil
, killsTotal
is 0, and your replaceObjectAtIndex:withObject:
silently doing nothing.
In ObjC, if "nothing" seems to happen, then the #1 cause is that you're messaging nil
.
精彩评论