Home > CodeSnippets, iOS, Objective-C > Private method in Objective-C

Private method in Objective-C

There are no private methods in Objective-C like e.g. in C# .NET. Though you can create a category with an empty name (class continuation) in Objective-C 2.0 (i.e. @interface MyClass ()). With this category you can create the private method implementations in the same @implementation MyClass file as the public methods.

With an addition like “__” at the start of your “private” methods you should bring in this naming convention to prevent your classes from being breakable in a too easy way. The possibility that someone will override some of your “private” methods by accident is then on a much lower value. Apple reserved names with a leading underscore for its own uses, so prefer an addition like “__”.

Create your classes for example like this:

In the header file:

@interface MyClass {
    // My Instance Variables

- (void)myPublicMethod;


And in the implementation file:

@interface MyClass(/* Private methods */)

- (void)__myPrivateMethod;


@implementation MyClass

- (void)myPublicMethod {
    // Implementation goes here

- (void)__myPrivateMethod {
    // Implementation goes here

The second alternative is to put the category into its own .h file. You can name such a file after its class and category with a + separating them, so @interface MyClass (PrivateMethods) can be found in MyClass+PrivateMethods.h. With this convention you can import the header in your unit test classes.

Categories: CodeSnippets, iOS, Objective-C
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: