Re: Objective-C Question
Re: Objective-C Question
- Subject: Re: Objective-C Question
- From: Greg Parker <email@hidden>
- Date: Mon, 11 Mar 2013 14:29:44 -0700
On Mar 11, 2013, at 1:55 PM, Dave <email@hidden> wrote:
> Not sure I understand:
>
> if from an Instance - Method, I do:
>
> [super someMehod];
>
> It calls -someMehod in the superclass.
>
> if from an instance + Method I do:
>
> [super someMethod], then surely it's an error because this isn't an instance?
A class is an object. Inside a class method, `self` is a class object, and [super someMethod] sends a message to that class object, searching for a class method implemented by some superclass.
(If a class is an object, then the class object must be an instance of some other class, right? Right: http://sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html )
> So from a + method, what do:
>
> [super someMethod]; and
> [[self superclass] someMethod];
These are similar but not identical. In your [NewClass newDict] example:
* [super newDict] calls +[BaseClass newDict].
* [[self superclass] newDict] also calls +[BaseClass newDict].
The difference is which object the message is sent to.
* [super newDict] calls +[BaseClass newDict], and inside that method `self` is the class object NewClass.
* [[self superclass] newDict] calls +[BaseClass newDict], and inside that method `self` is the class object BaseClass. (Because that's what the [self superclass] returned).
The [[self superclass] newDict] version is inferior for various reasons. It will do unexpected things in some circumstances (for example, if NewClass itself has a subclass). It's more code to type, and runs slower, and can bypass some of the compiler's type checking. You should use [super newDict].
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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