问题描述
我开始进入NSUserDefaults的内部,现在我可以使用提供的选择器加NSNotification对象作为参数成功拦截NSUserDefaultsDidChangeNotification通知.但是,返回的NSNotification对象未按预期出现.
我期望的是:
1)从通知中心接收NSUserDefaultsDidChangeNotification.
2)询问返回的提供的NSNotification对象,以获取包含至少一个键/值对的用户信息,其中包含键="=",当您睡着时,更改了以下默认值;而object =" NSArray的应用程序键已采用新值."
我得到的是:
1)如上所述
2)UserInfo = nil! (任何人都知道为什么......?)
我的应用程序委托是NSUserDefaultsDidChangeNotification的注册响应者,因此会将更改分发给需要知道其设置不同的对象.在我开始编写自己的代码以检测哪些键已受到用户默认值更改的影响之前,有人可以建议我吗? (a)是否必须确定自己发生了哪些变化?或者我是否错过了非常明显的事情? (b)谁能指出某些文档(除dev docs!之外),这些文档提供了有关如何以Apple认可的方式处理NSUserDefaultsDidChangeNotification的示例?我希望尽可能不要重新发明轮子.我已经舍弃了返回的NSNotification对象的dictionaryRepresentation,但是,显然,它也包含Apple自己的所有键/值.再次感谢您.V.V.
,您可以使用 KVO(键值编码)来跟踪NSUserDefaults中的特定键.在此处查看我的答案与代码段: https://stackoverflow.com/a/10950217/388412
您可以注册以观察NSUserDefaults standardUserDefaults对象的特定键的键值更改.
I'm starting to get into the inner sanctum of NSUserDefaults to the point where I can now successfully intercept an NSUserDefaultsDidChangeNotification notification using a supplied selector plus NSNotification object as a parameter. However, the returned NSNotification object does not appear as I had expected.
What I was expecting was:
1) Receive NSUserDefaultsDidChangeNotification from the notification centre.
2) Interrogate the returned supplied NSNotification object for user info containing at least one key/value pair with key="While you were asleep the following defaults changed" and object="NSArray of application keys that have taken new values."
What I get is:
1) As above
2) UserInfo=nil! (Anyone know why....?)
My app delegate is the registered respondent to NSUserDefaultsDidChangeNotification and will therefore distribute the changes down to whichever objects need to know about their settings being different. Before I start writing my own code to detect which keys have been affected by a change in user defaults, can anyone advise me; (a) Does one have to determine which changes have occurred one'sself - or am I missing something incredibly obvious? (b) Can anyone point me at some documentation (other than the dev docs!) which gives examples of how to handle NSUserDefaultsDidChangeNotification in an Apple-approved manner? I'm keen not to re-invent the wheel if possible. I've dumped out the dictionaryRepresentation of the returned NSNotification object - but that, obviously, contains all of Apple's own keys/values as well.Thanks, again, in advance.V.V.
you can use KVO (Key Value Coding) to track specific keys in the NSUserDefaults.See my answer here with code snippets: https://stackoverflow.com/a/10950217/388412
Instead of registering as a general observer for any NSUserDefaultsDidChangeNotifications in the NSNotificationCenter, you can register to observe key-value-changes for a specific key of the NSUserDefaults standardUserDefaults object.
这篇关于特别详细说明NSUserDefaultsDidChangeNotification更改了哪些默认设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!