Difference between protocol and delegates?

What is the difference between protocol and a delegate ?

and

How can we declare variables in the protocol class ?

+55
ios objective-c iphone protocols delegates
Mar 25 2018-11-11T00:
source share
7 answers

A protocol declared using syntax ( @protocol in Objective-C) is used to declare a set of methods that the class "accepts" (declares that it will use this protocol). This means that you can indicate in your code that "you don't care which class is used if it implements a certain protocol." This can be done in Objective-C as follows:

id<MyProtocol> instanceOfClassThatImplementsMyProtocol;

If you state this in your code, then any class that "conforms" to the MyProtocol protocol can be used in the instanceOfClassThatImplementsMyProtocol variable. This means that the code that uses this variable knows that it can use any methods defined in MyProtocol with this particular variable, no matter what class it is. This is a great way to avoid the inheritance design pattern and avoid tight coupling.

Delegates are using the protocol language feature. A delegation design pattern is a way to develop code to use protocols where necessary. In Cocoa structures, a delegate design pattern is used to indicate an instance of a class that conforms to a particular protocol. This particular protocol defines the methods that the delegate class must implement to perform certain actions on these events. A class that uses a delegate knows that its delegate conforms to the protocol, so it knows that it can call implemented methods at specific points in time. This design pattern is a great way to separate classes, because it really makes it easy to exchange one instance of a delegate for another - all the programmer has to do is make sure that the replacement instance or class conforms to the required protocol (i.e. it implements the methods specified in protocol)!

The protocols and delegates are not limited to the development of Objective-C and Mac / iOS, but the Objective-C language and Apple platforms make heavy use of this wonderful language feature and design template.

Edit:

Here is an example. The UIKit Cocoa Touch framework has a UITextFieldDelegate protocol. This protocol defines a series of methods that classes that are delegates of a UITextField instance must implement. In other words, if you want to assign a delegate to a UITextField (using the delegate property), you better make sure that this class matches the UITextFieldDelegate. In fact, because the delegate property of UITextField is defined as:

@property(nonatomic, weak) id<UITextFieldDelegate> delegate

The compiler will then issue warnings if you assign it a class that does not implement the protocol. This is really helpful. You must declare that the class implements the protocol, and by saying that it does, you let other classes know that they can interact with your class in a certain way. Thus, if you assign an instance of MyTextFieldDelegateClass to the delegate property of a UITextField object, UITextField knows that it can call some specific methods (related to text input, selection, etc.) of your MyTextFieldDelegateClass. He knows this because MyTextFieldDelegateClass said that he will implement the UITextFieldDelegate protocol.

Ultimately, all this leads to much more flexibility and adaptability of the code of your project, which, I am sure, you will soon understand after using this technology! :)

+78
Mar 25 '11 at 11:00
source share

A protocol is a set of methods (optional or mandatory) that will be implemented by a class that conforms to this protocol. While the delegate is a reference to this class that conforms to this protocol and will adhere to the methods defined in the protocol.

See this Apple document for more details.

+25
Mar 04 '15 at 21:06
source share

Delegation: action on behalf of another object (design pattern in oops)

This is a design template in which an object called a delegate acts on behalf of and at the request of another object. At some point in execution, he sends a message to his delegate; the message informs the delegate that some event will happen and will ask you to respond. The delegate implements the method called by the message and returns the appropriate value.

Example: the appdelegate object acts on behalf of the appobject.

Protocol: Enabling communication between objects not related to inheritance

A protocol is a declaration of a program interface, the methods of which any class can implement. Protocols are objective with a language function . Simply put, a list of methods that any class can implement. To use this, you need to confirm the protocol. For example, the UITableviewDatasource protocol, whose cellforRowAtIndexPath methods are declared in the protocol, but we implement it to create a tableview.

See https://developer.apple.com/library/mac/referencelibrary/GettingStarted/RoadMapOSX/books/StreamlineYourAppswithDesignPatterns/StreamlineYourApps/StreamlineYourApps.html

+13
Feb 17 '15 at 10:07
source share

I am important p A prerequisite is understanding the protocols for the first delegates. I recommend reviewing this short tutorial first, and then What is a protocol? . In addition, you MUST know the difference between a class and a protocol, so see Objective-C: class versus protocol and What is the meaning of protocols? .




protocol: is ONLY a project of the functions to be implemented. Any class that adopts this plan will have to implement these functions. (DO NOT confuse function implementation with function call)

delegate: 1 also allows you to do what the delegat- ing class does without inheritance , for example

For example, you have a viewController and you want to upload images, or you want to get the customer distance to the store, so instead of doing it all yourself, you just have a middle object that does it for you. This object is known as a delegate object. Usually you do something like this:

 class ViewController : UIViewController , DownloaderDelegate{ //other code // inside viewDidLoad or elsewhere you write: downloaderHandler.delegate = self // now self can also use whatever the delegating object gives it...previously it was only a viewController but now it 'almost' also a downloader 

very similar to what you do to match tableViewDelegate

 class ViewController : UIViewController , UITableViewDelegate{ //other code // inside viewDidLoad or elsewhere you write tableView.delegate = self 

your self can now also do tableView related things.




delegate: 2 But this object (delegate) is a simple vanilla object (id or Any). This is stupid! You have to say this: "Hey, you have to work to have the specific functionality that you need in order to comply with the protocol that we have defined for you (we are not going to extend Any or id, as it would be stupid, (instead) we made a very explicit limited protocol "
in Objective-C this is pure vanilla id so what do you do

  @property (weak) id<DownloaderProtocol>delegate; 

in Swift * this is a little easier to do:

 weak var delegate:DownloaderProtocol? 

The protocol comes to the rescue ... the delegate implements (rather than uses) the function to fit the needs of your delegating class.




*: In Swift, you don’t have an id , but you don’t need its Any equivalent, because in Swift the protocols are also of the first-class citizens type.

+6
Sep 12 '16 at 19:15
source share

See the delegate announcement in the program

  id<myProtocol> *delegatingObject; 

The delegating object maintains a link to another object and sends a message to that object at the appropriate time.

A protocol is a group of related properties and methods that can be implemented by any class.

This means that any object (type id) that myProtocol confirms (a group of related properties and methods) can work as a delegate, or you can say that any person (id) who has the required degree (protocol) can work as a teacher (delegate).

+4
Feb 20 '16 at 16:53 on
source share

We can say a protocol as a set of rules. These rules may be optional or binding, as we should use in the protocol.

Delegates are a messaging technique for targets C and swift. The entity must take care of this message.

Example: A simple example that every iOS developer is used to is the UITableview. When creating a table, you must implement cellForRowAtIndexPath() and numberOfRowsInSection() in your controller, so that the rules (protocol) are defined in the UItableview class as needed, this requires a protocol ,

There is another protocol, such as heightForRowAtIndexPath() which is optional.

Now we pass to Delegate in UITableView. There is a method (message) didSelectRowAtIndexPath() that sends you an event message. If you set the delegate to self, it means that your controller is ready to take care of this event.

This term seems more confusing for developers because we are used to using it together (:

Enjoy!!!!

+2
Dec 05 '18 at 8:02
source share

Protocol provides a set of declarations. Which can implement any other class.

Delegates is a messaging technology in Objective-C and Swift. Passing data or sending information from one object to another object using a delegate.

-one
Dec 05 '18 at 13:11
source share



All Articles