Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between declaring protocols on public interface and private interface /implementation file

Tags:

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>
like image 297
timpone Avatar asked Jan 21 '13 16:01

timpone


People also ask

What is difference between protocol and delegate in Swift?

Protocol is a set of methods (either optional or required) that would be implemented by the class which conforms to that protocol. While, delegate is the reference to that class which conforms to that protocol and will adhere to implement methods defined in protocol.

What's the difference between a protocol and a class in Swift?

You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.

What are iOS protocols?

In iOS development, a protocol is a set of methods and properties that encapsulates a unit of functionality. The protocol doesn't actually contain any of the implementation for these things; it merely defines the required elements.

Is it possible to prevent the adoption of a protocol by a struct?

The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. But there would be a time when you want to restrict protocols to be adopted by a specific class. In Swift 5, you can do just that.


2 Answers

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.

like image 130
rmaddy Avatar answered Jun 05 '23 21:06

rmaddy


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.

like image 36
herzbube Avatar answered Jun 05 '23 21:06

herzbube