【发布时间】:2020-07-23 15:37:52
【问题描述】:
我尝试从NSMutableDictionary<NSString *,NSString *> 继承,以便使用不区分大小写的键实现字典。这是我的实现:
@interface NSMutableDictionaryLowerCase : NSMutableDictionary<NSString*, NSString *>
@end
@implementation NSMutableDictionaryLowerCase
-(NSString *)objectForKey:(NSString *)aKey {
return [[super objectForKey:aKey] lowercaseString];
}
@end
NSMutableDictionaryLowerCase * x = [NSMutableDictionaryLowerCase new];
[x setValue:@"XxX" forKey:@"YyY"];
NSLog(@" %@ %@ ", x[@"xxx"], x[@"XXX"])
但是,当它与以下代码一起使用时,它会在 setValue 上捕获异常并显示以下消息:
[NSMutableDictionary setObject:forKey:]: method only defined for abstract class. Define -[NSMutableDictionaryLowerCase setObject:forKey:]!'
我不明白,NSMutableDictionary 不应该是抽象类,我以为这个方法实现已经存在了......我做错了什么?
【问题讨论】:
标签: objective-c oop inheritance