How do I update an Integervalue in a .plist?
I want to update an Integer Value inside my "Secret.plist". Here*s a picture: http://img214.imageshack.us/img214/8005/bildschirmfoto20101114u.png
Now, in my application, I want th开发者_StackOverflow社区e gold value to change, here is my code so far:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Secret" ofType:@"plist"];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSNumber *goldNumber = [NSNumber numberWithInt:1234];
[tempDict setObject:goldNumber forKey:@"Gold"];
[tempDict writeToFile:path atomically:YES];
My .plist didn't update after I executed the application, there still is a "5000" and not a "1234". What Am I doing wrong?
EDIT: My new value inside my .plist isn't saved/updated after I quit the application. As long as the applications runs, the values are somehow updated. So when I retrieve the value AFTER I updated it, it's correct. When I restart my application, it isn't updated anymore. NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[tempDict setObject:@"A new String." forKey:@"key"];
[tempDict writeToFile:path atomically:YES];
Not even this one would work for me. Does anybody has the code that simply would, will save my data to my .plist so that I can retrieve it when I restart my app?
Is this Secret.plist located in the app bundle? If you want to edit a file you should copy it outside the app bundle. There are standard directories where you can put stuff.
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
The documentsDir
is one place you could put it.
Except for that the code looks fine.
精彩评论