Re: Objective-C Question
Re: Objective-C Question
- Subject: Re: Objective-C Question
- From: Dave <email@hidden>
- Date: Mon, 11 Mar 2013 21:02:57 +0000
On 11 Mar 2013, at 20:53, John McCall wrote:
On Mar 11, 2013, at 1:33 PM, Dave <email@hidden> 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?
I could just say [BaseClass newDict], but the reason I didn't is so
the code is not dependent on knowing the base class, e.g. it can move
position in the hierarchy and still call the correct class method.
Cheers
Dave
There isn't a
-newDict
method defined, so how come I didn't get a complier error?
[super class] returns Class, which works kindof like 'id' for
purposes of looking up methods, except only looking at class
methods (and instance methods from the root class).
So, I should use, [super class], not [self superclass].
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden