Is it possible to observe (subscribe to) changes to values stored under different keys in an NSMutableDictionary? In my case the keys would already exist when the subscription is initiated, but the values change and I would like to be notified in this case. I would like the key of the changed value in the notification.
I assume that if my dictionary keys were all NSString instances I could just subscribe to each key path individually. But what if my keys are non-strings? Am I out of luck in that case?
That's an interesting idea. I can't find anything in NSDictionary or NSDictionaryController that looks promising. My first instinct would be to use composition around an NSMutableDictionary and intercept calls to setObject:forKey: (and maybe -removeObjectForKey:) to notify subscribers of changes.
There's a Cocoa With Love post on subclassing NSMutableDictionary that will likely be useful should you choose to go that route. I also have created my own NSMutableDictionary subclasses, and you're welcome to use the open-source code.
You could design an observer protocol that could specify particular keys that should be monitored. Shouldn't be too much code, but more than I have time to throw down at the moment.
I've successfully implemented this now, using composition around the NSMutableDictionary. I'm surprised how little code it took. The class I implemented is Board, used to represent the model in a board game. Anyone can subscribe to changes for board states by calling the addObserver: method, implemented like this:
- (void)addObserver:(id)observer {
for (id key in grid)
[self addObserver:observer
forKeyPath:[key description]
options:0
context:key];
}
Since you can only subscribe to keys using the KVO model, I cheated and subscribed to the description of the key, but passing the key itself as the context. In objects that observe instances of my Board I implement the -observeValueForKeyPath:ofObject:change:context:
to ignore the keyPath string and just use the passed-in context.
My simple Board class is not KVO compliant for the artificial properties I create using this method, so I passed 0
in the options
property so the KVO machinery will not try to get the old/new values of those keys. That would cause my code to blow up.
Anything that changes the board (in my simple class there is only one method that does this) raise the necessary notifications to cause the KVO machinery to leap into action:
- (void)setPiece:(id)piece atLocation:(Location*)loc {
[self willChangeValueForKey:[loc description]];
[grid setObject:piece forKey:loc];
[self didChangeValueForKey:[loc description]];
}
Voila! Subscription to an NSMutableDictionary with non-string keys!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With