I just started programming Objective-C and, having a background in Java, wonder how people writing Objective-C programs deal with private methods.
I understand there may be several conventions and habits and think about this question as an aggregator of the best techniques people use dealing with private methods in Objective-C.
Please include an argument for your approach when posting it. Why is it good? Which drawbacks does it have (that you know of) and how you deal with them?
As for my findings so far.
It is possible to use categories [e.g. MyClass (Private)] defined in MyClass.m file to group private methods.
This approach has 2 issues:
- Xcode (and compiler?) does not check if you define all methods in private category in corresponding @implementation block
- You have to put @interface declaring your private category in the begin of MyClass.m file, otherwise Xcode complains with a message like "self may not respond to message "privateFoo".
The first issue can be worked around with empty category [e.g. MyClass ()].
The second one bothers me a lot. I'd like to see private methods implemented (and defined) near the end of the file; I do not know if that's possible.
You could try defining a static function below or above your implementation that takes a pointer to your instance. It will be able to access any of your instances variables.
There isn't, as others have already said, such a thing as a private method in Objective-C. However, starting in Objective-C 2.0 (meaning Mac OS X Leopard, iPhone OS 2.0, and later) you can create a category with an empty name (i.e.
@interface MyClass ()
) called Class Extension. What's unique about a class extension is that the method implementations must go in the same@implementation MyClass
as the public methods. So I structure my classes like this:In the .h file:
And in the .m file:
I think the greatest advantage of this approach is that it allows you to group your method implementations by functionality, not by the (sometimes arbitrary) public/private distinction.
If you wanted to avoid the
@interface
block at the top you could always put the private declarations in another fileMyClassPrivate.h
not ideal but its not cluttering up the implementation.MyClass.h
MyClassPrivate.h
MyClass.m
As other people said defining private methods in the
@implementation
block is OK for most purposes.On the topic of code organization - I like to keep them together under
pragma mark private
for easier navigation in XcodeWhile I am no Objective-C expert, I personally just define the method in the implementation of my class. Granted, it must be defined before (above) any methods calling it, but it definitely takes the least amount of work to do.
You could use blocks?
I'm aware this is an old question, but it's one of the first I found when I was looking for an answer to this very question. I haven't seen this solution discussed anywhere else, so let me know if there's something foolish about doing this.