All about protocols in swift

Protocol oriented programming , like functional programming is an important concept in swift . I am learning protocols in swift and I am documenting it here. Everything I know about protocols will be here in this article. Do read and update your knowledge.

So, keeping it simple, a protocol says a struct , class or enum that if you want to be THAT, do THIS, this and THis. Example: if you want to be a human, you have to EAT, SLEEP, and Take REST and repeat!!! 🤓.

Protocol Syntax

protocol syntax
struct adopting multiple protocols
Classes with superclass
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { }
class ViewController: UIViewController {}extension ViewController: UITableViewDataSource, UITableViewDelegate
//implement protocol methods ands variables here..

Adding Property Requirements

  • A protocol can have properties as well as methods that a class, enum or struct conforming to this protocol can implement.
  • A protocol declaration only specifies the required property name and type. It doesn’t say anything about whether the property should be a stored one or a computed one.
  • A protocol also specifies whether each property must be gettable or gettable and settable.
  • Property requirements are always declared as variable properties, prefixed with the var keyword.
  • Gettable and settable properties are indicated by writing { get set } after their type declaration, and gettable properties are indicated by writing { get }.
get-set and get
  • A { get set } property cannot be a constant stored property. It should be a computed property and both get and set should be implemented.
  • A { get } property can be any kind of property, and it is valid for the property to be also settable if required.
  • Always prefix type property requirements with the static keyword when you define them in a protocol. This rule pertains even though type property requirements can be prefixed with the class or static keyword when implemented by a class:
type properties in a protocol declaration

What does conforming to a protocol means?

simple protocol conformation
protocol conformation as computed property

Method Requirements

As mentioned earlier, protocols can have methods as well.

  • A protocol can have type methods or instance methods.
  • Methods are declared in exactly the same way as for normal instance and type methods, but without curly braces or a method body.
  • Variadic parameters are allowed.
  • Default values are not allowed.
  • As with type property requirements, you always prefix type method requirements with the static keyword when they are defined in a protocol. This is true even though type method requirements are prefixed with the class or static keyword when implemented by a class:
Struct conforming to a protocol having methods
class conforming to a protocol having methods

Protocols with mutating methods

source: Article

Struct with mutation functions

Coming back to mutating method requirements:

If you define a protocol instance method requirement that is intended to mutate instances of any type that adopts the protocol, mark the method with the mutating keyword as part of the protocol’s definition.

enum and class implementing a protocol with mutating function

Initializer Requirements

Protocols can have specific initializers like normal methods which the conforming types can implement.

protocol with initializers

Class Implementations of Protocol Initializer Requirements

You can implement a protocol initializer requirement on a conforming class as either a designated initializer or a convenience initializer. In both cases, you must mark the initializer implementation with the requiredmodifier:

class conforming to protocol initializers
protocol SomeProtocol {init()}class SomeSuperClass {init() {// initializer implementation goes here}}class SomeSubClass: SomeSuperClass, SomeProtocol {// "required" from SomeProtocol conformance; "override" from SomeSuperClassrequired override init() {// initializer implementation goes here}}

Failable Initializer Requirements

Protocols can have failable initializers. A failable initializer requirement can be satisfied by a failable or nonfailable initializer on a conforming type. A nonfailable initializer requirement can be satisfied by a nonfailable initializer or an implicitly unwrapped failable initializer.

Protocols as Types

Protocol is a type. You can use it in many places like:

  • As a parameter type or return type in a function, method, or initializer
  • As the type of a constant, variable, or property
  • As the type of items in an array, dictionary, or other container
  • Because protocols are types, begin their names with a capital letter to match the names of other types in Swift (such as Int, String, and Double).


Delegation 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. Delegation pattern can also be used as a callback kind of mechanism.

delegation callback

Adding Protocol Conformance with an Extension

You can extend an existing type to adopt and conform to a new protocol, even if you do not have access to the source code for the existing type. Extensions can add new properties, methods, and subscripts to an existing type, and are therefore able to add any requirements that a protocol may demand.

Adding Protocol Conformance with an Extension

Declaring Protocol Adoption with an Extension

If a type already conforms to all of the requirements of a protocol, but has not yet stated that it adopts that protocol, you can make it adopt the protocol with an empty extension:

Declaring Protocol Adoption with an Extension

Collections of Protocol Types

Protocols can be used as a type to be stored in collection types like array or dictionary.

Collections of Protocol Types

Protocol Inheritance

A protocol can inherit one or more other protocols. The syntax of protocol inheritance is similar to class inheritance.

protocol inheritance

Class-Only Protocols

You can limit protocol adoption to class types (and not structures or enumerations) by adding the AnyObject or class protocol to a protocol’s inheritance list.

Class-Only Protocols
error: non-class type ‘XXXXXXXX’ cannot conform to class protocol “YYYYYY.”

Protocol Composition

Sometimes it is required for a type to conform to multiple protocols. Image a function which accepts a parameter which should conform to multiple protocols.

Protocol Composition swift 4

Checking for Protocol Conformance

You can use the is and as operators described in Type Casting to check for protocol conformance, and to cast to a specific protocol. Checking for and casting to a protocol follows exactly the same syntax as checking for and casting to a type:

  • The is operator returns true if an instance conforms to a protocol and returns false if it does not.
  • The as? version of the downcast operator returns an optional value of the protocol’s type, and this value is nil if the instance does not conform to that protocol.
  • The as! version of the downcast operator forces the downcast to the protocol type and triggers a runtime error if the downcast does not succeed.

Optional Protocol Requirements

Protocol can have optional methods and properties.These requirements do not have to be implemented by types that conform to the protocol.

First, we add @objc keyword in front of the protocol keyword. Then we add the @objc optional keyword in front of our optional method/variable.

When you use a method or property in an optional requirement, its type automatically becomes an optional.

optional Protocol Requirements

Protocol Extensions

Protocols can be extended to provide method and property implementations to conforming types. This allows you to define behavior on protocols themselves, rather than in each type’s individual conformance or in a global function.

Protocol extensions

Providing Default Implementations using protocol Extensions

Protocol extension can be used for providing default implementation within the protocol itself as explained in the previous section.

Providing Default Implementations using protocol Extensions

Adding Constraints to Protocol Extensions

When you define a protocol extension, you can specify constraints that conforming types must satisfy before the methods and properties of the extension are available. You write these constraints after the name of the protocol you’re extending using a generic where clause.

Adding Constraints to Protocol Extensions

If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

You can follow me on Medium for fresh articles. Connect with me on LinkedIn.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store