Doug Smith
Doug Smith

Reputation: 29314

What is the benefit of declaring private methods in the interface part of the .m file?

I noticed from the template Apple gives with Core Data and UITableViewControllers that it puts the configureCell method in the interface section of the .m file.

@interface CustomTableViewController ()

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;

@end

What is the benefit of doing this? Up until now I've always just wrote custom methods but not "declared" them anywhere else in the file like Apple does above. Should I be doing that?

Upvotes: 1

Views: 69

Answers (2)

rmaddy
rmaddy

Reputation: 318824

This is old style and it isn't needed anymore. In older versions of the compiler, methods had to be in the proper order. In other words, a method could only call private methods that appeared before it in the .m file.

By adding the declaration of the private method in the class extension, the order problem went away. It also solved a problem if you had two methods that needed to call each other. Neither could be 1st.

But the modern compiler essentially does a two-pass compile now so there is no need for the private declarations anymore.

Upvotes: 3

Grzegorz Krukowski
Grzegorz Krukowski

Reputation: 19802

Private interface comes in handy when you want to document your code - you can put all documentation code to private interface and keep implementation clean without unnecessary comments blocks - when you implement a body of a function.

Other than that I found it more annoying to keep it there - because when you refactor it or add new parameter you have to remember to go through that interface as well - otherwise it may lead to a but crash.

Upvotes: 2

Related Questions