I am trying to implement a subclass of NSMutableDictionary that returns nil instead of throwing a NSUndefinedKeyException when the key is not present in the Dictionary.
However when I try to add objects to my dictionary I get
[NSMutableDictionary setObject:forKey:]: method only defined for abstract class
NilDictionary.h
@interface NilDictionary : NSMutableDictionary {
}
@end
NilDctionary.m
@implementation NilDictionary
- (id)valueForUndefinedKey:(NSString *)key {
return nil;
}
@end
Do I really have to implement all the methods from NSMutableDictionary again in my subclass or is there some other class I should be subclassing?
Clarification: My original problem came down to me not being able to read the documentation properly.
If you need to subclass NSMutableDictionary check out the correct answer. If you want a dictionary that returns nil when your key is not present, NSMutableDictionary does that already.
NSUndefinedKeyException
? Trying to access an invalid key in a dictionary automatically returnsnil
anyways; how are you getting this error? – Branchiopod