Specifically detailing what defaults have changed on NSUserDefaultsDidChangeNotification

后端 未结 2 1351
野性不改
野性不改 2021-01-22 16:01

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 sup

相关标签:
2条回答
  • 2021-01-22 16:24

    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 讨论(0)
  • 2021-01-22 16:35

    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.

    0 讨论(0)
提交回复
热议问题