Best way of preventing other programmers from calling -init Best way of preventing other programmers from calling -init objective-c objective-c

Best way of preventing other programmers from calling -init


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.


To add to what @DarkDust posted, you could alternatively use UNAVAILABLE_ATTRIBUTE

- (id)init UNAVAILABLE_ATTRIBUTE;

This will throw an error when a user tries to call init on an instance of this class.


Flag it deprecated?Developers will be developers, you can't stop us all! ;-)

How do I flag a method as deprecated in Objective-C 2.0?