← Back to Swift Course | Chapter 9: Inheritance & Protocols | Lesson 6 of 7

Protocol Extensions

A protocol extension lets you write actual code once, right on the checklist itself, so every type that conforms automatically gets that behavior for free.
Syntax
swift
protocol ProtocolName {
    var property: Type { get }
}

extension ProtocolName {
    func method() -> ReturnType {   // default implementation
        return value
    }
}

Providing a Default Implementation

A protocol extension can implement a method directly, so any conforming type gets that behavior automatically without writing it themselves.

Example: Providing a Default Implementation

markup
// Define a protocol `Greetable` describing required behavior
protocol Greetable {
    var name: String { get }
}
// Extend `Greetable` with additional functionality
extension Greetable {
    // Define the function `greet` with no parameters and returning a String
    func greet() -> String {
        // Return `"Hello, \(name)!"` from this function
        return "Hello, \(name)!"
    }
}
// Define a struct `Visitor`
struct Visitor: Greetable {
    var name: String
}
// Declare a constant `visitor`, set to `Visitor(name: "Sam")`
let visitor = Visitor(name: "Sam")
// Print `visitor.greet()` to the console
print(visitor.greet())

Overriding a Default Implementation

A conforming type can still provide its own implementation of a method that a protocol extension already defines, which takes priority for that type.

Example: Overriding a Default Implementation

markup
// Define a protocol `Greetable` describing required behavior
protocol Greetable {
    var name: String { get }
}
// Extend `Greetable` with additional functionality
extension Greetable {
    // Define the function `greet` with no parameters and returning a String
    func greet() -> String {
        // Return `"Hello, \(name)!"` from this function
        return "Hello, \(name)!"
    }
}
// Define a struct `Robot`
struct Robot: Greetable {
    var name: String
    // Define the function `greet` with no parameters and returning a String
    func greet() -> String {
        // Return `"BEEP \(name) BOOP"` from this function
        return "BEEP \(name) BOOP"
    }
}
// Declare a constant `robot`, set to `Robot(name: "Unit1")`
let robot = Robot(name: "Unit1")
// Print `robot.greet()` to the console
print(robot.greet())
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming a protocol extension's default implementation always runs; a conforming type can still override it with its own implementation.
  2. Forgetting that protocol extensions cannot add new required members -- they only provide default implementations or entirely new convenience methods.
  3. Adding stored properties in a protocol extension, which is not allowed; only computed properties and methods can be added.
Chapter Summary
  • A protocol extension provides a default implementation for a method or computed property.
  • Any conforming type automatically gets the default unless it provides its own implementation.
  • Extensions cannot add stored properties or new required members to a protocol.
  • This pattern reduces repeated boilerplate across many conforming types.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.