Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 295
Greg Avatar asked Mar 13 '11 23:03

Greg


1 Answers

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.

like image 127
Anomie Avatar answered Nov 15 '22 14:11

Anomie