How to avoid “incomplete implementation” warning in partial base class

后端 未结 4 686
天命终不由人
天命终不由人 2020-12-29 08:20

I have created a protocol that my classes need to implement, and then factored out some common functionality into a base class, so I did this:

@protocol MyPr         


        
相关标签:
4条回答
  • 2020-12-29 08:54

    You could conceivably do something like this:

    @implementation Base
    
    - (void)bar
    {
        if ([self class] == [Base class]) {
            [self doesNotRecognizeSelector:_cmd];
        }
    }
    
    @end
    

    That way, you have an implementation, but by default it raises an exception. However, if a derived class accidentally calls [super bar] or does not override bar, then the exception won't be raised. If that's not what you want, you could just shorten it to:

    @implementation Base
    
    - (void)bar
    {
        [self doesNotRecognizeSelector:_cmd];
    }
    
    @end
    

    In which case, an exception will be raised, even if a subclass calls [super bar] or does not override bar.

    0 讨论(0)
  • 2020-12-29 09:00

    In Obj-C, there is no concept of abstract class. So that, you can not let your Base class to be abstract (which means dont' implements all methods in the protocol). You can only have 2 choices. Let the method in protocol to be optionol and then implements it yourself in the derived class. Or, force all classes in the hierarchy to implement it, but let the caller to be careful not call the incorrect method ones

    0 讨论(0)
  • 2020-12-29 09:01

    In your protocol definition, you need to declare your methods under the @optional keyword.

    Your code should look like this:

    @protocol MyProtocol
    
    @optional
    - (void) foo;
    - (void) bar;
    
    @end
    

    See this question on SO.

    0 讨论(0)
  • 2020-12-29 09:05

    I do something like this

    @protocol MyProtocol
    - (void) foo;
    - (void) bar;
    @end
    
    @interface BaseAbstract
    - (void) bar; // give a default implementation
    @end
    
    @interface Derived_1 : BaseAbstract<MyProtocol>
    // you will get a compiler warning for foo() since it is not implemented
    
    // you will NOT get compiler for bar() warning since a default 
    // implementation is inherited
    @end
    
    @interface Derived_2 : BaseAbstract<MyProtocol>
    @end
    
    typedef BaseAbstract<MyProtocol> Base;
    
    0 讨论(0)
提交回复
热议问题