Tommy
Tommy

Reputation: 481

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

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

Upvotes: 22

Views: 24553

Answers (6)

Raj
Raj

Reputation: 6015

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 .

Upvotes: 0

vasi
vasi

Reputation: 1036

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.

Upvotes: 0

Barry Wark
Barry Wark

Reputation: 107754

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.

Upvotes: 41

Abizern
Abizern

Reputation: 150565

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.

Upvotes: 0

Andrew Grant
Andrew Grant

Reputation: 58786

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.

Upvotes: 2

Chuck
Chuck

Reputation: 237010

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."

Upvotes: 3

Related Questions