• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Know about Mutability
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Know about Mutability


  • Subject: Re: Know about Mutability
  • From: James Bucanek <email@hidden>
  • Date: Fri, 14 Jan 2005 09:06:47 -0700

Paul Sargent wrote on Friday, January 14, 2005:

>Is there a good reason that NSDictionary inherits from
>NSMutableDictionary, rather than the other way round?

I don't have a psychic link with the original authors of the NS classes, but I have often been in a situation where it is easier and more elegant to create a "full-featured" base class, then override specific behaviors on the subclass -- even when the subclass is "logically" simpler than the superclass.

I run into this all the time with tree structures.  You have two node classes: A root node and a child node.  You'd think that the "simpler" class would be the root node (that has children, but doesn't have a parent), and the child node would extend root node (by adding a 'parent' property).  But it almost always turns out to be easier to make a generic child/root object (often adding an 'isRoot' property), then subclass it to enforce the root node behavior.

Evan Schoenberg wrote on Friday, January 14, 2005:
>
>On Jan 14, 2005, at 1:05 AM, Hugh Hoover wrote:
>
>> I'm confused about why respondsToSelector: isn't working correctly...
>> Either the object will handle the selector (and respondsToSelector:
>> returns true) or it doesn't and respondsTo returns false?
>
>Note that when you send the setObject:forKey: selector to it, it
>responds with an exception that a mutating method was sent to an
>immutable object.  To be posting such a specialized exception, it must
>be handling the selector and taking action... it's not responding in
>the way you would like, but it -is- responding.

OK, getting back to the issue of [myDict isMutable], then what's stopping one from doing something like this (pseudo-code):

    + (NSMutableDictionary*)forceObject:(NSObject*)object intoDictionary:(NSDictionary*)originalDict
    {
        @try
            [originalDict addObject:object];
            return ((NSMutabloDictionary*)origiinalDict);
        @catch
            NSMutableDictionary* newDict = [[[NSMutableDictionary alloc] initWithDictionary:originalDict] autorelease];
            [newDict addObject:object];
            return (newDict);
        @end
    }

--
James Bucanek <mailto:email@hidden>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: Know about Mutability (From: Paul Sargent <email@hidden>)

  • Prev by Date: Re: Know about Mutability
  • Next by Date: menuItem connection to other nib
  • Previous by thread: Re: Know about Mutability
  • Next by thread: Re: Know about Mutability
  • Index(es):
    • Date
    • Thread