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
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
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
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
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
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
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
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
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
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
10 matches
Mail list logo