What is / are the differences between declaring these Protocols these ways? Is it just that the ones in the .h file are publicly available?
in .h file:
@interface TestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
in .m file:
@interface TestViewController () <UISearchBarDelegate, UISearchDisplayDelegate, UIAlertViewDelegate, MKMapViewDelegate, CLLocationManagerDelegate>
Unless I am mistaken, it is not possible to use the forward declaration
@protocol Foo;
if you are also declaring that your class is adopting the protocol.Which means that in the header file of your class you need to include/import another header file with the protocol declaration. Depending on the size and complexity of a project, this can lead to header file "dependency hell".
Whether or not this is a real danger must be judged by the actual project. Most of the time it's probably not something you need to care about, but I just wanted to mention this angle.
I admit it can not be matter of style!!!
Why to hide those protocols in .m, may be your classes will go with some frameworks. There .h is visible and everyone can see "Oh yaaa, these are the delegate protocols for this class". Instead of hidden magic.
So, it is made transparent rather than opaque.
I judge preferable the first way, so that who uses the class knows if the class implements some protocol. This can only be an advantage.
Protocols are used to guarantee that some method are implemented, this is an alternative to multiple inheritance which isn't possible in Objective-C. Let's suppose that I want to pass an object that conforms to some protocol to a function or method, and I must be sure that this class implements some protocol. In this case I must know which protocols the class is implementing.
It also shuts up some compiler warning to functions (or methods) like these:
Transparency is often a good idea and rarely a disadvantage.
When you add the protocols to the .h file, this tells everyone that includes the header file that the class adheres to the given protocols.
When you add the protocols to the .m file, this is essentially a private indication that the class adheres to the protocols. Only the implementation knows.
You should only use the first form (in the .h file) when outside classes need to know that the class adheres to the protocol. You should use the second form (in the .m file) when only the implementation cares.
In the example you gave, it is highly unlikely that other classes need to know about the class adhering to the table view protocols. Those should be in the .m file. It's also unlikely any other class needs to know about the search protocols. These are implementation details. These belong in the .m file.
There may be cases where you use both. This is fine.
Here' my guideline. Put it in the .m file unless you have a specific need to let other classes know about the use of the protocol.