NSDictionary:仅为抽象类定义的方法.我的应用程序崩溃了 [英] NSDictionary: method only defined for abstract class. My app crashed

查看:13
本文介绍了NSDictionary:仅为抽象类定义的方法.我的应用程序崩溃了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用 addImageToQueue 后,我的应用程序崩溃了.我添加了 initWithObjects: forKeys: count: 但它对我没有帮助.

My app crashed after I called addImageToQueue. I added initWithObjects: forKeys: count: but it doesn't helped me.

Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '*** -[NSDictionary initWithObjects:forKeys:count:]: 
method only defined for abstract class.  
Define -[DictionaryWithTag initWithObjects:forKeys:count:]!'

我的代码

- (void)addImageToQueue:(NSDictionary *)dict
{
 DictionaryWithTag *dictTag = [DictionaryWithTag dictionaryWithDictionary:dict];
}

@interface DictionaryWithTag : NSDictionary
@property (nonatomic, assign) int tag;

- (id)initWithObjects:(id *)objects forKeys:(id *)keys count:(NSUInteger)count;

@end

@implementation DictionaryWithTag

@synthesize tag;

- (id)initWithObjects:(id *)objects forKeys:(id *)keys count:(NSUInteger)count
{
 return [super initWithObjects:objects forKeys:keys count:count];
}
@end

推荐答案

你在继承 NSDictionary 吗?这在 Cocoa-land 并不常见,这或许可以解释为什么您没有看到预期的结果.

Are you subclassing NSDictionary? That's not a common thing to do in Cocoa-land, which might explain why you're not seeing the results you expect.

NSDictionary 是一个类簇.这意味着您实际上永远不会使用 NSDictionary 的实例,而是使用它的私有子类之一.请在此处查看 Apple 对类集群的描述.从那个文档:

NSDictionary is a class cluster. That means that you never actually work with an instance of NSDictionary, but rather with one of its private subclasses. See Apple's description of a class cluster here. From that doc:

您可以像创建任何其他类一样创建集群实例并与之交互.但是,在幕后,当您创建公共类的实例时,该类会根据您调用的创建方法返回相应子类的对象.(您没有也不能选择实例的实际类.)

You create and interact with instances of the cluster just as you would any other class. Behind the scenes, though, when you create an instance of the public class, the class returns an object of the appropriate subclass based on the creation method that you invoke. (You don’t, and can’t, choose the actual class of the instance.)

你的错误信息告诉你的是,如果你想继承 NSDictionary,你必须为它实现你自己的后端存储(例如通过用 C 编写一个哈希表).它不仅要求您声明该方法,还要求您从头开始编写它,自己处理存储.这是因为像这样直接对类集群进行子类化与说您想为字典的工作方式提供新的实现是一样的.我敢肯定,这是一项重大的任务.

What your error message is telling you is that if you want to subclass NSDictionary, you have to implement your own backend storage for it (for example by writing a hash table in C). It's not just asking you to declare that method, it's asking you to write it from scratch, handling the storage yourself. That's because subclassing a class cluster directly like that is the same as saying you want to provide a new implementation for how dictionaries work. As I'm sure you can tell, that's a significant task.

假设你肯定想继承 NSDictionary,你最好的办法是编写你的子类来包含一个普通的 NSMutableDictionary 作为属性,并用它来处理你的存储.本教程向您展示了一种方法.这实际上并不难,您只需将所需的方法传递给您的字典属性.

Assuming you definitely want to subclass NSDictionary, your best bet is to write your subclass to contain a normal NSMutableDictionary as a property, and use that to handle your storage. This tutorial shows you one way to do that. That's not actually that hard, you just need to pass the required methods through to your dictionary property.

您也可以尝试使用关联引用,它模拟将对象实例变量添加到现有类".这样,您可以将 NSNumber 与现有字典关联来表示标签,并且不需要子类化.

You could also try using associative references, which "simulate the addition of object instance variables to an existing class". That way you could associate an NSNumber with your existing dictionary to represent the tag, and no subclassing is needed.

当然,您也可以只将 tag 作为字典中的键,并将值存储在其中,就像任何其他字典键一样.

Of course, you could also just have tag as a key in the dictionary, and store the value inside it like any other dictionary key.

这篇关于NSDictionary:仅为抽象类定义的方法.我的应用程序崩溃了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆