Re: Forwarding messages to another class
Re: Forwarding messages to another class
- Subject: Re: Forwarding messages to another class
- From: Kyle Sluder <email@hidden>
- Date: Sat, 06 Jun 2015 18:08:03 -0700
On Jun 6, 2015, at 2:35 PM, Cosmo <email@hidden> wrote:
>
> I’m trying to send messages from a class to one of its subclasses. I have a method that returns the class I want to forward to. If I use it in the following manner, it works:
>
> + (void)logout
> {
> // subclasses handle it
> [[self classToUseForBackend] logout];
> }
>
> By setting breakpoints in this method and the subclass I can see that the message goes where i expect.
>
> If I use it in the following manner, the message is not forwarded to the subclass with the identical signature. It ends up being re-sent to the superclass method (i.e. where I’m calling it here) and I get an infinite loop.
>
> + (NSString *)errorMessageForCode:(NSInteger)errorCode
> {
> // subclasses handle it
> NSString *msg = [[self classToUseForBackend]
Objective-C uses purely dynamic dispatch. `self` always refers to the instance the message is being sent to, regardless of what class owns the method implementation that happens to be sending the message. So [self classToUseForBackend] will *always* invoke the most-specific (“most-overridden”) implementation of that method.
--Kyle Sluder
_______________________________________________
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