Alex
Alex

Reputation: 833

Private method declarations in objective-C

Do I have to DECLARE all private methods in .m class file inside

@interface ClassName()
  //Privare Methods
@end

before

@implementation ClassName
  // Implementation of Private & Instance methods
@end

for every method I am implementing & using OTHER THEN the instance methods (methods declared in .h file)?

X-Code 4 DOES NOT give me WARNING for ALL private methods but ONLY for few of them. For example, it warns me for methods I am calling inside gesture handler functions but not inside other routines/methods. I am confused as to declare all non-instance methods or just declare the ones I get warned for.

Upvotes: 1

Views: 235

Answers (2)

hamstergene
hamstergene

Reputation: 24429

XCode won't warn about methods that appear before the point of invocation:

@implementation 

- (void) foo:(float)x;
{
    NSLog(@"%f", x); // prints 15.000000
}

- (void) bar;
{
    [self foo:15.0]; // no warning
    [self baz:15.0]; // warning
}

- (void) baz:(float)x;
{
    NSLog(@"%f", x); // prints 0.000000 instead of 15.0
}

@end

It is strongly recommended to declare methods which you are warned about, the code above gives one example why.

Upvotes: 3

justadreamer
justadreamer

Reputation: 2440

No you don't have to declare every method you implement. The declaration is needed when the method you call is defined after the method which is calling it in .m file:

- (void) methodA {
     [self methodB]; //here you will get a warning if you don't define methodB it in a private class extention
}

- (void) methodB {
}

Upvotes: 1

Related Questions