8

I've been declaring private methods in class extensions, according to Best way to define private methods for a class in Objective-C.

But, I just realized that, in Xcode 4, if I leave out the declaration of a private method altogether and just implement it, the app compiles and runs without warning or error.

So, should I even bother declaring private methods in class extensions?

Why should we have to declare methods anyway? In Java, you don't... neither in Ruby.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651

3 Answers3

10

A method definition only needs to be defined if the caller is declared before the method. For consistency I would recommend defining your private methods in the extension.

-(void)somemethod
{
}

-(void)callermethod
{
    //No warning because somemethod was implemented already
    [self somemethod];
}

-(void)callermethod2
{
    //Warning here if somemethod2 is not defined in the header or some extension
    [self somemethod2];
}

-(void)somemethod2
{
}
Joe
  • 56,979
  • 9
  • 128
  • 135
  • Not quite a category and an extension are slightly different. In an extension you can add ivars and the compiler will moan if you do not implement the methods you declare in an extension. I'm sure there are their are other differences but these are the ones I take advantage of. – Paul.s Jul 20 '11 at 19:30
  • Class extensions do not allow you to add iVars but the difference between the two are extension methods are required to be implemented in the main @implementation where a category it is not. – Joe Jul 20 '11 at 19:49
  • No you can add ivars in an extension I use this all the time. An extension is declared with nothing between the brackets like this `@interface UIViewController ()` where as a category does have something between the brackets like this `@interface UIViewController (myCategory)`. The book iOS recipes mentions this technique - I can't track it down in apple docs at the min – Paul.s Jul 20 '11 at 20:42
  • I just tested again for my own sanity and no it does not work in the extension! But that is using LLVM GCC 4.2 compiler, the newer LLVM 2.0 does support this. All of my existing projects used the GCC compiler :) – Joe Jul 20 '11 at 20:50
5

This answer has already been correctly answered by Joe for Xcode prior to v4.3. However, in v4.3 and above, not only do private methods not need to be declared, but declaration order is now irrelevant. For details, see:

Private Methods in Objective-C, in Xcode 4.3 I no longer need to declare them in my implementation file ?

Community
  • 1
  • 1
ravron
  • 11,014
  • 2
  • 39
  • 66
2

This will compile and run fine without declaration:

- (void)foo {
}

- (void)bar {
    [self foo];
}

But last I checked, this will give a warning:

- (void)bar {
    [self foo];
}

- (void)foo {
}

In other words, it's just like in C: a declaration is not necessary if the definition comes before any use. C requires this to avoid having to add an extra pass to the compiler (one to find the functions and then one to actually parse them). As for whether you should declare them when not necessary, it's really up to the style of the codebase you're working with.

As for other languages that don't require declarations, some just go ahead with the extra pass, while others don't need to know the number and types of the arguments or the return type at compile time (they look up functions at runtime instead, or they don't have strongly-typed variables to begin with so it doesn't "matter") so they can just skip it.

Anomie
  • 92,546
  • 13
  • 126
  • 145