Swift Tutorial

Protocols

Interface-based programming and the power of Protocol Extensions

What is a Protocol?

A Protocol defines a blueprint of methods, properties, and other requirements that suit a particular task. Classes, Structs, and Enums can all adopt a protocol to provide an implementation.
swift
protocol Togglable {
    mutating func toggle()
}

struct LightSwitch: Togglable {
    var isOn = false
    mutating func toggle() { isOn.toggle() }
}
Wait! In Swift, we focus on Protocol-Oriented Programming. This means we design our systems around what objects do (protocols) rather than what they are (inheritance).

Protocol Extensions

Wait! This is a game-changer. You can provide a default implementation for a protocol's methods using an extension. This allows you to add functionality to any type that conforms to that protocol automatically.
swift
extension Togglable {
    func describe() {
        print("This item can be toggled.")
    }
}

Protocol as Type

You can use a protocol as a type, allowing you to store a collection of different objects that all conform to the same protocol.
swift
let items: [Togglable] = [LightSwitch(), DoorLock()]

Summary

TermDescription
protocolThe keyword to define a blueprint
conformsWhen a type implements a protocol's requirements
anyany Protocol - A type-erased box for a protocol
somesome Protocol - An opaque type (Used heavily in SwiftUI)
DelegationA common pattern where a protocol is used to communicate between objects
Best ForDecoupling code and creating flexible, reusable components
Logic"Design the interface first"
SafetyCompiler ensures that all requirements are met
ModernThe "Swift way" of building large systems
StandardUse protocols like Identifiable or Equatable from the std lib
IdentityProtocols can also specify required properties (get, set)
InheritProtocols can inherit from other protocols