Updated 26 February 2021
Hello guys, Today we will learn about the Difference between protocol Delegate and Closures.
Before starting we need to know what is protocol or closures are?
The protocol is a blueprint for methods, properties, and other required functionality.
protocols are declared after structure, enumeration, and classes.
we declare multiple or single functions in it. It is a delegate method
for example:-
1 2 3 |
protocol protocolName : class/struct/enum{ func functionName(parametrs) } |
It is used to pass around and used in our code for callbacks. or we can say that it is a self-contained block of functionality
closures are similar to c and objective c.
for example:-
1 |
var ClosureName: ((parameters) -> returnType) |
make a two-view controller and take the example of protocol and closures
In the first controller, I took a label and go button and set the constraint accordingly.
In the second controller, I took a textField and back button.
Now let’s take an example of protocol calling and closures calling
Example of protocol calling:-
create a view Controller and callBack View Controller class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import UIKit class ViewController: UIViewController, callBackDelegate { @IBOutlet weak var nameLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() } @IBAction func goToNextVc(_ sender: Any) { let storyBoard = UIStoryboard.init(name: "Main", bundle: nil) let vc = storyBoard.instantiateViewController(identifier: "CallBackViewController") as! CallBackViewController vc.callBackData = self self.navigationController?.pushViewController(vc, animated: true) } func callBack(text: String) { nameLabel.text = text } } |
in the above code, we see we call the delegate function (vc.callBackData = self ), which we created in the next controller, we need to call this function otherwise your app will be crash.
create a callBack view controller where we use the protocol method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import UIKit protocol callBackDelegate { func callBack(text: String) } class CallBackViewController: UIViewController { @IBOutlet weak var textField: UITextField! var callBackData: callBackDelegate? override func viewDidLoad() { super.viewDidLoad() } @IBAction func backButton(_ sender: Any) { callBackData?.callBack(text: textField.text ?? "") self.navigationController?.popViewController(animated: true) } } |
And the output is –
step1:- click on the GO button, you will reach on next view Controller and write your name in the text field.
step 2:- After that when you click on the back button, you will see your name is showing in the previous controller.
Example of Clouser:-
create a view controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import UIKit class ViewController: UIViewController { @IBOutlet weak var nameLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() } @IBAction func goToNextVc(_ sender: Any) { let storyBoard = UIStoryboard.init(name: "Main", bundle: nil) let vc = storyBoard.instantiateViewController(identifier: "ClouserCallBackViewController") as! ClouserCallBackViewController vc.callBack = { (text: String) in self.nameLabel.text = text } self.navigationController?.pushViewController(vc, animated: true) } func callBack(text: String) { nameLabel.text = text } } |
create a callBack view controller where we use the Clouser method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import UIKit class ClouserCallBackViewController: UIViewController { @IBOutlet weak var namelabel: UITextField! var callBack: ((_ text: String)-> Void)? override func viewDidLoad() { super.viewDidLoad() } @IBAction func backButton(_ sender: Any) { callBack!(namelabel.text ?? "") self.navigationController?.popViewController(animated: true) } } |
and the output is – same as above output.
step1:- click on the GO button, you will reach on next view Controller and write your name in the text field.
step 2:- After that when you click on the back button, you will see your name is showing in the previous controller.
As we see both method’s functionality are the same but there is a difference – In the protocol, we can make more than one function but in Closures, it is a self-contained block of functionality.
In the protocol method, we use the call by delegate method then use it but in closer, we can call it directly, there is no need for the delegate method.
Conclusion –
In this blog, we have discussed protocol working and Closures working
I hope this blog will help you in getting some basic knowledge about the Protocol vs Closures method.
Thanks for reading!
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.