Greg
Greg

Reputation: 34798

objective-c private versus public methods and declaration in header or not?

What is the best practice approach to private methods in objective-c. That is a method that's only going to be used the class as a helper method.

In particular what's not clear to me is:

  1. Is there a need to have the method specified in the header file as private at all? i.e. why not just leave it out of the header file, and
  2. If you can leave it out of the header file, then what is the point of having private methods?
  3. Or is it the case in objective-c there is no such thing as real private methods, in which case is it better just to specify everything in the header file and no bother marking the private at all?

thanks

Upvotes: 9

Views: 8521

Answers (3)

PapaSmurf
PapaSmurf

Reputation: 2475

Best practice (and is even a compiler option to check) is that ALL methods be declared one way or the other. To 'hide' helper methods from prying eyes, declare it as such in the implementation .m file, as in:

#import Client;

@interface myClass (Private)
- (void) privateMethod;
- (float) bankAccountBalanceForClient:(Client *)client;
@end

@implementation myClass
- (void) privateMethod;
{
    //foo here
}

and so on. the private methods are a Category called Private of myClass methods. This category can be declared anywhere, even in a master .h file called private methods, although that would be a maintenance nightmare.

So, using the public .h file for public methods, and the .m file to declare private methods, you have all your methods declared somewhere. I use this compiler option to ensure and force it, so that any method used is actually declared somewhere (or I get a syntax error) and thus I dont get any runtime crashes due to method not found.

Upvotes: 2

Anomie
Anomie

Reputation: 94794

There is no need to specify the method in the public header file. You may want a "private" header file for use by other classes in your module, if the classes in your module are supposed to be "friends". You could even have a "protected" header file, as Apple does with UIGestureRecognizerSubclass.h for example. It's all just convention, though, nothing supported by the language itself.

A private method in Objective-C is just one that is not publicly documented; any method can still be called from anywhere, as long as the caller knows the name of it in order to create the appropriate selector. The advantage of not publicly documenting a method is that you are free to change or remove it without worrying about backwards compatibility. Leaving them out of the header file is one way of not publicly documenting them.

Upvotes: 7

DougW
DougW

Reputation: 30025

What you probably want to use is called "Class Extensions". They look similar, but shouldn't be confused with Categories. This will allow you to declare private methods in your .m file, and you'll get all the nice IDE corrections and suggestions.

Here's a decent article on it
And a related SO question

Upvotes: 6

Related Questions