This is actually more complicated than merely subclassing NSMutableDictionary. NSMutableDictionary (and likewise NSMutableArray) are opaque classes. That is that they are merely interfaces that hide dynamic data structure that doesn't conform like a normal concrete class. Specifically when you create an NSMutableDictionary, you get a class that utilizes the NSMutableDictionary interface, but is not actually an NSMutableDictionary! That means the returned class won't actually be your class, Yikes! To subclass NSMutableDictionary is really to implement the NSMutableDictionary interface with custom functionality.
What you'll need to do is have your subclass have a data structure as a member variable (simplest would just be an NSMutableDictionary) and use that class for when you implement your override methods.
example:
@interface MyMutableDictionary : NSMutableDictionary
... interface methods ...
@end
and
@implementation MyMutableDictionary
{
NSMutableDictionary* _proxy;
}
- (id) init {
if (self = [super init]) {
_proxy = [[NSMutableDictionary alloc] init];
}
return self;
}
... implementation of methods ...
- (void) setObject:(id)obj forKey:(id)key {
if (obj) {
[_proxy setObject:obj forKey:key];
} else {
[_proxy removeObjectForKey:key];
}
}
@end
This example is a contrived one that shows how to implement a custom NSMutableDictionary that doesn't throw an exception when setting a nil object but instead removes the object with that key. For any remaining methods that you need to just directly use the forwardingTargetForSelector: method such that you can have your _proxy member take over any unimplemented methods (though you will need to subclass NSObject instead and explicitly declare all the desired methods you want for your mutable dictionary - this is because otherwise the real NSMutableDictionary methods will be used which won't work).
- (id) forwardingTargetForSelector:(SEL)aSelector
{
return _proxy;
}