22

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

Matt J
  • 43,589
  • 7
  • 49
  • 57
Tommy
  • 481
  • 1
  • 6
  • 9
  • 2
    Other answers may be found at http://stackoverflow.com/questions/172598/best-way-to-define-private-methods-for-a-class-in-objective-c – mouviciel Mar 18 '09 at 13:25
  • possible of duplicate. "http://stackoverflow.com/questions/172598/best-way-to-define-private-methods-for-a-class-in-objective-c" – Ayaz Aug 23 '13 at 05:32

6 Answers6

41

If you're working in 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 a MyClass.h file, you can add to your MyClass.m the following:

@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 advanage of a class extension is that the "extension" methods are implemented in the original class body. Thus, you don't have to worry about which @implementation block a method implementation is in and the compiler will give a warning if the extension method is not implemented in the class' @implementation.

As others have pointed out, the Objective-C runtime will not enforce the privateness of your methods (and its not too hard to find out what those methods are using class dump, even without the source code), but the compiler will generate a warning if someone tries to call them. In general, the ObjC community takes a "I told you not to call this method [by putting it in a private class extension or category or just by documenting that the method is private] and you called it anyways. Whatever mess ensues is your fault. Don't be stupid." attitude to this issue.

Community
  • 1
  • 1
Barry Wark
  • 107,306
  • 24
  • 181
  • 206
3

No, any object can send any message to any other object. You can, however, put the method in a category that's part of the class's implementation file. That way, you'll get a "Class may not implement this method" warning if you try to call it anywhere else. That's the normal way of making a method "private."

Chuck
  • 234,037
  • 30
  • 302
  • 389
2

There is nothing that will prevent the method being called (since objective-c is message based anything can be sent any message), but you can declare them outside of the header so they are not visible and the compiler will generate warnings if used.

This works for both class and instance methods.

E.g.

#import "SomeClass.h"

// Interface for hidden methods
@interface SomeClass (hidden)
+(void) hiddenClassMethod;
-(void) hiddenInstanceMethod; 
@end

Note: Do NOT declare variables like this or they will become class-variables - e.g. only one variable will be used by all instances.

Andrew Grant
  • 58,260
  • 22
  • 130
  • 143
  • 1
    It is possible to have private methods in a message-based language. Ruby does it. Objective-C just happens not to. – Chuck Mar 15 '09 at 02:08
  • Right, I never said that this was common to all message based languages. – Andrew Grant Mar 15 '09 at 02:23
  • Ruby only obscures them, just like Objective-C: http://blog.jayfields.com/2007/11/ruby-testing-private-methods.html Really though, the goal is encapsulation, not security. If you cannot trust all the code running in your system all is lost anyway. – Kendall Helmstetter Gelner Mar 15 '09 at 08:00
  • Note: A recent addition to objective C is that you can leave the category blank, and then you get errors if the methods are not found - so it would look like: @interface SomeClass () in the implementation. Nothing wrong with naming it though. – Kendall Helmstetter Gelner Mar 15 '09 at 08:02
0

You can do so by using categories. I've got a fuller description in my answer to this SO question.

As has been said, you can't stop anyone sending a message to a selector, but by using categories you can reduce the visibility of these functions.

Also, you can have more than one category extending a class. So, by using informative category names you can group private functions into related blocks, improving the self-documenting nature of your code.

Community
  • 1
  • 1
Abizern
  • 146,289
  • 39
  • 203
  • 257
0

As others mentioned, you can't have code that's

  1. a method, and
  2. impossible to call from outside a class.

Folks have already pointed out that you can abandon point 2, and get a method that's hard-but-not-impossible to call. Alternatively, why not abandon point 1?

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

Now the code can only be called from within same file. You don't get any of the magic private-member access you can get with a method, so this is by no means a perfect solution. But there's no better way to achieve privacy.

vasi
  • 1,026
  • 7
  • 6
0

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 it will be helpful .

Raj
  • 5,895
  • 4
  • 27
  • 48