Is it possible to declare a method as private in Objective-C?

Is it possible to declare a method as private in Objective-C?

+20
objective-c
Mar 15 '09 at 1:33
source share
6 answers

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.

+33
Mar 16 '09 at 17:42
source share

No, any object can send any message to any other object. However, you can put the method in a category that is part of the class implementation file. Thus, you will receive a warning “The class cannot implement this method” if you try to call it elsewhere. This is the usual way to make the method "private".

+2
Mar 15 '09 at 1:37
source share

There is nothing that interferes with the method being called (since objective-c is a message based on anything, any message can be sent), but you can declare them outside the header so that they are not visible, and the compiler generates warnings if .

This works for both class methods and instance.

eg.

 #import "SomeClass.h" // Interface for hidden methods @interface SomeClass (hidden) +(void) hiddenClassMethod; -(void) hiddenInstanceMethod; @end 

Note. DO NOT declare such variables or they will become class variables - for example. only one variable will be used by all instances.

+2
Mar 15 '09 at 1:56
source share

You can do this using categories. I have a more complete description in the answer to this SO question.

As already mentioned, you cannot prevent someone from sending a message to the selector, but with the help of categories you can reduce the visibility of these functions.

In addition, you can have more than one category extending a class. Thus, using the names of information categories, you can group private functions into appropriate blocks, improving the self-documenting nature of your code.

0
Mar 15 '09 at 20:56
source share

As mentioned, you cannot have code

  • method and
  • cannot be called from outside the class.

People have already indicated that you can refuse point 2 and get a method that is difficult but not impossible to call. Alternatively, why not leave point 1?

static id myPrivateMethod(MyObject *me, int arg1, id arg2) { ... }

Now the code can be called from only one file. You do not get any magic access to the private member that you can get using the method, so this is far from an ideal solution. But there is no better way to achieve privacy.

0
Mar 16 '09 at 17:53
source share

To implement hidden methods (instance and / or class)

  // =========================== // = File: SomeClass.m // =========================== #import "SomeClass.h" // ================================= // = Interface for hidden methods // ================================= @interface SomeClass (hidden) -(void) hiddenInstanceMethod; @end // ================================ // = Implementation for SomeClass // ================================ @implementation SomeClass -(void) hiddenInstanceMethod { printf( "Hidden instance method\n" ); } -(void) msg { printf("Inside msg()...\n"); [self hiddenInstanceMethod];//private method calling } @end 

http://macdevelopertips.com/objective-c/private-methods.html

reffer this link will be helpful.

0
Aug 23 2018-11-11T00:
source share



All Articles