Search code examples
objective-coverridingsubclasspragma

Restrict usage/generate a compile error when someone uses a particular method in an Objective-C class/object


Suppose I have an AwesomeClass but I cannot change it's implementation (ie it is from a static library or framework). I like everything about AwesomeClass except for - (void)thatOneBadMethod. I can subclass AwesomeClass to be MyAwesomeClass and override that method with an empty implementation (not calling super) but the problem is that if my fellow developer is using MyAwesomeClass and is unaware of my actions he may try to use thatOneBadMethod thinking that it is doing something it is not.

Is there some preprocessor directive that I can put in the override for thatOneBadMethod so that he gets a compiler error or warning when he tries to use it?


Solution

  • You can deprecate the method in the header file:

    -(void)thatOneBadMethod __attribute__ ((deprecated("Unsupported, please call 'thatOneGoodMethod' instead!")));
    

    But I'd also suggest generating an error when people try to call it:

    -(void)thatOneBadMethod {
      NSAssert(NO, @"Unsupported, please call 'thatOneGoodMethod' instead!");
    }