Hi,
I'm having a problem with protocol usage when compiling Objective-C code with gcc 4 (in Xcode 2.1). This same code compiles with no warnings with gcc 3.3. It works at run time no matter what I compile with, but I'd like to remove the warnings. Is this a compiler bug, or am I not using protocols correctly?
With gcc 4, I get warnings about a method having a different declared type from the header file to the implementation file, even though they're clearly identical in the source. Here's the warnings I get:
In function '-[LookupClass stringForObject:]': warning: conflicting types for '-(NSString *)stringForObject:(MyClass <MyProtocol> *)inObj' warning: previous declaration of '-(NSString *)stringForObject:(MyClass *)inObj'
And here's the code.
In the header file (LookupClass.h):
#import "MyClass.h" #import "MyProtocol.h"
@interface LookupClass
- (NSString *)stringForObject:(MyClass <MyProtocol> *)inObj;
@end
In the implementation file (LookupClass.m):
@implementation LookupClass
- (NSString *)stringForObject:(MyClass <MyProtocol> *)inObj { ... code to find the right string goes here }
@end
I declare a function prototype to take one parameter, an object of type MyClass that also supports the MyProtocol protocol. Not all objects of MyClass support MyProtocol (so I can't put MyProtocol in the MyClass definition), but the specific sub-classes of MyClass that this method accepts do indeed fully support MyProtocol.
The other problem is that -stringForObject: needs to call some methods of MyClass, so I don't want to just replace "MyClass" with "id". I have tried doing just that, and then using typecasts when calling MyClass methods of inObj to keep the compiler happy. That does work, but it seems like such a hack that I don't really want to keep it that way.
I suppose I could subclass MyClass, and have a "MyClassThatSupportsMyProtocol" class that declares support of MyProtocol. Then the classes that support the protocol, that currently directly subclass MyClass could instead be subclasses of MyClassThatSupportsMyProtocol. Putting in an extra class that does absolutely nothing except declare support for the protocol also seems strange, but it would probably keep the compiler happy.
Thanks for any help with this matter.
Kyle
|