First, there is no "original method" with a prototype like - (NSString *)stringForObject:(MyClass *)inObj;
The only place that ever shows up is in the gcc 4 compiler warning.
The actual code looks like this below for both the prototype and the implementation. - (NSString *)stringForObject:(MyClass <MyProtocol> *)inObj;
It really looks to me like the compiler is simply dropping the "<MyProtocol>" part from the prototype in the header file. Does anybody know if the protocol syntax is supposed to be correct if applied to any parameter type, or only type "id"?
As I said originally, if I do this instead - (NSString *)stringForObject:(id <MyProtocol>)inObj;
then gcc 4 doesn't complain, but I have to use typecasts to avoid warnings when calling methods from "MyClass" on inObj.
joar's idea of declaring a subclass of MyClass that includes the MyProtocol will definitely work, but is that the way that it's supposed to be? Can protocols only be applied either to an entire class on the @interface line, or should it work as a parameter type also? Conceptually, it does make sense that protocols should be applied to a class as a whole then use type "id" with protocol(s) as parameters. I simply can't find any place that explicitly states that. And, since gcc 3.x supported protocols applied to any type as a parameter, I made use of that syntax.
On the other hand, it seems a bit silly to declare a new class simply to add protocol support with no actual implementation. I imagine I'll also have to add empty implementation of the protocol methods to avoid more warnings. My class hierarchy is going to look like:
MyClass - doesn't support MyProtocol |->Foo - subclass of MyClass that doesn't support MyProtocol |->MyClassWithMyProtocol - doesn't implement anything, but declares support for MyProtocol |->Bar - implements MyProtocol
There are several other classes both at the "Foo" level and at the "Bar" level. My original method that accepts any of the "Bar" classes was declared like this: - (NSString *)stringForObject:(MyClass <MyProtocol> *)inObj;
but will now be
- (NSString *)stringForObject:(MyClassWithMyProtocol *)inObj;
This will definitely remove the compiler warnings, but it just seems a odd that I have to have the "MyClassWithMyProtocol" class in there at all. Is this a compiler bug, or a fix that the compiler actually catches this usage pattern?
Thanks, Kyle On Jun 22, 2005, at 9:25 AM, The Karl Adam wrote: He could also use a specialized version of that same protocol since he seems to be specializing certain methods.
Something like:
@protocol MyProtocolSubclass <MyProtocol> - (NSString *)stringForObject:(MyClass <MyProtocol> *)inObj; @end
Only problem is that neither solution will really make the warnings go away if I understand the problem correctly. While both will work the compiler regards (MyClass *) and (MyClass <MyProtocol> *) as different types, as such you can't override one method with the other or fulfill that method without another.
If he goes with my method he'll get the warning that the original method is not implemented, and with joar's method he should get the same warning about a missing implementation for the original method -(NSString *)stringForObject:(MyClass *)inObj.
-Karl
Would something like this work?:
@interface Foo : NSObject < ProtoA > - (void) someMethod: (id < ProtoA, ProtoB >) obj; @end
@interface Bar : Foo < ProtoB > @end
Just make sure that the methods you need to call from "someMethod:" are declared in the ProtoA protocol.
j o a r
|