You probably don't want to override valueForKey
, because NSDictionary
and NSMutableDictionary
are abstract base classes hiding a class cluster behind them. Per their documentation, in a straight subclassing you'd need to reimplement all of:
- setObject:forKey:
- removeObjectForKey:
- count
- objectForKey:
- keyEnumerator
It's easier to implement an alternative class that pretends to be an NSMutableDictionary but forwards any calls it doesn't understand to an actual NSMutableDictionary. Which you can do via forwardingTargetForSelector:.
E.g.
@interface MyFakeDictionary: NSObject
{
NSMutableDictionary *theRealDictionary;
}
/* reimplement whatever init methods you want to use */
- (id)valueForKey:(id)key;
@end
...
@implementation MyFakeDictionary
- (id)valueForKey:(id)key
{
id val = [theRealDictionary objectForKey:key];
/* etc, as you like */
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return theRealDictionary;
}
@end
So there's no inheritance relationship between MyFakeDictionary and NSMutableDictionary, but if you send a selector to MyFakeDictionary that it doesn't understand (like any of the normal dictionary messages other than the one you've replaced), NSObject's built-in logic will redirect them to the member theRealDictionary. It's 'has a' rather than 'is a', with transparent message forwarding, and therefore conveniently dodges any issues with class clusters.