• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problem with protocols using gcc 4
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with protocols using gcc 4


  • Subject: Re: Problem with protocols using gcc 4
  • From: Kyle Hammond <email@hidden>
  • Date: Wed, 22 Jun 2005 10:36:41 -0500

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

On 6/21/05, j o a r <email@hidden> wrote:


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

---------------------

Kyle Hammond

email@hidden


<http://www.snowmintcs.com/> - personal productivity software


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Problem with protocols using gcc 4 (From: Kyle Hammond <email@hidden>)
 >Re: Problem with protocols using gcc 4 (From: j o a r <email@hidden>)
 >Re: Problem with protocols using gcc 4 (From: The Karl Adam <email@hidden>)

  • Prev by Date: NSURLConnection leaks
  • Next by Date: No binding for plain old string in NSTextView?
  • Previous by thread: Re: Problem with protocols using gcc 4
  • Next by thread: Selecting an input method from Cocoa
  • Index(es):
    • Date
    • Thread