Use __attribute __ ((objc_requires_super)) with the protocol

I have something like this:

@protocol MyProtocol <NSObject> - (void)oneMethod; @end 

.

 @interface BaseClassWithProtocol : NSObject <MyProtocol> @end 

.

 @interface ChildClassWithProtocol : BaseClassWithProtocol @end 

BaseClassWithProtocol implemented oneMethod , and I want to show a warning if ChildClassWithProtocol does not call super in its oneMethod implementation.

But I don’t know where to write __attribute__((objc_requires_super)) .

Logging is not supported when overriding oneMethod in .h looks silly.

So, is there a standard way that can solve the problem?

+7
ios objective-c
source share
2 answers

The super call requirement is introduced by BaseClassWithProtocol . This is not part of the protocol. Of course, some class could implement MyProtocol , but he wants this requirement not to be imposed. This is the nature of the protocols. They introduce an interface, not an implementation.

So, overriding a method in BaseClassWithProtocol with an attribute seems to me perfectly reasonable. Not sure why this looks "stupid."

+7
source share

Protocols are not intended to provide alerts for your various implementations. They only warn you if you agree to include the necessary methods in your implementation or not. But how do you implement the methods that apply to your Base class, which uses the protocol. That is , you must have the necessary functionality in the .h or .m files.

Consider this method:

You have a method in your protocol where you want to receive a warning for super calls. Now you accept the protocol in the class, which is a subclass of NSObject. But the NSObject class does not match the protocol you define. Should you now receive a warning for your class? According to your requirements, you should receive this warning. But this is not so. You will almost never receive this warning with this procedure.

+1
source share

All Articles