Protocols and Delegates in Swift (3.0):

A protocol defines a blueprint of methods, properties, and other requirements for a particular task or functionality. The protocol is then, a required task that can then be adopted by a class, structure, or enumeration to provide an implementation of those requirements. When a type satisfies the requirements is then said to conform to that protocol.

Syntax

Protocols also follow similar syntax as that of classes, structures, and enumerations −

protocol SomeProtocol {
// protocol definition
}

Protocols are declared after the class, structure or enumeration type names. It is also possible to have single and multiple protocol declarations. If multiple protocols are defined they have to be separated by commas as in the following example:

struct SomeStructure: Protocol1, Protocol2 {
// structure definition
}

When a protocol has to be defined for a super class, the protocol name should follow the super class name with a comma. For example:

class SomeClass: SomeSuperclass, Protocol1, Protocol2 {
// class definition
}

Protocols can have stored or computed properties, instance or mutating methods or functions. However, if they are computed properties, variable instances or mutating functions (with no curly braces), then they cannot be declared with a constant but with a var (variable) instead in order to conform to the protocol.

Moreover, protocols are types and begin their name with capital letters. According to the Apple Documentation:

Apple Documentation: Swift 3.0

Not only protocols are a type but they can also be extended, they can require specific initializers to be implemented by conforming types and overall require more functionality or a variety of specifications for a conforming type.

A frequent use of a protocol for a Swift developer is when implementing a design pattern or delegate. A Delegate is a design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type. According to the Apple Documentation:

This design pattern is implemented by defining a protocol that encapsulates the delegated responsibilities, such that a conforming type (known as a delegate) is guaranteed to provide the functionality that has been delegated. 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.

One frequent example an iOS Developer can encounter the protocol and delegate pattern is when implementing a TableView to a regular ViewController. A UITableView object must have an object that acts as a data source and an object that acts as a delegate; typically these objects are either the application delegate or, more frequently, a custom UITableViewController object. Although they have many optional methods, there are 2 that are always required in order to conform to the DataSource protocol specifically according to the Apple Documentation:

tableView(UITableView, numberOfRowsInSection: Int)

Required. Tells the data source to return the number of rows in a given section of a table view.

func tableView(UITableView, cellForRowAt: IndexPath)

Required. Asks the data source for a cell to insert in a particular location of the table view.

In other words, the datasource provides information that UITableView needs to construct tables and manages the data model when rows of a table are inserted, deleted, or reordered.

In addition to the datasource, the delegate of a UITableView object must adopt the UITableViewDelegate protocol. The delegate manages table row configuration and selection, row reordering, highlighting, accessory views, and editing operations. In the case where are tableView is used with A regular UIViewController, then the regular ViewController is the delegate.

The above is a simple example that Swift Developers encounter regularly and that Apples provides for us. But also, programmers are not limited to Apple’s protocols and delegates. We can make our own for our code when programming and write our own protocols and delegates when useful.

For more about this concept and related topics, you can see the the Apple Documentation: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.