Objective-C: Should I declare private methods?
Asked Answered
C

3

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.

Chemush answered 20/7, 2011 at 19:5 Comment(1)
Well you can't get xcode to autocomplete methods unless you declare them.Comfort
P
9

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
{
}
Pyrope answered 20/7, 2011 at 19:11 Comment(4)
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.Nick
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.Pyrope
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 minNick
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 :)Pyrope
E
6

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 ?

Endymion answered 28/6, 2012 at 0:41 Comment(0)
L
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.

Leann answered 20/7, 2011 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.