如何更新.plist中的整数值?

时间:2020-12-31 07:22:59

I want to update an Integer Value inside my "Secret.plist". Here*s a picture: http://img214.imageshack.us/img214/8005/bildschirmfoto20101114u.png

我想更新我的“Secret.plist”中的整数值。这里是一张图片:http://img214.imageshack.us/img214/8005/bildschirmfoto20101114u.png

Now, in my application, I want the 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?

我执行应用程序后,我的.plist没有更新,仍然有“5000”而不是“1234”。我究竟做错了什么?

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];

编辑:退出应用程序后,我的.plist中的新值未保存/更新。只要应用程序运行,值就会以某种方式更新。因此,当我更新它后检索值时,它是正确的。当我重新启动我的应用程序时,它不再更新。 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?

甚至这个都不适合我。有没有人会有这样的代码,将我的数据保存到我的.plist中,这样我可以在重新启动应用程序时检索它?

1 个解决方案

#1


0  

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.

这个Secret.plist是否位于应用包中?如果要编辑文件,则应将其复制到应用程序包之外。有标准的目录,你可以放东西。

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];

The documentsDir is one place you could put it.

documentsDir是你可以放的地方。

Except for that the code looks fine.

除此之外,代码看起来很好。

#1


0  

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.

这个Secret.plist是否位于应用包中?如果要编辑文件,则应将其复制到应用程序包之外。有标准的目录,你可以放东西。

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];

The documentsDir is one place you could put it.

documentsDir是你可以放的地方。

Except for that the code looks fine.

除此之外,代码看起来很好。