Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionary: method only defined for abstract class. My app crashed

Tags:

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 
like image 871
Voloda2 Avatar asked May 29 '12 12:05

Voloda2


1 Answers

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 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.)

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.

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.

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.

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.

like image 186
Amy Worrall Avatar answered Oct 13 '22 22:10

Amy Worrall