Re: exception in class derived from NSMutableDictionary
Re: exception in class derived from NSMutableDictionary
- Subject: Re: exception in class derived from NSMutableDictionary
- From: Philippe Mougin <email@hidden>
- Date: Thu, 31 Jul 2003 01:23:27 +0200
Robert,
Your problem comes from the fact that you haven't correctly subclassed
NSMutableDictionary.
You must take into account the fact that NSMutableDictionary is a class
cluster (i.e., an abstract class providing no storage and only some
methods, but not all). You have to provide storage (i.e., manage the
internal state of the object) and to implement a set of so called
"primitives methods" in your subclass. Here is the list of the
primitive methods of NSMutableDictionary, extracted from the
documentation: count, objectForKey:, keyEnumerator:, setObjectForKey:,
removeObjectForKey:.
This may surprise you to learn that NSMutableDictionary is an abstract
class because, as you pointed, you are able to instantiate
NSMutableDictionary with [[NSMutableDictionary alloc] init]. But if you
look closer, you will see that this does not return a direct instance
of NSMutableDictionary, but an instance of a private concrete subclass
of NSMutableDictionary (a subclass which provides storage and
implements the primitives methods).
In order to master the subclassing of NSMutableDictionary (and many
other classes in Cocoa), you should read this:
http://developer.apple.com/documentation/Cocoa/Conceptual/Foundation/
Concepts/ClassClusters.html
Contrary to popular belief, subclassing a class cluster like
NSMutableDictionary is very easy and provides a correct way to
implement your own storage if needed, which is what we want from a top
notch object framework like Cocoa. In your case, look in details at the
section "A Composite Object" in the class cluster documentation.
Best,
Phil
> I have a class that I have derived from NSMutableDictionary -
> solely for the purpose of being able to add items using a custom
> method. If I execute either of the first two pieces of code, they
> work fine. If I execute the third, I get an exception on the count
> method with the following message:
>
> 2003-07-30 11:47:27.964 V360Locator[1766] An uncaught exception was
> raised
> 2003-07-30 11:47:27.965 V360Locator[1766] *** -count only defined for
> abstract class. Define -[VideoSensorCollection count]!
> 2003-07-30 11:47:27.965 V360Locator[1766] *** Uncaught exception:
> <NSInvalidArgumentException> *** -count only defined for abstract
> class. Define -[VideoSensorCollection count]!
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.