Member-only story

Protocols in Swift

Protocols are extremely important in Swift

Steven Curtis
5 min readJan 21, 2020

A protocol allows objects, and provide a concrete implementation of any object that conforms to it. We can think of a protocol as a set of rules and guidelines that an object can conform to

For the examples, read on.

Photo by Mike Meyers on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • Use of extensions (guide HERE)
  • Some knowledge of Object-oriented programming, specifically overriding (guide HERE)

Terminology

class: An object defined in Swift, using pass by reference semantics

conforms: conformance is if an object adopts a protocol (or inherits it from another class) and contains the functionality specified

enum: A type consisting of a set of named values, called members

extensions: Extensions add new functionality to a class, struct, enum or protocol

inheritance: The mechanism in which one class acquires the property of another class

method: A group of statements that together can perform a function

protocol: A blueprint on methods, properties and requirements to suit a piece of functionality

property: An association of a value with a class, structure or enumeration

struct: An object defined in Swift, using pass by value semantics

The origin of protocols

In natural English a protocol is a say that two parties agree to communicate. A simple example of this is a traditional handshake in England (an alternative is the bow in Japan).

In computing this idea has often been adopted in communications, for example the sending of acknowledgements in the Internet Protocol.

The Transmission Control Protocol

The meaning of protocol in Swift is tightly coupled to this meaning, although it is subtly…

--

--

No responses yet

Write a response