Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forward protocol @required to subclasses

Tags:

objective-c

I have:

@interface SuperClass : UIViewController <UITableViewDelegate,UITableViewDataSource>

And then

@interface SubClass : SuperClass

This SuperClassdoes not have the required protocol methods implemented SubClass one does.
Is it possible to prevent the warnings (saying SuperClass implementation is incomplete)?

Instead of implementing empty/nil methods in SuperClass, can the @required warnings validation be made against SubClass?

like image 219
Filipe Pina Avatar asked May 31 '11 17:05

Filipe Pina


2 Answers

You might not declare protocol adoption in the superclass, but demand compliance in all subclasses. This can be done by implementing +initialize in your superclass as follows:

+ (void)initialize
{
  if (self != [SuperClass class] && 
      ![self conformsToProtocol:@protocol(UITableViewDelegate)])
  {
    @throw [NSException ...]
  }
}

That way, whenever a subclass of SuperClass is initialized, it will throw an exception if it doesn't conform to <UITableViewDelegate>. This requires no further work after putting this in the superclass.

like image 147
Jonathan Sterling Avatar answered Oct 13 '22 01:10

Jonathan Sterling


No, what you're asking for is essentially abstract classes, which don't exist in Objective-C.

Your best bet is to stub the methods in the base class to throw an exception of some kind.

like image 33
Joshua Weinberg Avatar answered Oct 12 '22 23:10

Joshua Weinberg