简体   繁体   English

ios-单个密钥与在NSUserDefaults中存储结构化数据

[英]ios - Individual key vs Storing structured data in NSUserDefaults

I have like 100 levels in my game and I want, for example, to store completion rate (in %) and highscore (int) 我的游戏中有100个关卡,例如,我想存储完成率(%)和高分(int)

What would be the best way to accomplish it with NSUserDefaults? 用NSUserDefaults完成它的最佳方法是什么?

Should I use individual keys for that like 我应该像这样使用单个键吗

"levelCompletion%number%", "levelHighscore%number%"

or maybe store a collection somehow 或以某种方式存储收藏

"levelInfo%number%"

And an object for levelInfo key would be NSDictionary, for example. 例如,levelInfo键的对象为NSDictionary。

Which way is more efficient? 哪种方法更有效?

I would make a dictionary with all of the keys for each level, and nothing/empty/NSNumber(0) values. 我会制作一个字典,其中包含每个级别的所有键,并且不包含/空/空/ NSNumber(0)值。 That way when the defaults are spooled out, all 100 will be there for examination, although normally most would be "0". 这样,当假脱机默认值时,所有100个都将在那里检查,尽管通常大多数情况下为“ 0”。

There is no efficiency concern here, this is a triviality in storage terms and should be at the bottom of any list of premature optimization! 这里没有效率问题,这在存储方面是不重要的,应该在任何过早优化列表的底部!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM