How to adopt a superclass's protocol?

2008-04-28 Thread K. Darcy Otto
I need to have a subclass optionally extend a method already in the superclass. After some research, my best guess is that an optionally defined protocol is the best way to go about this. So, what I have in the superclass is: @interface ClassA : NSObject { ... } ... @end @protocol Check

Re: How to adopt a superclass's protocol?

2008-04-28 Thread Jens Alfke
On 28 Apr '08, at 5:00 PM, K. Darcy Otto wrote: I need to have a subclass optionally extend a method already in the superclass. After some research, my best guess is that an optionally defined protocol is the best way to go about this. If you really want a fully abstract method, use a cat

Re: How to adopt a superclass's protocol?

2008-04-28 Thread Michael Vannorsdel
You can specify that a class adopts a protocol by defining it as: @interface ClassB : ClassA { ... } ... @end This will tell the compiler that ClassB implements the Check protocol. You can put multiple protocols by separating them with a comma: @interface ClassB : ClassA { ... } ... @e

Re: How to adopt a superclass's protocol?

2008-04-28 Thread K. Darcy Otto
Okay, I have done this, and things are compiling and running correctly. Thank you. Two additional questions then. First, I still get the warning that the superclass "may not respond" to the method (and to be sure, it is only implemented in the subclass, but the superclass calls it after

Re: How to adopt a superclass's protocol?

2008-04-28 Thread Michael Vannorsdel
You can make the superclass's method look like this: - (void)doSomething { if([self conformsToProtocol:@protocol(Check)]) [(SuperClass *)self optionalMethodToImplement]; } The cast eliminates the compiler warning. As far as making it private it depends what you mean by

Re: How to adopt a superclass's protocol?

2008-04-29 Thread Paul Sargent
On 29 Apr 2008, at 04:22, K. Darcy Otto wrote: First, I still get the warning that the superclass "may not respond" to the method (and to be sure, it is only implemented in the subclass, but the superclass calls it after a conformsToProtocol: check). Sounds like the way things a decompose

Re: How to adopt a superclass's protocol?

2008-04-29 Thread K. Darcy Otto
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 t

Re: How to adopt a superclass's protocol?

2008-04-29 Thread Andy Lee
Sorry to answer a question with a question, but will this really do what you want? I see from the docs that the @optional keyword means the method is not required. Doesn't that mean you can conform to the Check protocol without implementing -optionalMethodToImplement, which would mean that

Re: How to adopt a superclass's protocol?

2008-04-29 Thread Michael Vannorsdel
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

Re: How to adopt a superclass's protocol?

2008-04-29 Thread Graham Cox
This is what I'd expect. A protocol isn't much different from a class; if you want to subclass a class, you need to import its header. Same here. The concept of a "private protocol" is a bit of a contradiction in terms - protocols exist to allow more than one class to comply with a common