Re: Using performSelector: on super
Re: Using performSelector: on super
- Subject: Re: Using performSelector: on super
- From: Negm-Awad Amin <email@hidden>
- Date: Wed, 6 Aug 2008 10:49:30 +0200
Am Di,05.08.2008 um 22:20 schrieb Andy Lee:
On Aug 5, 2008, at 3:39 PM, I. Savant wrote:
I'm going to make a bold statement: You should only ever call [super
someMethod] from within an overridden someMethod implementation. I
believe this is true in all cases (because I can't at the moment
imagine a case where you'd do otherwise). Third party opinions are
welcome.
The one case that comes to mind is when a designated initializer
calls its superclass's different designated initializer.
--Andy
Jepp, because of some reasons:
- You should override the DI of the base class, if you define
additional ivars in the subclass.
- You should call the base' class DI in the subclass' DI.
- You should call the subclass' DI in the subclass' non-DI.
If super would be dispatched dynamically, this would lead to an
infinite loop. Example:
Superclass NSObject, DI: -init
Baseclass: Instrument, DI: -initWithName:
-init is overriden to call the DI -initWithName
Subclass: Piano, DI (still) -initWithName:
-initWithName is overriden for some additional ivars (i.e. keyCount)
Code:
Piano* piano = [[[Piano alloc] init] autorelease];
will call -init (Instrument)
-init (Instrument) uses dynamic dispatched self and calls -
initWithName: (Piano) (That's correct to give Piano the chance to init
its ivars.)
-initWithName: (Piano) will call supers -initWithName: (Instrument).
This would be done correct, even it is dispacthed dynamivally. The
instance has the class Piano (look at the alloc) so even the "run-
times super" is Instrument. Everything nice.
-initWithName: (Instrument) send the -init-Message to its super. There
is a Problem:
** Dispacthing statically (as it is done), the compiler performs a
hard-wired call to NSObjects -init. That's correct and the intended
behaviour.
** Dispatching this dynamically the run-time-system would send this
message to the superclass of the instance. We have a Piano-instance
(look at the alloc). The superclass of Piano is Instrument. So,
dynamically dispatched, -init (INstrument) and *not* -init (NSObject)
would be called. That''s the point we started: inifinite loop-
You can imagine such situations outside initialization. Buit it is a
typical init-problem.
Amin
_______________________________________________
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
Amin Negm-Awad
email@hidden
_______________________________________________
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