Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 220.127.116.11 release ready!
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
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.
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.
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
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
create a callBack view controller where we use the Clouser method
and the output is – same as above output.
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.
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!
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.