Re: How to adopt a superclass's protocol?
Re: How to adopt a superclass's protocol?
- Subject: Re: How to adopt a superclass's protocol?
- From: Jens Alfke <email@hidden>
- Date: Mon, 28 Apr 2008 17:16:57 -0700
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 category:
@interface ClassA : NSObject
{
...
}
...
@end
@interface ClassA(Check)
-(BOOL)optionalMethodToImplement;
@end
Now you don't have to implement that method in the base class, but the
compiler will allow you to call it on any instance of ClassA. (Of
course at runtime you'll get a message-not-understood exception if the
subclass didn't implement the method.)
To see if an instance implements that method, call [self
respondsToSelector: @selector(optionalMethodToImplement:)].
if ([self conformsToProtocol:@protocol(Check)])
[self optionalMethodToImplement];
}
Problem #1: The compiler complains that ClassA may not respond to
optionalMethodToImplement.
If you still want to do it this way, you can work around that problem
by casting the receiver:
[(id<Check>)self optionalMethodToImplement];
—Jens
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden