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
@optional
-(BOOL)optionalMethodToImplement;
@end

Now, I don't want the superclass to implement it, so when it comes to the actual method that needs to be extended, I do the following:
-(BOOL)checkMethod
{
... stuff ...
if ([self conformsToProtocol:@protocol(Check)])
        [self optionalMethodToImplement];
}

Problem #1: The compiler complains that ClassA may not respond to optionalMethodToImplement. Then I have:

@interface ClassB : ClassA
{
...
}
...
@end

@interface ClassB (private) <Check>
-(BOOL)optionalMethodToImplement;
@end

Problem #2: The compiler cannot find the protocol in the superclass.

I realise I could do the same thing by having a dummy method in the superclass, and override that in the subclass. And I'm about to head down that path; but it seems like there should be a way to do this with protocols. Thanks in advance.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to