Implement a pure virtual method in Objective-C

前端 未结 6 1528
清酒与你
清酒与你 2020-12-02 18:22

I want to go to there. Seriously though, how does one implement a pure virtual method in an \"Apple\" way? Do you use a Protocol with your base class and throw exceptions on

6条回答
  •  一生所求
    2020-12-02 18:58

    In Objective-C, there is no pure virtual support as in C++.

    A simulation would be that you declare a method in your interface but don't implement it in your .m file. Of course you'd get compiler warnings but IIRC you can turn those off. But you won't get warnings/errors if you don't overwrite them in the subclass, which you get in C++ (IIRC).

    An alternative would be to implement them with just an NSAssert(NO, @"Subclasses need to overwrite this method"); body. Still, you'd only catch this at runtime, not compiletime.

提交回复
热议问题