如何更新 .plist 中的整数值?
我想更新“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此 Secret.plist 位于应用程序包中吗?如果您想编辑文件,您应该将其复制到应用程序包之外。有一些标准目录可以放置东西。
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.
The
documentsDir
is one place you could put it.Except for that the code looks fine.