I have a View Controller or a View that implements delegate
and dataSource
methods of UITableView
.
These methods are not going to be used outside this class.
So is it a good idea to make the declaration of the UITableViewDataSource
and UITableViewDelegate
inside the .m file with a private category?
For example :
Instead of doing this:
MyViewController.h
@interface MyViewController:UIViewController<UITableViewDataSource,UITableViewDelegate>
{}
@end
I put the declaration in the private category inside as there is no need for other objects to know whether my View Controller uses a TableView or not.
MyViewController.m
@interface MyViewController()<UITableViewDataSource,UITableViewDelegate>
@end
and Implementation in the .m file being the same in both the cases.
So is it a good idea to make the declaration of the
UITableViewDataSource and UITableViewDelegate inside the .m file with
a private category?
That depends. Is the fact that MyViewController
these protocols a private implementation detail, or something that you want to communicate to anyone using the class?
Header files are there to communicate your class’s public interface, not just to the compiler but also to anyone using the class. If you would include the fact that your class implements a given protocol in the written documentation, then you should declare the use of the protocol in the header. If you wouldn’t put that in the docs, and instead want to send the message that nobody should count on the class implementing that protocol, put it in the implementation file.
Technically, it’s of no consequence. Interface Builder will recognize that your class implements the protocols and offer a connection.
Apple code puts the protocols implemented in the header, and it’s common practice. So anyone browsing your code will expect to see them there.
I think you are over thinking it hiding the protocols. I can’t imagine an scenario where it is harmful to expose that information. Can you?
4