If you are running Objective-C 2.0, the best way to create methods that are "hard" for others to call is to put them in a class extension . Assuming you have
@interface MyClass : NSObject { } - (id)aPublicMethod; @end
in MyClass.h
, you can add the following to your MyClass.m
:
@interface MyClass () //note the empty category name - (id)aPrivateMethod; @end @implementation MyClass - (id)aPublicMethod {...} - (id)aPrivateMethod {...} //extension method implemented in class implementation block @end
The advantage of extending a class is that the "extension" methods are implemented in the original class of the class. This way, you don’t have to worry about @implementation
blocking the implementation of the method, and the compiler will warn you if the extension method is not implemented in the << 24> class.
As others have noted, the Objective-C runtime will not ensure the integrity of your methods (and it is not too difficult to find out that these methods use a class dump, even without source code), but the compiler will generate a warning if someone tries to call them. In general, the ObjC community accepts “I told you not to call this method [by putting it in an extension or category of a private class, or simply by documenting that this method is private], and you called it anyway. Don't be silly. Attitude to this question.
Barry Wark Mar 16 '09 at 17:42 2009-03-16 17:42
source share