Protocol Basics in Swift
From the Swift cheat sheet · Protocols · verified Jul 2026
Protocol Basics
Defining and adopting protocols
swift
// Protocol definition
protocol Vehicle {
var numberOfWheels: Int { get }
var color: String { get set }
func start()
func stop()
}
// Protocol adoption
struct Car: Vehicle {
let numberOfWheels = 4
var color: String
func start() {
print("Engine started")
}
func stop() {
print("Brakes applied")
}
}
// Multiple protocols
protocol Named {
var name: String { get }
}
protocol Aged {
var age: Int { get }
}
struct Person: Named, Aged {
let name: String
let age: Int
}💡 Protocols define a blueprint of requirements
⚡ Protocol extensions provide default implementations
📌 Associated types make protocols generic
🟢 Use & for protocol composition