NSMutableDictionary 子类化

发布于 2024-10-14 10:13:38 字数 697 浏览 8 评论 0原文

我正在尝试实现 NSMutableDictionary 的子类,当字典中不存在该键时,它返回 nil 而不是抛出 NSUndefinedKeyException 。

但是,当我尝试将对象添加到字典中时,我得到

[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

我真的必须在我的子类中再次实现 NSMutableDictionary 中的所有方法吗?还是有其他一些我应该子类化的类?

澄清:我最初的问题归根结底是我无法正确阅读文档。

如果您需要子类化 NSMutableDictionary,请查看正确的答案。如果你想要一个在你的键不存在时返回 nil 的字典,NSMutableDictionary 已经做到了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

故乡的云 2024-10-21 10:13:38

NSMutableDictionary 类参考 说:

在子类中,您必须重写它的两个原始方法:

1. setObject:forKey:
2. removeObjectForKey:

您还必须重写NSDictionary类的原始方法。


NSDictionary 类参考 说:

如果您确实需要子类 NSDictionary,则需要考虑由 类簇 表示的子类 -因此,这些方法在概念上基于以下几种原始方法:

1.计数
2. objectForKey:
3. keyEnumerator
4. initWithObjects:forKeys:count:

在子类中,您必须重写所有这些方法。

NSDictionary 的其他方法通过调用这些原语中的一个或多个进行操作。非原始方法提供了一次访问多个条目的便捷方法。


看来您需要重写所有这六个方法才能使您的NSMutableDictionary子类完美工作。

NSMutableDictionary Class Reference says:

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

1. setObject:forKey:
2. removeObjectForKey:

You must also override the primitive methods of the NSDictionary class.


NSDictionary Class Reference says:

If you do need to subclass NSDictionary, you need to take into account that is represented by a Class cluster—there are therefore several primitive methods upon which the methods are conceptually based:

1. count
2. objectForKey:
3. keyEnumerator
4. initWithObjects:forKeys:count:

In a subclass, you must override all these methods.

NSDictionary’s other methods operate by invoking one or more of these primitives. The non-primitive methods provide convenient ways of accessing multiple entries at once.


It seems that you need to override all these six methods to make your NSMutableDictionary subclass work perfect.

凉世弥音 2024-10-21 10:13:38

这是你的问题。 NSDictionary (及其可变对应项)是类簇的一部分(了解有关它们的更多信息 此处,在“类簇”标题下),并且不应被子类化因为它会导致诸如您提到的问题(请阅读 NSDictionary 类参考)。无论您需要做什么,您都将有一种方法来扩展您想要使用的类,以便做您想做的事情。例如,上面的代码可以轻松地放置在一个类别中(阅读有关类别的更多信息此处)。

Here's your problem. NSDictionary (and its mutable counterpart) is part of a class cluster (read more about them here, under the 'Class Cluster' heading), and should not be subclassed because it causes problems such as what you've mentioned (read the subclassing notes in the NSDictionary Class Reference). Whatever it is you need to do, you're going to have a way to extend the classes you want to use in order to do what you want to do. For instance, the above code can easily be placed in a category (read more about categories here).

忘年祭陌 2024-10-21 10:13:38

您确定在为 KEY (不是值)传递“nil”时没有遇到异常吗?

Are you sure you are not getting the exception when passing in "nil" for a KEY (not a value)?

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