Python Protocols, Protocol Extensions & POP (Protocol-Oriented Programming)
Swift is a Protocol-Oriented language. Learn how protocols define blueprints of methods and properties, how extensions add functionality to existing types without subclassing, and how protocol extensions provide default implementations.
"A protocol is like a job contract: it states that anyone holding the title of "Driver" must know how to steer and brake. A protocol extension is a free company training manual providing a standard default steering technique to anyone who signs the contract."
Deep Dive: How It Works
Protocols: Define requirements (properties with { get set }, methods, inits) without concrete implementations.
Extensions: Add new methods, computed properties, and protocol conformances to existing types (even types you don’t own, like String or Int).
Protocol Extensions: Provide default method implementations for protocols, enabling mixin-like trait composition without inheritance.
Common Standard Protocols: Identifiable, Equatable, Hashable, Comparable, and CustomStringConvertible.
Syntax Blueprint
protocol Describable {
var description: String { get }
}
extension Describable {
func printDescription() { print(description) }
}Define protocol contracts and extend protocols with default implementation methods.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Trying to add stored properties inside an extension.Why it happens: Extensions can only add computed properties and methods, not stored instance state.
How to fix: Declare stored properties in the main type definition and use extensions for computed helpers and protocol conformance.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Extend Int with Squared Helper
Write an extension on Int with a computed property var squared: Int { self * self }.
In top-level code, declare let num = 8 and print "8 squared: \(num.squared)".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.