简体   繁体   English

子类化NSMutableDictionary

[英]Subclassing 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. 我试图实现一个NSMutableDictionary的子类,它返回nil而不是在字典中不存在键时抛出NSUndefinedKeyException。

However when I try to add objects to my dictionary I get 但是,当我尝试将对象添加到我的字典中时,我得到了

[NSMutableDictionary setObject:forKey:]: method only defined for abstract class

NilDictionary.h NilDictionary.h

@interface NilDictionary : NSMutableDictionary {
} 
@end

NilDctionary.m 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? 我是否真的必须在我的子类中再次实现NSMutableDictionary中的所有方法,或者是否有其他类我应该进行子类化?

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. 如果你需要子类NSMutableDictionary,请检查正确的答案。 If you want a dictionary that returns nil when your key is not present, NSMutableDictionary does that already. 如果你想要一个在你的密钥不存在时返回nil的字典,NSMutableDictionary已经这样做了。

NSMutableDictionary Class Reference says: NSMutableDictionary类参考说:

In a subclass, you must override both of its primitive methods: 在子类中,您必须覆盖它们的两个基本方法:

1. setObject:forKey: 1. setObject:forKey:
2. removeObjectForKey: 2. removeObjectForKey:

You must also override the primitive methods of the NSDictionary class. 您还必须覆盖NSDictionary类的基本方法。


NSDictionary Class Reference says: NSDictionary类参考说:

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: 如果确实需要子类NSDictionary ,则需要考虑由Class cluster表示的 - 因此,这些方法在概念上基于几种原始方法:

1. count 数数
2. objectForKey: 2. objectForKey:
3. keyEnumerator 3. keyEnumerator
4. initWithObjects:forKeys:count: 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. NSDictionary's其他方法通过调用这些原语中的一个或多个来操作。 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. 您似乎需要覆盖所有这六个方法,以使您的NSMutableDictionary子类完美运行。

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 ). NSDictionary (及其可变对应物)是类集群的一部分( 在这里 ,在“类集群”标题下阅读更多关于它们的内容),并且不应该被子类化,因为它会导致诸如你所提到的问题(阅读子类注释)在NSDictionary类参考中 )。 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 ). 例如,上面的代码可以很容易地放在一个类别中( 在这里阅读更多关于类别的内容)。

当你传入“nil”代表KEY(不是值)时,你确定你没有得到异常吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM