Observing NSMutableDictionary changes

后端 未结 3 1298
北恋
北恋 2020-12-14 02:43

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 subscripti

3条回答
  •  轮回少年
    2020-12-14 03:03

    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!

提交回复
热议问题