Swift Protocols Cheat Sheet
Covers defining protocols, protocol conformance, protocol extensions with default implementations, and protocol-oriented programming patterns in Swift.
2 PagesIntermediateApr 8, 2026
Defining & Conforming
Declaring a protocol and adopting it in multiple types.
swift
protocol Vehicle { var wheels: Int { get } func drive() -> String}struct Car: Vehicle { var wheels: Int = 4 func drive() -> String { "Driving a car with \(wheels) wheels" }}struct Motorcycle: Vehicle { var wheels: Int = 2 func drive() -> String { "Riding a motorcycle" }}let vehicles: [Vehicle] = [Car(), Motorcycle()]for v in vehicles { print(v.drive())}
Protocol Extensions
Providing default implementations shared by all conforming types.
swift
protocol Greetable { var name: String { get }}// Default implementation shared by every conforming typeextension Greetable { func greet() -> String { "Hello, \(name)!" }}struct Person: Greetable { var name: String // greet() comes free from the extension}struct Robot: Greetable { var name: String // Override the default implementation func greet() -> String { "BEEP BOOP, \(name)" }}print(Person(name: "Ana").greet()) // "Hello, Ana!"print(Robot(name: "R2").greet()) // "BEEP BOOP, R2"
Associated Types
Protocols that act as generic templates via associatedtype.
swift
protocol Container { associatedtype Item var items: [Item] { get set } mutating func add(_ item: Item)}struct Stack<T>: Container { var items: [T] = [] mutating func add(_ item: T) { items.append(item) }}var intStack = Stack<Int>()intStack.add(1)intStack.add(2)// Protocols with associated types can't be used as a plain type// (`Container` alone) -- use `some Container` or generics instead.func printCount(_ container: some Container) { print(container.items.count)}
Protocol Composition
Combining and constraining protocols.
- protocol A & B- Composition type requiring conformance to multiple protocols at once
- Protocol inheritance- `protocol B: A { }` requires conformers of B to also conform to A
- class-only protocol- `protocol Delegate: AnyObject { }` restricts conformance to reference types
- some Protocol- Opaque type; a specific concrete type conforming to the protocol, known at compile time
- any Protocol- Existential type (Swift 5.7+); boxes any conforming type, resolved at runtime
- Extension conformance- Types can be retroactively conformed to a protocol via an extension elsewhere
Common Standard Protocols
Protocols from the Swift standard library you'll conform to often.
- Equatable- Enables == comparison; often synthesized automatically for simple structs
- Hashable- Enables use as a Set element or Dictionary key; implies Equatable
- Comparable- Enables <, >, <=, >= and sorting via sort()
- CustomStringConvertible- Provides a custom `description` property used by print() and string interpolation
- Codable- Combines Encodable and Decodable for JSON/plist (de)serialization
- Identifiable- Requires an `id` property; used heavily by SwiftUI's List and ForEach
Pro Tip
Favor protocol-oriented programming: define behavior in a protocol extension once, and let value types (structs/enums) conform to it, instead of building a class inheritance hierarchy — it avoids fragile base-class problems and works with Swift's value semantics.
Was this cheat sheet helpful?
Explore Topics
#SwiftProtocols#SwiftProtocolsCheatSheet#Programming#Intermediate#DefiningConforming#ProtocolExtensions#AssociatedTypes#ProtocolComposition#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance