Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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?

share|improve this answer
    
I am not referring to implementation of delegate methods I am referring to declaration. –  Amogh Talpallikar May 15 '13 at 12:33
    
@AmoghTalpallikar I think the question is: Why would you ever want to hide the fact that a given class implements some protocol? The answer, for me, is that a class may sometimes implement a protocol, but it's something that you want to be able to change. Perhaps it's a view with its own interface but which uses a UICollectionView internally, so you declare use of UICollectionViewDelegate in the implementation. Does it really matter? Most of the time, probably not at all. –  Caleb May 15 '13 at 13:12
    
for normal methods in any class whether its a viewcontroller,view or a regular NSObject subclass, If I have a method which I use internally I put the declaration in the private category which makes it a private method. and in case I want to expose a method, I will put it's declaration in .h In case of methods that are implementation of a protocol, in case the usage of the method like any other method is only within the class basically treating it as a private method. So I would like to make that method private, to do that I need to put declaration of that method in .m in a private category. –  Amogh Talpallikar May 15 '13 at 13:28
    
Since this method is a protocol method the declaration of these methods presence turns out to be the declaration of the protocol. So I thought why not put the declaration in the .m file make those methods private. I know I am overthinking it but same ways we can declare any method in .h also why make any method private at all? –  Amogh Talpallikar May 15 '13 at 13:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.