Skip navigation.
 
mlRe: How to adopt a superclass's protocol?
FROM : Michael Vannorsdel
DATE : Wed Apr 30 00:00:38 2008

Sounds like you're getting into a mess of includes.  I know you'd like 
to use a protocol but honestly the cleanest way to do this is not to 
use a protocol but implement the optional methods in the subclasses 
without listing them in the header.  Then in the superclass method 
just check if it responds to that selector and call it with 
performSelector (or objc_msgSend if multiple args; the compiler turns 
messages into this anyways).  The compiler won't complain and the 
runtime will handle everything more cleanly.  Protocols are mostly for 
requiring methods to be implemented by a class so a calling object can 
be sure all the methods needed are there.  Protocols can lead to a 
mess when you want some methods implemented, some not, and hidden 
methods.


On Apr 29, 2008, at 2: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?

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