保存一组整数 iPhone

发布于 2024-09-28 11:44:46 字数 518 浏览 6 评论 0原文

在我的游戏中,我想保存一组整数作为玩家可以查看的统计数据。例如死亡人数。这是一个整数,每次游戏结束时我都会加一。

我怎样才能保存它,然后在重新启动游戏时将其保留在该号码上?

谢谢。

编辑:

好的,在阅读了一些答案后,我认为写入 plist 是前进的方向。我一直在看教程,但可以说我尝试一下:

scoreData *score = [scoreData sharedData];
[dictionary setValue:score.highScore forKey:@"key2"];
[dictionary writeToFile:@"stats.plist" atomically:NO];

我已经访问了我的单例,里面有我的分数。现在,当尝试 setValue 时,我收到一条错误消息,提示我正在尝试将 int 转换为对象。

我不知道还有什么办法可以解决它。这看起来很简单,但是我看到的每个地方似乎都给出了基本相同的方法。

感谢迄今为止的帮助,非常感谢。

In my game I want to save a set of integers as statistics players can view. E.g number of deaths. This is an int I simply increase by one each time they get a game over.

How can I save this then have it at that number when I relaunch the game?

Thanks.

EDIT:

Ok after reading a few answers Im thinking writing to a plist is the way forward. I have been looking at tutorials but lets say I try this:

scoreData *score = [scoreData sharedData];
[dictionary setValue:score.highScore forKey:@"key2"];
[dictionary writeToFile:@"stats.plist" atomically:NO];

I have accessed my singleton with my score inside. Now when trying to setValue I get an error saying Im trying to convert an int to object.

Im not sure how else to approach it. It seems simple enough, however everywhere I look seem to give essentially the same approach.

Thanks for the help thus far, anymore is appreciated.

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

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

发布评论

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

评论(4

桃气十足 2024-10-05 11:44:46

我不会滥用 NSUserDefaults(Apple 在今年的 WWDC 上不鼓励这样做)。相反,为什么不简单地创建一个 NSMutableDictionary,然后在其中存储 NSNumber 对象。 MutableDictionary 可以很容易地写入文件并且很容易读入。

I would not abuse NSUserDefaults (Apple discouraged this at WWDC this year). Instead why not simply create an NSMutableDictionary and then store NSNumber objects in it. The MutableDictionary can easily be written to file and as is easily read in.

宣告ˉ结束 2024-10-05 11:44:46

任何数量的很多方式。

如果您只保存这个以及其他一些简单的事情,那么使用用户默认值可能是最好的主意。

但是,如果您要保存的项目数量多于几个,您可能需要使用自己的属性列表(如果项目数量少于 200 左右)。

如果您有很多设置,我通常建议人们查看核心数据。它处理很多物品的速度很快,而其他两个则没有那么多。

Any number of a lot of ways.

If you are only saving this and maybe a couple other simple things, using user defaults is probably the best idea.

If however, you are saving a lot more items than just a few, you may want to either use your own property list (if the number of items is less than 200 or so).

If you have a lot of settings, I generally advise folks to look at Core Data instead. It's fast with lots of items, whereas the other two, not so much.

∞梦里开花 2024-10-05 11:44:46

尝试...

[[NSUserDefaults standardUserDefaults] setInteger:highScore forKey:@"HighScore"];
highScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];

Try...

[[NSUserDefaults standardUserDefaults] setInteger:highScore forKey:@"HighScore"];
highScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];
农村范ル 2024-10-05 11:44:46

您可以写入文件(即纯 C 函数 fopen() 等),可以使用您自己的类并使用 NS(Keyed)Archiver,或使用 NSUserDefaults 保存数据。

You can write to a file (i.e. plain C functions fopen() etc), can use your own classes and use NS(Keyed)Archiver, or use NSUserDefaults to save data.

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