On Jun 6, 2015, at 2:35 PM, Cosmo <[email protected]> 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 protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to