Best way of preventing other programmers from calling -init

后端 未结 5 1968
醉梦人生
醉梦人生 2020-12-23 11:43

When designing a class hierarchy, sometimes the subclass has added a new initWithSomeNewParam method, and it would be desirable to disable calls to the old

5条回答
  •  伪装坚强ぢ
    2020-12-23 12:22

    You can explicitly mark your init as being unavailable in your header file:

    - (id) init __unavailable;
    

    or:

    - (id) init __attribute__((unavailable));
    

    With the later syntax, you can even give a reason:

    - (id) init __attribute__((unavailable("Must use initWithFoo: instead.")));
    

    The compiler then issues an error (not a warning) if someone tries to call it.

提交回复
热议问题