Forwarding protocol @ to subclasses

I have:

@interface SuperClass : UIViewController <UITableViewDelegate,UITableViewDataSource> 

And then

 @interface SubClass : SuperClass 

This SuperClass does not implement the required SubClass protocol SubClass .
Can warnings be warned (saying that the SuperClass implementation is not complete)?

Instead of embedding empty / nil methods in SuperClass , can @required warnings be done against SubClass ?

+6
source share
4 answers

No, you are asking, in fact, for abstract classes that are not in Objective-C.

It is best to mute methods in the base class to throw an exception.

+5
source

You can not declare acceptance of the protocol in the superclass, but require 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 ...] } } 

Thus, whenever a SuperClass subclass is initialized, it throws an exception if it does not match <UITableViewDelegate> . This does not require further work after putting it in a superclass.

+10
source

If SuperClass does not comply with UITableViewDelegate protocols, etc., it should not have it in .h.

You can simply move the protocols to SubClass .

0
source

Yes, this is possible with the help of an extension of a private class.

Example 1

Superclass.h

 @interface Superclass : UIViewController @end 

Superclass.m

 @interface Superclass()<UITableViewDelegate, UITableViewDataSource> @end @implementation Superclass // implement interface methods @end 

Subclass.h

 @interface Subclass : Superclass<UITableViewDelegate, UITableViewDataSource> @end 

After taking another step, you can move the extension of the private class to the private header Superclass + Private.h, then your other inner classes may know that it also implements them.

Example 2

Superclass + Private.h

 #import "Superclass.h" @interface Superclass()<UITableViewDelegate, UITableViewDataSource> @end 

Superclass.m

 #import "Superclass+Private.h> @implementation Superclass // implement interface methods @end 

Note There is a limitation on the fact that this method does not allow you to override and call the super in method, which does not correspond to a subclass of your superclass. For these methods, I would recommend the UIGestureRecogniser subclass template.

0
source

Source: https://habr.com/ru/post/889471/


All Articles