如何使 NSUserDefault 设置与应用内设置保持同步?
我已经使用 NSUserDefaults 来更改 settings.bundle 中的设置。我也有使用观察者更新的应用程序设置。这很好用。当我更改应用程序中的设置时,我会更新 NSUserDefaults 值以将其反映在设置包中。但随后会发生一些奇怪的行为。我可以更改一次,但是当我第二次将其切换到“ON”时,它会直接返回“OFF”。我猜观察员正在被呼叫,并且出现了问题。有人见过这种行为吗?我什至不明白到底发生了什么。
I have used NSUserDefaults to change settings in settings.bundle. I also have in app settings which I update using an observer. This works fine. When I change the setting in app, I update the NSUserDefaults value to reflect it in the settings bundle. But some strange behavior occurs then. I can change it once, but the second time when I switch it to ON it goes back to OFF directly. I am guessing the observer is being called then and something is going wrong. Has anyone seen this behavior? I do not even understand what is really going on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保每次编辑完 NSUserDefaults 后都会同步它们。
[[NSUserDefaults standardUserDefaults]同步];
Make sure that each time you are done editing the NSUserDefaults you sync them.
[[NSUserDefaults standardUserDefaults] synchronize];