Re: Using performSelector: on super
Re: Using performSelector: on super
- Subject: Re: Using performSelector: on super
- From: "Clark Cox" <email@hidden>
- Date: Tue, 5 Aug 2008 06:38:07 -0700
On Tue, Aug 5, 2008 at 5:41 AM, email@hidden
<email@hidden> wrote:
> Hello list
>
> My superclass (SuperSocket) provides a private method -close.
> My subclass overrides this private method as follows:
>
> - (void)close
> {
> SEL closeSelector = @selector(close);
Note, for getting the selector that the current method was called
with, you can just use "_cmd":
>
> if ([SuperSocket instancesRespondToSelector:closeSelector]) {
> [super performSelector:closeSelector]; // seems to send to
> self
> } else {
> NSAssert (NO, @"SuperSocket no longer responds to close
> message.");
> }
>
> // subclass additional code
> }
>
> If I am not mistaken this code appears to send the close selector to the
> self object.
Correct.
> If [super performSelector:closeSelector] is replaced by [super close] then
> the super object receives the close message as expected.
Remember, there is no such thing as a "super object". "super" is just
a keyword that tells the compiler to skip searching the current class
for methods. It doesn't change which object the message is sent to.
> I know that [super respondsToSelector: closeSelector] is equivalent to [self
> respondsToSelector: closeSelector] as the methods tests the object as a
> whole (see NSObject Protocol Reference entry for respondsToSelector;);
> So is that the story here?
Correct.
> Or am I on the wrong page entirely.
If you *really* need to call [super close] without hard-coding the
message (i.e. using an arbitrary selector), you can get the method's
IMP and call that like a function:
void (*functionPointer)(id,SEL,...) = [SuperSocket
instanceMethodForSelector: _cmd];
functionPointer(self,_cmd);
--
Clark S. Cox III
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