Skip navigation.
 
mlRe: How to adopt a superclass's protocol?
FROM : Andy Lee
DATE : Tue Apr 29 23:05:08 2008

Sorry to answer a question with a question, but will this really do 
what you want?

I see from the docs that the @optional keyword means the method is not 
required.  Doesn't that mean you can conform to the Check protocol 
without implementing -optionalMethodToImplement, which would mean that 
-conformsToProtocol: could give you a misleading answer?  (I would 
check this myself with some test code, but I don't have enough time 
right this second.)

If the above is correct, I suppose you could take out the @optional 
keyword, but personally, I prefer the category approach that Jens 
Alfke suggested rather than using protocols at all.  Then you don't 
have to change your class declaration in order to tell yourself that a 
given class implements a given method.  You can just ask the object 
*directly* whether it implements the method.  This makes it easier to 
change your mind about whether and where to implement -
optionalMethodToImplement, because you don't have to remember to 
change class declarations.

--Andy

On Apr 29, 2008, at 4:40 PM, K. Darcy Otto wrote:

> The casting worked, and the protocol gets found; but I'm still 
> getting a warning that the protocol is not found.  Here's what I have:
>
> Superclass.h:
>
> @protocol Check;
>
> Superclass.m:
>
> @protocol Check
>
> @optional
> -(BOOL)optionalMethodToImplement;
>
> @end
>
> (I'm relegating the protocol to the .m file to make it "private" in 
> a sense.)
>
> Subclass.h
>
> @interface Subclass : Superclass <Check>
>
> ...
>
> Now, the compiler issues a warning at the @interface line in 
> Subclass.h - "no definition of protocol 'Check' is found".  But it 
> is clear that the protocol is being located at runtime nevertheless, 
> because if I eliminate <Check> from the @interface line, 
> optionalMethodToImplement: does not get called.  Also, what is 
> interesting is that if I move @protocol Check ... @end in 
> Superclass.m to Superclass.h, the warning goes away.  So, is there a 
> way to keep @protocol Check ... @end in Superclass.m and eliminate 
> the warning?
>
> Thanks.
>
> On 28-Apr-08, at 11:25 PM, Michael Vannorsdel wrote:
>

>> You can make the superclass's method look like this:
>>
>> - (void)doSomething
>> {
>>     if([self conformsToProtocol:@protocol(Check)])
>>         [(SuperClass <Check> *)self optionalMethodToImplement];
>> }
>>
>> The cast eliminates the compiler warning.
>>
>> As far as making it private it depends what you mean by that.  If 
>> you don't want it visible in headers you're going to distribute you 
>> can put:
>>
>> @protocol Check;
>>
>> in the public header then actually define the whole protocol in a 
>> private undistributed header.
>>
>>
>> On Apr 28, 2008, at 9:22 PM, K. Darcy Otto wrote:
>>

>>> Okay, I have done this, and things are compiling and running 
>>> correctly.  Thank you.  Two additional questions then.  First, I 
>>> still get the warning that the superclass "may not respond" to the 
>>> method (and to be sure, it is only implemented in the subclass, 
>>> but the superclass calls it after a conformsToProtocol: check). 
>>> Second, I would like the optionalMethodToImplement to be private - 
>>> usually I put this in the .m file under a category; but when I do 
>>> that with the protocol, the subclass cannot locate the protocol. 
>>> Any suggestions?

>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (<email_removed>)
>>
>> 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:
>> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>>
>> This email sent to <email_removed>

>
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlHow to adopt a superclass's protocol? K. Darcy Otto Apr 29, 02:00
mlRe: How to adopt a superclass's protocol? Jens Alfke Apr 29, 02:16
mlRe: How to adopt a superclass's protocol? Michael Vannorsdel Apr 29, 02:32
mlRe: How to adopt a superclass's protocol? K. Darcy Otto Apr 29, 05:22
mlRe: How to adopt a superclass's protocol? Michael Vannorsdel Apr 29, 08:25
mlRe: How to adopt a superclass's protocol? Paul Sargent Apr 29, 10:51
mlRe: How to adopt a superclass's protocol? K. Darcy Otto Apr 29, 22:40
mlRe: How to adopt a superclass's protocol? Andy Lee Apr 29, 23:05
mlRe: How to adopt a superclass's protocol? Michael Vannorsdel Apr 30, 00:00
mlRe: How to adopt a superclass's protocol? Graham Cox Apr 30, 00:52