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

查看:214
本文介绍了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:]!'



my code

- (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天全站免登陆