1
votes

This has been asked before, but there's no definite answer, maybe something has changed in a year.

My Android application is able to observe changes made to preferences and propagate the appropriate changes to all parts of the application.

I know that Objective-C has a good support for Key-Value Observing. Is it possible to observe changes made to preferences in Objective-C? I can: create a facade for the NSUserDefaults:

define a certain "Preferences" class,

implement a property for each user default I'm interested in,

Register classes interested in changes to observe changes to the properties

set user defaults through the "Preferences" class

change the value of the property for the default each time the default is set

and use KVO that way.

Is there an easier way? this seems like a maintenance nightmare.

Thank you!

1

1 Answers

2
votes

On MacOS there is the NSUserDefaultsController to handle this, but as I've been informed, that's not available on iOS.

It would seem that your only recourse here is to listen for the very coarse-grained NSUserDefaultsDidChangeNotification and then repopulate everything based on user defaults. Otherwise, yes, you'd have to keep a copy of the old state around and provide custom code to send Key-Value change notifications for each difference. Kind of a drag.

It's unfortunate; I know iOS doesn't support Cocoa bindings, but I can still see uses for NSUserDefaultsController.

My original post:

As I understand it, NSUserDefaults alone is not Key-Value Observable, but instead uses an NSNotification to notify listeners about changes. If you want to KVO the user defaults you would want to use an NSUserDefaultsController.

Is there something you want to do that NSUserDefaultsController doesn't enable? Why would you make your own class for this?