I have an array of NSMutableDictionary objects which are displayed in a master–detail interface which has a few text fields and a bunch of check boxes. The controls are bound to the dictionary keys, accessed through an array controller’s selection.
I’d like to add some logic which clears one check box when another is cleared, and restores the original value if it’s rechecked in the same session. Since I need to associate storage with the dictionary, and need to add code too, I thought I’d use composition to extend NSMutableDictionary.
Here’s what I’ve done:
- I created a
LibraryEntrysubclass which contains an NSMutableDictionary. - I implemented
forwardInvocation:,respondsToSelector:,methodSignatureForSelector:, and after some trial-and-errorvalueForUndefinedKey:. - I created my forwarder objects.
- I left the bindings as they were.
It loads up the data just fine, but I’m guessing that KVO won’t work correctly. I’m guessing the binder is calling addObserver: on the my object but I haven’t implemented anything special to handle it.
I thought of simply overriding addObserver: and forwarding the message to the dictionary. But if I do that the observeValueForKey: notifications won’t originate from my object (the original receiver of addObserver), but from the dictionary.
Before I tried to implement more transparent forwarding for these KVO calls, I thought … this is getting messy. I keep reading “use composition, not subclassing” to get behavior like this. Is it just the wrong pattern for this situation? Why? Because of KVO?
It seems like I’d have cleaner results if I abandon composition and choose one of these alternatives:
- Use decorators, with one instance observing each dictionary
- Store the transient keys in the dictionary and have the controller remove them before saving
- Dispense with the dictionary and declare properties instead
Here’s my code, in case it’s helpful (values is the dictionary):
- (void)forwardInvocation:(NSInvocation *)anInvocation {
if ([values respondsToSelector:[anInvocation selector]])
[anInvocation invokeWithTarget:values];
else
[super forwardInvocation:anInvocation];
}
- (BOOL)respondsToSelector:(SEL)aSelector {
if ( [super respondsToSelector:aSelector] )
return YES;
else
return [values respondsToSelector:aSelector];
}
- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
NSMethodSignature* signature = [super methodSignatureForSelector:selector];
if (!signature) signature = [values methodSignatureForSelector:selector];
return signature;
}
-(id)valueForUndefinedKey:(NSString *)key {
return [values valueForKey:key];
}
I think with a combination of Objective-C associated storage, and some blocks, you could hook up arbitrary behaviors to a dictionary (or any other KVO compliant object) and solve your problem that way. I cooked up the following idea that implements a generic KVO-triggers-block mechanism and coded up and example that appears to do the sort of thing you want it to do, and does not involve subclassing or decorating foundation collections.
First the public interface of this mechanism:
This will allow you to attach block-based observations/behaviors to arbitrary objects. The task you describe with the checkboxes might look something like this:
I implemented the block/KVO hook-up by creating an object to keep track of the blocks and userInfos, then have that be the KVO observer. Here’s what I did:
One thing that’s sort of unfortunate, is that you have to remove the observations/behaviors in order to break down the transitive retain cycle between the original dictionary and the observing object, so if you don’t remove the behaviors, you’ll leak the collection. But overall this pattern should be useful.
Hope this helps.