Objective-C 重写 NSMutableDictionary 的 valueForKey:

发布于 2024-11-03 00:57:29 字数 378 浏览 0 评论 0原文

我需要重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陪你搞怪i 2024-11-10 00:57:29

您可能不想覆盖 valueForKey,因为 NSDictionaryNSMutableDictionary 是隐藏在其后面的类簇的抽象基类。根据他们的文档,在直接子类化中,您需要重新实现所有以下内容:

  • setObject:forKey:
  • removeObjectForKey:
  • count
  • objectForKey:
  • keyEnumerator

实现一个假装是 NSMutableDictionary 但将其不理解的任何调用转发给的替代类会更容易一个实际的 NSMutableDictionary。您可以通过 forwardingTargetForSelector:

例如

@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

,MyFakeDictionary 和 NSMutableDictionary 之间没有继承关系,但是如果您向 MyFakeDictionary 发送一个它无法理解的选择器(就像除了您替换的消息之外的任何普通字典消息一样),NSObject 的内置逻辑将重定向将它们传递给成员 theRealDictionary。它是“has a”而不是“is a”,具有透明的消息转发,因此可以方便地避免类集群的任何问题。

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.

还不是爱你 2024-11-10 00:57:29

在子类化 NSMutableDictionary: 时必须注意

Apple 文档:

Methods to Override

In a subclass, you must override both
of its primitive methods:

setObject:forKey: 
removeObjectForKey:
You must also override the primitive methods of the NSDictionary class.

You must pay attention when subclassing NSMutableDictionary:

from the Apple docs:

Methods to Override

In a subclass, you must override both
of its primitive methods:

setObject:forKey: 
removeObjectForKey:
You must also override the primitive methods of the NSDictionary class.
和影子一齐双人舞 2024-11-10 00:57:29

获取您的代码并将其包装在以下内容中:

@implementation NSMutableDictionary(myValueForKey)

<your code here>

@end

这是 NSMutableDictionary 上的一个类别。类别可以轻松添加或扩充标准方法,而无需子类化。

不过,一个建议是使用不同的方法名称,因为您不想调用 [super valueForKey:...] - 您需要调用 [self valueForKey :...]。因此,为您的方法使用不同的方法名称将使您的方法与标准方法区分开来。

Take your code and wrap it in the following:

@implementation NSMutableDictionary(myValueForKey)

<your code here>

@end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文