Re: Forwarding messages to another class
Re: Forwarding messages to another class
- Subject: Re: Forwarding messages to another class
- From: Cosmo <email@hidden>
- Date: Sat, 06 Jun 2015 15:47:08 -0700
Thanks for the response.
> On Jun 6, 2015, at 3:15 PM, Quincey Morris <email@hidden> wrote:
>
> On Jun 6, 2015, at 14:35 , Cosmo <email@hidden> wrote:
>>
>> Can somebody explain to me why I’m getting this different behavior. Is there anything I can do to achieve my goal?
>
> The most likely immediate reason is that the class returned by '[self classToUseForBackend]’ doesn’t actually implement a method called ‘errorMessageForCode:’. If you think it does, check the spelling and capitalization of the method name in the subclass.
>
> However, the code fragments you show here don’t make any sense. Class methods in Obj-C (methods with a ‘+’) have inheritance like instance methods. So, if a subclass implements (say) ‘logout’, execution is never going to reach the superclass implementation. If a subclass *doesn’t* implement (say) ‘errorMessageForCode:’, you’re going to get an infinite loop.
>
The first code example I included definitely works. And I copied and pasted the method signatures from the superclass to the subclass, so there should not have been any differences. (I did it a second time for that second example to make sure that wasn’t the source of the problem.)
> Furthermore, if the subclass method happens to call the ‘super’ method, then you will again end up with an infinite loop.
>
> If your intention is to have a class hierarchy where the base class defines methods that the subclass must implement — that is, where the base class has abstract methods — there are two straightforward ways:
>
> 1. Define the base class method but don’t do anything in it, except possibly to cause an exception:
>
> + (void) logout {
> NSAssert (NO, @“Subclass responsibility”);
> }
>
> 2. Use a protocol instead of a base class:
>
> @protocol BaseClassProtocol
> + (void) logout;
> @end
>
I should have explained that I’m calling these methods on instances of the superclass, not the subclass, so inheritance doesn’t work. I am trying create structures with a high-level object that my app knows about, which can call through to decoupled objects written for specific database backends. I suppose they don’t necessarily need to be subclasses, and that protocols might be a better solution.
In searching for an explanation before asking my question on this list I found a few discussions of message forwarding in Objective-C. I saw examples of forwarding messages with a return type and no parameters, and no return type with a single parameter. But none included anything about forwarding a message with both a return type and a parameter. I didn’t see anything saying this cannot be done, so I thought there might be somebody on this list who’d know more about that and could give me a definitive answer. For now, my totally baseless guess is that Objective-C does not support that combination.
In any event I realized that I could pass in a block and accomplish what I want successfully, so I can move on. But I will consider changing this to use protocols instead.
_______________________________________________
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