Brief About Delegation Design pattern in Swift

Posted By : Varun Wadhwa | 10-Apr-2017

In this blog I'll explain about delegation design pattern in iOS.

Delegation is design pattern in iOS where one class (called a delegator) give control or responsibility for some behaviour logic to another class (called delegator).

In Swift , delegation can be achieved by writing an abstraction layer called a Protocol.

Protocols are contracts , when an object (say a UIViewController) subclass agrees to providing logic back on recieving the message. (called confoming a protocol).

Delegation is use for one to one message passing and it's used often instead of subclassng a class to change its behaviour. 

One main advantage of delegate over subclassing is that a class may implement multiple delegate protocols.

Data Source is often confused with Delegates the only difference between them is that the delegates supplies behaviour  logic where as data source supllies data.

For Example :

UITableView itself doesn't know how many cells to present and what to present in each cell , it asks other class (i.e our custom class) to inform it about number of cells and what data cell to return.

Thanks,

About Author

Author Image
Varun Wadhwa

Varun is a mobile developer with experience in various technologies like Titanium , Cordova and native iOS development.

Request for Proposal

Name is required

Comment is required

Sending message..