Re: Another Gnarly Objective-C Question!
Re: Another Gnarly Objective-C Question!
- Subject: Re: Another Gnarly Objective-C Question!
- From: Graham Cox <email@hidden>
- Date: Tue, 12 Mar 2013 20:08:42 +1100
On 12/03/2013, at 7:39 PM, Dave <email@hidden> wrote:
> Hi All,
>
> Thanks for all help on the last question, here is one on a similar vein:
>
> This is all Class methods, no instances are allocated or intended to be allocated at this stage.
>
> in + Method in a subclass:
>
> [[self class] someMethod];
There's no reason to do this in a +method, just call [self someMethod];
However, it's equivalent because in a +method, [self class] simply returns self.
> This calls +someMethod in the current class, if it exists, if not, it calls in one of the super-classes if it exists. Is this correct?
Yes, like any method of any class, whether an instance method or a class method.
> If one of the super classes calls [[self class] someMethod]; It will call as high up the chain as possible, resulting in an infinite loop in this case?
It will enter an infinite loop if it calls itself, like any function anywhere (that doesn't terminate under some condition). It's got nothing to do with how high in the chain, or whatever.
+ (void) method
{
[[self class] method]; // stack overflow due to infinite recursion because this is identical to [self method]
}
in a + method, [self class] === self. Once you've got that, you've got it.
You're overthinking this.
A class method is just an instance method of the class object. No magic at all. So all this confusion you've caused yourself about [super class] and so on is wholly unnecessary to correctly use class methods.
[super class] is a useless construction because +class returns self. That means that [super class] is no different from [self class] is no different from self. It's reasonable to call [super sameMethod] in a class method and it will do exactly what it does for an instance method (because it IS an instance method of the class object). It will call super's implementation. In general that's all you need - deeper introspection in class methods just to do ordinary kinds of work (like your dictionary case) are not required.
--Graham
_______________________________________________
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