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

发布于 2024-10-02 12:59:31 字数 1051 浏览 3 评论 0原文

我想更新“Secret.plist”中的整数值。 这里*一张图片:http://img214.imageshack.us/img214/8005/bildschirmfoto20101114u。 现在,在我的应用程序

中,我希望更改黄金值,这是到目前为止我的代码:

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

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

编辑:退出应用程序后,我的 .plist 中的新值不会保存/更新。只要应用程序运行,这些值就会以某种方式更新。因此,当我更新后检索该值时,它是正确的。当我重新启动应用程序时,它不再更新。 NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    [tempDict setObject:@"A new String." forKey:@"key"];
    [tempDict writeToFile:path atomically:YES];

即使这个对我来说也不起作用。是否有人拥有简单地将我的数据保存到我的 .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 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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短叹 2024-10-09 12:59:31

此 Secret.plist 位于应用程序包中吗?如果您想编辑文件,您应该将其复制到应用程序包之外。有一些标准目录可以放置东西。

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

documentsDir 是您可以放置​​它的地方。

除此之外,代码看起来不错。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文