1

Possible Duplicate:
Objective-C: Should I declare private methods?

In objective-C I thought that you had to declare a method before calling it, or at least the method implementation had to be before the call to that method in the file. But it seems you can call a method without any prototype declared in the .h or class extension, and regardless of order.

I thought the following would have a problem but it works fine, so I am just wondering is there any need to declare 'private' methods in your class extension?

- (id)init
{
    self = [super init];
    if (self) {
        [self methodA];
    }
    return self;
}

- (void)methodA
{
    NSLog(@"method A");
    [self methodB];
}

- (void)methodB
{
    NSLog(@"method B");
}
Community
  • 1
  • 1
tagy22
  • 1,353
  • 17
  • 26
  • As a developer we should always follow best practices. Defining your private methods in the class extension enables you to see what kind of methods that you're using in your class as private methods. As we all know most of the time developers don't document their code and after a week when they wanna make some changes in their code they don't remember where to look and what to do. SO in my opinion although the app works without anyproblem it's always better to have declaration of methods in our class so that we will immediately know that which methods are used as private or not. Hope helps.. – lionserdar Oct 17 '12 at 20:20

0 Answers0