sách gpt4 ai đã đi

cocoa - 何时(不)滥用 NSUserDefaults

In lại 作者:行者123 更新时间:2023-12-03 16:01:29 26 4
mua khóa gpt4 Nike

我想知道这些指南的用途:
1 - 我多久可以读取 NSUserDefaults
2 - 我可以在 NSUserDefaults 中合理存储多少数据

显然,NSUserDefaults 的使用量是有限制的,但我很难确定什么是合理的,什么是不合理的。

一些例子:

  • 如果我的游戏有一个选项让计算机成为玩家之一,我将使用 NSUserDefaults 来保存该 bool 值。这已经很清楚了。但是,每次我想知道计算机是否是玩家时,在游戏过程中访问 NSUserDefaults 是否也合理,或者我应该使用实例变量来代替?假设我需要每秒检查该 bool 值。答案是否相同,只是 100 毫秒?每 10 秒怎么样?

  • 如果我的游戏有 50 个移动物体,并且我希望在用户退出应用程序时存储它们的位置和速度,那么 NSUserDefaults 是存储该数据的合理位置吗?那么 20 个移动物体呢? 200 怎么样?

1 Câu trả lời

I wonder what the guidelines are for: 1 - how often I can read from NSUserDefaults

相当有规律。期望默认值的开销与线程安全的 NSDictionary 类似

2 - how much data I can reasonably store in NSUserDefaults

从 body 上来说,这超出了您的需要。逻辑最大值是您需要的速度以及它在磁盘上占用的空间。还请记住,此表示形式是在启动/关闭以及其他各种时间时从磁盘读取/写入的。

If my game has an option for the computer to be one of the players, I will use NSUserDefaults to save that boolean value. That much is clear. But is it also reasonable to access NSUserDefaults during my game every time I want to know whether the computer is a player or should I be using an instance variable for that instead?

只需向对手对象添加一个const bool即可。除了内存之外,运行时损失为零,这不会很严重。

Assume here I need to check that boolean every second. Is the answer the same is it's 100 ms instead? What about every 10 s?

同样,它就像一个线程安全的 NSDictionary(散列)。它会相当快,并且足以以该频率进行读取。是否是最好的设计取决于程序。如果它变得很大,那么性能将会受到影响。

If my game has 50 moving objects and I want their positions and speeds to be stored when the user quits the app, is NSUserDefaults a reasonable place to store that data? What about 20 moving objects? What about 200?

没问题,尽管我不会在游戏过程中通过用户默认值进行读/写;只需根据需要保存/加载状态即可。

我不建议将所有这些保存在用户默认值中。只需为您的游戏状态创建一个文件表示,并使用用户默认值来实现其设计目的。如果它很大并且您经常写入它,那么实现可能会定期将状态刷新到磁盘,这可能需要相对较长的时间。

关于cocoa - 何时(不)滥用 NSUserDefaults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5520797/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com