• 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
How to adopt a superclass's protocol?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to adopt a superclass's protocol?


  • Subject: How to adopt a superclass's protocol?
  • From: "K. Darcy Otto" <email@hidden>
  • Date: Mon, 28 Apr 2008 17:00:30 -0700

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 (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


  • Follow-Ups:
    • Re: How to adopt a superclass's protocol?
      • From: Michael Vannorsdel <email@hidden>
    • Re: How to adopt a superclass's protocol?
      • From: Jens Alfke <email@hidden>
  • Prev by Date: AppKiDo, delegates, known bugs
  • Next by Date: Re: Failure on unarchiving a NSBezierPath
  • Previous by thread: AppKiDo, delegates, known bugs
  • Next by thread: Re: How to adopt a superclass's protocol?
  • Index(es):
    • Date
    • Thread