开发者

Specifically detailing what defaults have changed on NSUserDefaultsDidChangeNotification

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, a开发者_如何学JAVAgain, 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.


Take a look at Apple's documentation: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

At the very end there is a notifications section which states:

NSUserDefaultsDidChangeNotification This notification is posted when a change is made to defaults in a persistent domain. The notification object is the NSUserDefaults object. This notification does not contain a userInfo dictionary.

So the NIL userInfo dictionary is a choice Apple made. If you really want this feature I would recommend you submit a comment to Apple via http://bugreport.apple.com/

I ultimately got around this by just writing some logic to compare the settings in the notification to the ones I had loaded to let me know which ones had changed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜