I have a dictionary that I read from a plist. I want to create a subclass of NSDictionary to implement something like the following, so that I can avoid using @"key name" everywhere in my source code:
@interface MyDict{
}
-(NSString*) textString;
@end
@implementation MyDict
-(NSString*) textString {
return [self objectForKey:@"textString"];
}
@end
In my other method:
MyDict *d = ... // something i read from plist
NSString *str = [d textString];
When I call the method, the app crashes because of “unrecognized selector textString”. What is wrong here?
Your class has no superclass. Also, the conventional wisdom is that it’s very difficult to subclass
NSDictionarybecause it is an class cluster. You don’t actually get anNSDictionaryback when you:You get a private subclass (
NSCFDictionaryin this case).You might want to try defining your own dictionary keys, the way Apple does: