Objective-C 重写 NSMutableDictionary 的 valueForKey:
我需要重写 NSMutableDictionary 的 valueForKey: 方法。我想检查特定值,进行一些操作并返回它。有什么需要注意的地方吗?如果有不对的地方请指正,例如:
- (id)valueForKey:(NSString*)key {
id val = [super valueForKey:key];
if([val isKindOfClass:[NSString class]] && [val isEqualToString:@"<null>"]) {
return @"No Value";
}
else {
return val;
}
}
谢谢
I need to override the valueForKey: method of a NSMutableDictionary. I want to check for a specific value, do some manipulation and return it. Are there any points that should I be aware of ? Please correct me if there is something wrong, for example:
- (id)valueForKey:(NSString*)key {
id val = [super valueForKey:key];
if([val isKindOfClass:[NSString class]] && [val isEqualToString:@"<null>"]) {
return @"No Value";
}
else {
return val;
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能不想覆盖
valueForKey
,因为NSDictionary
和NSMutableDictionary
是隐藏在其后面的类簇的抽象基类。根据他们的文档,在直接子类化中,您需要重新实现所有以下内容:实现一个假装是 NSMutableDictionary 但将其不理解的任何调用转发给的替代类会更容易一个实际的 NSMutableDictionary。您可以通过 forwardingTargetForSelector:。
例如
,MyFakeDictionary 和 NSMutableDictionary 之间没有继承关系,但是如果您向 MyFakeDictionary 发送一个它无法理解的选择器(就像除了您替换的消息之外的任何普通字典消息一样),NSObject 的内置逻辑将重定向将它们传递给成员 theRealDictionary。它是“has a”而不是“is a”,具有透明的消息转发,因此可以方便地避免类集群的任何问题。
You probably don't want to override
valueForKey
, becauseNSDictionary
andNSMutableDictionary
are abstract base classes hiding a class cluster behind them. Per their documentation, in a straight subclassing you'd need to reimplement all of: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.
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.
在子类化 NSMutableDictionary: 时必须注意
Apple 文档:
You must pay attention when subclassing NSMutableDictionary:
from the Apple docs:
获取您的代码并将其包装在以下内容中:
这是 NSMutableDictionary 上的一个类别。类别可以轻松添加或扩充标准方法,而无需子类化。
不过,一个建议是使用不同的方法名称,因为您不想调用
[super valueForKey:...]
- 您需要调用[self valueForKey :...]
。因此,为您的方法使用不同的方法名称将使您的方法与标准方法区分开来。Take your code and wrap it in the following:
This is a category on NSMutableDictionary. Categories make it easy to add to or augment standard methods without having to subclass.
One recommendation would be to use a different method name, though, since you won't want to call
[super valueForKey:...]
-- you'll want to call[self valueForKey:...]
. So using a different method name for your method will differentiate yours from the standard method.