观察 NSUserDefaults 中各个设置的更改

发布于 2024-12-13 19:15:23 字数 368 浏览 4 评论 0原文

我想观察 [NSUserDefaults standardDefaults] 中设置值的更改。我正在使用 InAppSettingsKit 访问和更改程序中的设置。到目前为止,我只是假设所有值都已更改,并向所有观察者发送通知以根据新值进行更新。这效率不高,我只想在发生相关更改时向观察者发送通知。

有人可以建议一种有效的方法来做到这一点吗?

我可以假设 [NSUserDefaults standardDefaults] 中的任何键的值都不是数组、集合或字典吗? (使用设置对我来说是新的。如果我必须对更改进行强力扫描,将前字典与后字典进行比较,这可能会有所帮助。并且不需要任何递归。)

如果这已经在InAppSettingsKit,我还没见过,如果有人能指出,我会很高兴。

I would like to observe changes to settings values in [NSUserDefaults standardDefaults]. I am using the InAppSettingsKit to access and change settings in the program. Until now, I have just assumed all values have changed and sent notifications to all observers to update based on new values. This is not efficient and I only want to send notifications to observers when a relevant change has occurred.

Can someone suggest an efficient way to do this?

Can I assume that the value for any key from [NSUserDefaults standardDefaults] is never an array, set, or a dictionary? (Working with settings is new for me. This would probably help if I had to do a brute force scan for changes, comparing a before-dictionary with an after-dictionary. And it would not require any recursion.)

If this is already in the InAppSettingsKit, I haven't seen it, and I would be glad if someone could point it out.

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

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

发布评论

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

评论(2

眸中客 2024-12-20 19:15:23

没有系统通知保存默认值更改的详细信息。我不能谈论 inappsettings 套件,因为我没有使用过它。

响应更改的各个对象应保留其相关默认设置的本地值,并且仅在必要时执行昂贵的操作。因此响应通知应该是一个简单的操作。如果您有两个单独的对象响应单个默认键中的不同子对象,那么您可能需要重新考虑默认值的组织方式。如果您有一些具体示例,请将它们包含在您的问题中。

在重写整个应用程序以“效率”的名义拥有某种默认管理器对象之前,请进行测试以确保您实际上浪费了大量的、影响用户体验的时间让大量对象响应通知,否则您过早地进行优化,你知道他们对此有何评论。

在回答你问题的第二部分时,你不能做出这样的假设。任何属性列表对象都可以存储在用户默认值中。

There is no system notification that holds the details of which default value was changed. I can't speak for inappsettings kit as I have not used it.

Your individual objects that are responding to the changes should hold local values for their relevant default settings and only perform expensive operations if necessary. Therefore responding to the notification should be a simple operation. If you have two separate objects that respond to different sub-objects within a single dafaults key then you may want to re-think how your defaults are organised. If you've got some specific examples please include them in your question.

Before rewriting your whole app to have some sort of defaults manager object in the name of "efficiency" do test to make sure that you are actually wasting significant, user-experience impacting amounts of time having lots of objects respond to the notification, otherwise you are optimising prematurely and you know what they say about that.

In answer to the second part of your question, you can't make that assumption. Any property list objects can be stored in user defaults.

叫思念不要吵 2024-12-20 19:15:23

iOS 中尚不支持绑定。

Binding is not yet available in iOS.

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