Protocol vs Closures

Updated 26 February 2021

Save

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?

Protocol: 

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:-

Closures:

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:-

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.

                                                                           first view controller image                          second view controller image

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.

                                                                   output 1 Image                           output 2 image

step 2:- After that when you click on the back button, you will see your name is showing in the previous controller.

final output

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.

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.

Difference between protocol Delegate and Closures –

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!

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home