Using invocation forwarding

  • A while back I found out that subclassing a dictionary is a nightmare,
    because dictionaries are actually one of a number of private subclasses.
    Categories won't work, since I still need a couple of  additional variables.

    I was thinking that simply wrapping a dictionary in a class with a
    forwardInvocation method would allow me send any messages I don't understand
    on to the dictionary, while still having a few variables and a couple of
    specialized methods.  Are there reasons or situations where this would fail
    while a normal dictionary wouldn't?  Any advice or warnings?

    Thanks,

    JT
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • It really isn't that tough. Create a subclass of NSDictionary. Make an
    NSDictionary a member variable of your class. Provide impementations of
    "- (unsigned)count", "- (NSEnumerator *)keyEnumerator" and "-
    (id)objectForKey:(id)aKey" that merely call the relevent methods of the
    class's member dictionary.

    And that's it. All the other methods of NSDictionary are layered on top
    of those three primary methods. You don't need to fool about with
    forward invocations.

    If you want to subclass NSMutableDictionary do the same thing but
    provide implementations of "- (void)removeObjectForKey:(id)aKey" and "-
    (void)setObject:(id)anObject forKey:(id)aKey" too.

    See
    http://developer.apple.com/documentation/Cocoa/Conceptual/Foundation/
    Concepts/ClassClusters.html for more information (the example on that
    page shows you how to subclass NSArray).

    Jon.

    On Dec 16, 2003, at 11:17, John Timmer wrote:

    > A while back I found out that subclassing a dictionary is a nightmare,
    > because dictionaries are actually one of a number of private
    > subclasses.
    > Categories won't work, since I still need a couple of  additional
    > variables.
    >
    > I was thinking that simply wrapping a dictionary in a class with a
    > forwardInvocation method would allow me send any messages I don't
    > understand
    > on to the dictionary, while still having a few variables and a couple
    > of
    > specialized methods.  Are there reasons or situations where this would
    > fail
    > while a normal dictionary wouldn't?  Any advice or warnings?
    >
    > Thanks,
    >
    > JT
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.

    [demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month december 2003 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today