-1

how to make a single method in a class to private in Objective-c?

1

4 Answers 4

3

There's no concept of private methods in Objective-C. What you can do, however, is obmitting the method in the header file and adding a category in a private file.

Usually this is down like this:

@interface MyObject : NSObject {
   // ivars
}
// public methods
@end

// usually in a seperate file
@interface MyObject ()
// private methods
@end

An empty name for a category means that it's a class extension, this tells the compiler that the method is required in the main implementation block.

1

You can't strictly speaking create private methods in Objective-C; but you can create a category in the file containg the implementation (MyClass.m), hiding it from the header file (MyClass.h).

//MyClass.h    
#import <Foundation/Foundation.h>

@interface MyClass : NSObject {

}

@end

Implementation file:

//MyClass.m

@interface MyClass ()

- (void)myPrivateMethod;

@end

@implementation MyClass

- (void)myPrivateMethod {
  return;
}

@end
1

Objective-C doesn't support private methods natively by design.

However, you can achieve private method control by using "class extensions" (anonymous categories).

The basic idea is to declare a class extension with your private methods inside the implementation file for your class (not its header), but outside of your @implementation block. You can then implement the methods and no outside class will be able to see them. Like this:

@interface MyClass ()
    - (void)myPrivateMethod;
@end

@implementation MyClass
    - (void)myPrivateMethod
    {
        //implementation goes here
    }

    -(void)someOtherMethod
    {
        [self myPrivateMethod];
    }
@end

Using a class extension forces you to implement the methods in the main @implementation block and requires that they are implemented (much like as if they were declared in the main @interface block in the header).

Unfortunately, due to Objective-C's dynamic nature, this won't actually prevent those methods from being called by other classes. They will get a warning telling them that the class may not respond to the message, but at runtime it will work. The only thing this gives you is the ability to hide the methods from other programmers.

1
  • 1
    Anonymous categories are called class extension. The difference is that a method has to be declared in the main implementation block. Normal categories may be in a seperate, in the main implementation block or even not at all. Mar 3, 2010 at 11:45
0

You can't. Only instance variables can be marked as private.

Though there are ways to do something equivalent to private methods. The general idea is that you don't declare the private method in your header but in your implementation file.

Therefore, if you'd want to have a public (myPublicMethod) and private (myPrivateMethod) method, you're header could look like this:

@interface MyClass {
}
- (void)myPublicMethod;
@end

And then you have three options for your implementation file:

1.

Don't declare the method in any @interface section and just make sure it's implemented before it's being used in the @implementation section.

@implementation MyClass

- (void)myPrivateMethod {  }

- (void)myPublicMethod {
    [self myPrivateMethod];
}

@end

2.

Use an anonymous category in your implementation file to declare the private methods and implement them in the main @implementation block.

@interface MyClass ()
- (void)myPrivateMethod;
@end

@implementation MyClass

- (void)myPrivateMethod {  }

- (void)myPublicMethod {
    [self myPrivateMethod];
}

@end

3.

Use a regular category in your implementation file and implement the private methods in a separate @implementation block.

@interface MyClass (PrivateMethods)
- (void)myPrivateMethod;
@end

@implementation MyClass (PrivateMethods)

- (void)myPrivateMethod {  }

@end

@implementation MyClass

- (void)myPublicMethod {
    [self myPrivateMethod];
}

@end
1
  • Advantages / disadvantages for each method would be nice. Mar 3, 2010 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.