Protocols are like contracts: Contracts formalize agreements between people by stating the agreement in writing. Similarly, protocols formalize the agreement between objects by stating the type of information that an object can request from another object. Specifically, protocols declare properties and methods. Consider this snippet of the UITableViewDataSource protocol:

public protocol UITableViewDataSource : NSObjectProtocol {

public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

optional public func numberOfSectionsInTableView(tableView: UITableView) -> Int

optional public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?

optional public func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?

The protocol declares, but does not implement, two required methods (the top two) and three optional methods (prefixed with the word optional). The methods in the code above serve as the contract.

Conforming to a protocol is like signing a contract: Once someone signs a contract, that person is legally obligated to uphold the agreement stated within the contract. If someone breaks the contract, they could get fined or go to jail. Similarly, when an object conforms to a protocol, it is obligated to implement any properties and methods required by the protocol. If an object conforms to a protocol (signs the contract), but does not define the required properties and methods (breaks the contract), the program will not compile (goes to jail).

To conform to a protocol, an object must add the name of the protocol to its definition. For example, if we had a class named CustomViewController (assuming its a subclass of UIViewController) and wanted it to conform to the UITableViewDataSource protocol, we would define CustomViewController as follows:

class CustomViewController: UIViewController, UITableViewDataSource {

The name of the protocol follows immediately after the name of the superclass, and is separated by a comma.

Although Swift does not support multiple inheritance, it does support objects conforming to multiple protocols, like so:

class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, SomeCustomProtocol {

When objects conform to many different protocols, as above, it is very easy to lose track of which properties and methods belong to which protocol. To combat this problem and keep our code base organized, we often use extensions to conform to protocols, like so:

class CustomViewController: UIViewController {

extension CustomViewController: UITableViewDataSource {

extension CustomViewController: UITableViewDelegate {

extension CustomViewController: SomeCustomProtocol {

For more information on extensions checkout this discussion.


If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository or comment below.

Summer academy

An iOS Development Summer Course

Design, code and launch your own app. Locations in San Francisco and Asia

Find your location

Product College

A computer science college

Graduate into a successful career as a founder or software engineer.

Learn more

Cookies on Make School's website

We have placed cookies on your device to ensure that we give you the best experience on our website.

This site uses cookies to deliver our services. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of Make School’s Products and Services is subject to these policies and terms.

Please note that Make School no longer supports Internet Explorer

We recommend upgrading to a modern web browser. Learn more