You are currently looking at an older section of the wincent.dev website.
Please check the new version of the site at https://wincent.dev/ for updated content.

wincent knowledge base

« Unit testing guidelines | Main | Eliminating "X-Authentication-Warning" headers from Squirrelmail »

March 01, 2006

Avoiding protocol-related warnings

By default, GCC warns when a subclass is marked as adopting a protocol and it inherits protocol methods from its superclass rather than implementing them itself.

If you have code that looks like this (showing the header content only, not the implementation):

@protocol MyProtocol

- method;
@end
@interface MySuperclass
- method;
@end
@interface MySubclass : MySuperclass <MyProtocol>
@end

GCC will generate a warning at compile time that MyClass seems to be missing a method implementation required by MyProtocol. You can suppress these warnings using the -Wno-protocol switch; from the GCC man page:

-Wno-protocol
If a class is declared to implement a protocol, a warning is issued
for every method in the protocol that is not implemented by the
class.  The default behavior is to issue a warning for every method
not explicitly implemented in the class, even if a method implemen-
tation is inherited from the superclass.  If you use the -Wno-pro-
tocol option, then methods inherited from the superclass are con-
sidered to be implemented, and no warning is issued for them.

There are reasons why you might actually want to see these warnings which I won't go into here (for more info check the objc-language mailing list archives), but if you're sure that you want to be rid of them, then the -Wno-protocol switch is for you. It certainly beats declaring a bunch of methods like this in your subclass:

@implementation MySubclass

- method { return [super method]; }
@end

And in many cases it is better than the alternative of dumping the formal protocol and opting for an informal one (defined as a category in a parent or root class) instead.

Posted by wincent at March 1, 2006 02:16 PM