Re: Extending NSMutableDictionary what am I doing wrong?
Re: Extending NSMutableDictionary what am I doing wrong?
- Subject: Re: Extending NSMutableDictionary what am I doing wrong?
- From: "Louis C. Sacha" <email@hidden>
- Date: Fri, 27 Feb 2004 14:22:59 -0800
Hello...
I was trying to point out two things in my previous email, but it got
really long, and then didn't come out quite right after I tried to
edit it to make it a bit shorter :) As Ben pointed out, the
"composite object" example is different from what I was trying to
suggest with my example for your Page class. The difference between
the two has to do with how you want your class to work: do you want
an NSMutableDictionary type object to be used as a dictionary, or are
you making a class that uses an NSMutableDictionary as its storage...
If you basically want a class that works like an NSMutableDictionary,
with just the title and text methods added to it, the composite
object type class that subclasses NSMutableDictionary would be the
way to go. Since it would be a true subclass of NSMutableDictionary,
you would only need to implement the primitive methods for yourself
(as Stephan has said). All the other methods call those primitive
methods to do the work in their implementations, so they would invoke
the modified versions in your subclass and be redirected to the
embedded dictionary whenever it is needed.
The main point that I was trying to get across with my example of
your Page object (subclassed from NSObject) in my other email was
that you might not want to actually subclass NSDictionary to
implement this. Of course, I don't know exactly what you plan to do
with your class, but "encapsulation" would be the better solution if
you are mainly planning to use the NSMutableDictionary as storage for
your class. If the main way you interact with a Page instance is
through methods that aren't methods defined in NSDictionary or
NSMutableDictionary, then there really isn't a compelling reason to
make your Page class a subclass of NSDictionary. In other words, if
you will mostly be calling methods like the title and text methods
in my example code of your Page class, which would access information
stored in the real dictionary using specific keys, using a NSObject
subclass would be just as effective.
It all depends on how you plan to use your class. If the statement "a
Page is a dictionary" describes what you are trying to do more
accurately than "a Page has a title, text etc...", then you probably
want a subclass of NSMutableDictionary. On the other hand, if the "a
Page has a title, text etc..." statement is more accurate, you would
probably want to just make a subclass of NSObject that uses a
dictionary as storage.
Hope that helps make what I was trying to say a bit clearer...
Louis
In the example the Louise C. Sacha give (extending from a NSObject)
it is true you would have to write pass through methods but if you
extend from the NSDictionary you only need to implement the
primitive methods as all the other methods use them for there
functionality(not including creation methods). This is because it
is only the primitive method which access the instance variables.
Stefan
On 27 Feb 2004, at 13:29, Ben Dougall wrote:
On Friday, February 27, 2004, at 12:29 pm, Stefan Pantos wrote:
Hi,
As I under it you don't have to implement all the super classes
methods only the primitive ones. Which in the case of NSDictionary
are {- (unsigned)count; - (NSEnumerator *)keyEnumerator; -
(id)objectForKey:(id)aKey;} And any creation methods which you
want to be available. Is this correct?
So in the example for MonthArray at
http://developer.apple.com/documentation/Cocoa/Conceptual/Foundation/
Concepts/ClassClusters.html#//apple_ref/doc/uid/20000262 you could
call say initWithContentsOfFile: but it wouldn't necessarily
work? There is no real way of know if a method is primitive or
not till you try it unless they structure the interface is a way
to make it > obvious?
also just to say that the primitive aspect you point out is i think
irrelevant with the type of class cluster subclass that Louis C.
Sacha was talking about and i was commenting on -- that is the
example titled "A Composite Object: An Example" below that month
array example on that page. that's irrelevant i think, because
with that composite type you basically have a normal dictionary
instance wrapped in your object (that also is a sublclass of the
dictionary abstract class). and to enable access to the dictionary
you have encased in your class you need to provide pass through
methods regardless of primitive or non-primitive.
_______________________________________________
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.
_______________________________________________
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.