Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly are protocols and delegates and how are they used in IOS?

Tags:

I'm really confused about the concept of delegates and protocols. Are they equivalent of interfaces and adapter classes in Java? How do they work? None of the resources I've read were helpful so far. "Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it." I have no idea what this means. Can someone please explain what they are and give a simple example? Thanks in advance!

EDIT:

As far as I now understand,

1) delegates implement protocols (another name for interfaces)

2) object registers a delegate (that implements a protocol)

3) object can call protocol methods on the delegate

Therefore, a delegate is connecting the object with the protocol.

Please correct me if I'm wrong.

I still don't understand why the object itself can't implement a protocol? It could've been so much easier!

like image 295
Oleksiy Avatar asked Jun 19 '13 13:06

Oleksiy


People also ask

What are delegates and protocols in iOS?

Delegation can be used to respond to a particular action, or to retrieve data from an external source without needing to know the underlying type of that source. Apple notes that: A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.

WHAT IS protocols in iOS?

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.

What is the difference between delegate and protocol?

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. Have a look at this Apple doc for more detail.

What is protocol in iOS Objective-C?

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.


1 Answers

Protocols are a way to specify a set of methods you want a class to implement if it wants to work with one of your classes. Delegates and Data Sources like UITableViewDelegate and UITableViewDataSource are protocols indeed.

You specify a protocol this way:

@protocol MyProtocol <NSObject>  - (void)aRequiredMethod;  @required - (void)anotherRequiredMethod;  @optional - (void)anOptionalMethod;  @end 

Methods declared after the @required or before any other specifier are required and the classes that want to use your protocol need to implement all of them. You can also declare some optional methods by declaring them after the @optional specifier.

You then can specify that a class "conforms" to a protocol (implements the required methods) in the interface of the class:

@interface MyClass <MyProtocol>  @end 

You usually keep a reference to an object conforming to a protocol using a property. For example, to keep track of a delegate:

@property (nonatomic, weak) id<MyProtocol> delegate; 

At this point, in your code, you just have to call the method you want to call on the object that you're keeping reference of and that implements your protocol as you would with any other method:

[self.delegate aRequiredMethod]; 

To check whether an object conforms to a protocol you can call

[self.delegate conformsToProtocol:@protocol(MyProtocol)] 

To check whether an object implements a method you can call

[self.delegate respondsToSelector:@selector(anOptionalMethod)] 

For more information, check the Apple's guide Working With Protocols.

like image 152
Gianluca Tranchedone Avatar answered Sep 28 '22 11:09

Gianluca Tranchedone