On Mar 12, 2013, at 12:31 AM, Dave <d...@looktowindward.com> wrote: > On 11 Mar 2013, at 21:45, John McCall wrote: >> On Mar 11, 2013, at 2:02 PM, Dave <d...@looktowindward.com> wrote: >>> On 11 Mar 2013, at 20:53, John McCall wrote: >>>> On Mar 11, 2013, at 1:33 PM, Dave <d...@looktowindward.com> wrote: >>>>> On 11 Mar 2013, at 20:26, Mike Abdullah wrote: >>>>>>> >>>>>>> I had assumed (and I thought I'd done something like this before) that >>>>>>> the: >>>>>>> >>>>>>> myDict = [[super class] newDict]; >>>>>>> >>>>>>> statement would call newDict in BaseClass???? Instead it calls the >>>>>>> version in NewClass and goes into an infinite loop!! >>>>>> >>>>>> Yes. [super class] calls super's implementation of the -class method. >>>>>> You haven't overridden -class, so it does the same thing as [self class]. >>>>>> >>>>>> People often make the same mistake in trying to do [super >>>>>> respondsToSelector… >>>>>> >>>>>> I'm guessing what you're really after is [[self superclass] newDict] >>>>>> >>>>> >>>>> Thanks a Million, yes that's what I wanted! >>>> >>>> Are you sure? This will indeed call the superclass's 'newDict', but the >>>> 'self' object will be the superclass, not your class. That means it'll >>>> (almost certainly) create an instance of your class's superclass. The >>>> easier way to do this is just [Foo newDict], where Foo is the name of your >>>> superclass. >>>> >>>> If you want to invoke your superclass method, but with your class as >>>> 'self' — i.e. if you want to create an instance of your class — you should >>>> use [super newDict]. >>> >>> I don't know what you mean by: >>> >>>> i.e. if you want to create an instance of your class >>> >>> I don't want to create an instance of my class, I want the class to return >>> a dictionary, that gets things added from the current class down through >>> the inheritance hierarchy, >>> >>> BaseClass alloc's a dict and puts in some data, >>> Subclass1 calls base class and it's it own data. >>> Subclass2 calls subclass1. >>> >>> How is this creating an instance of any object except the NSDict? >> >> Where do you think Subclass1's "own data" goes if the object allocated is >> actually a BaseClass? > > I'm not allocating a base class or any other kind of class except a > dictionary. Did I say anything about instance data? No. The data I mean is > NOT instance data, how could it be? There is no object allocated in this > example (except the dictionary). I am using class + methods and they work on > the Class,
You seem to be assuming that this works the way it does in C# or Java. It does not. To use your example: > BaseClass (Class not an instance of a class). > +(MSMutableDictionary*) newDict > { > MSMutableDictionary* myDict; > > myDict = [[MSMutableDictionary alloc] init]; > [myDIct setObect:@"SomeaValueX" forKey:@"someKeyX"]; > > return myDict; > } > > Subclass1: > > +(MSMutableDictionary*) newDict > { > MSMutableDictionary* myDict; > > myDict = [super newDict]; > [myDIct setObect:@"SomeValueY" forKey:@"someKeyY"]; > > return myDict; > } > > > Subclass2: > > +(MSMutableDictionary*) newDict > { > MSMutableDictionary* myDict; > > myDict = [super newDict]; There is exactly one difference between [[self superclass] newDict] and [super newDict] here: the value of 'self'. (This assumes the obvious behavior for +superclass, of course.) [[self superclass] newDict] is exactly equivalent to [Subclass1 newDict]. [super newDict] calls the same method as [Subclass1 newDict], but using a 'self' value that is still the Subclass1 class. Now, if everything is implemented the way you've quoted it, the value of 'self' doesn't matter, because +[Subclass1 newDict] just passes it on to +[BaseClass newDict], and +[BaseClass newDict] ignores 'self'. However, that wouldn't be an idiomatic implementation. The usual expectation is that allocating methods, like +new methods, construct an object *of type self* (at least). For example, NSObject has a +new method that looks like this: + (id) new { return [[self alloc] init]; } This way, subclasses don't have to reimplement +new in order to make it work: [NSWindow new] eventually does an [[NSWindow alloc] init], just like you'd expect. This is why I was talking about instance methods and fields: because, generally, people introduce new classes because they expect to create instances of them, and [[self superclass] newDict] bypasses that in a way that might not be obvious. What you're doing is introducing a ton of classes in order to create a delegation hierarchy of factory methods. It's probably the most wasteful way of achieving this goal that I can see — why not just use functions or something? — but you're certainly welcome to it. Just understand that most people, reading code like +[FooDictionary newDict], would reasonably assume that this actually creates an instance of FooDictionary. John. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com