Skip to content
Prepare to Grow

Swift Fundamentals

Protocols and Extensions

Define shared capabilities and add focused behavior without changing original declarations.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Declare protocol requirements.
  • Make types conform to protocols.
  • Add behavior with extensions.
  • Use protocol extensions for shared default implementations.
01

Learning stage

Concept Explanation

A protocol describes requirements that a conforming type must satisfy, such as properties or methods. It expresses capability without specifying one concrete implementation type.

An extension adds methods, computed properties, initializers, or conformances to an existing type. A protocol extension can provide shared implementations to all conforming types.

  • Protocols describe required interfaces.
  • Conformance promises that a type satisfies those requirements.
  • Extensions organize additional behavior.
  • Protocol extensions can supply reusable default behavior.
02

Learning stage

Syntax & Examples

Two different models conform to the same progress-reporting capability and receive a shared summary.

ProgressReporting.swiftswift
protocol ProgressReporting {
  var completedUnits: Int { get }
  var totalUnits: Int { get }
}

extension ProgressReporting {
  var progressText: String {
      "\(completedUnits) of \(totalUnits) complete"
  }
}

struct Course: ProgressReporting {
  let completedUnits: Int
  let totalUnits: Int
}

let swift = Course(completedUnits: 4, totalUnits: 11)
print(swift.progressText)
Expected output: “4 of 11 complete”.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • Abstract services so production and test implementations can be substituted.
  • Describe shared capabilities across unrelated model types.
  • Organize framework conformances and focused helpers in extensions.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Creating a protocol before more than one meaningful implementation or testing seam exists.
  • Making one large protocol that forces unrelated requirements on conforming types.
  • Using an extension as a dumping ground for unrelated helpers.
  • Assuming a protocol extension changes stored layout; extensions cannot add stored properties.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

Activity 1

Define a capability

Create a Describable protocol with a description property, then conform two unrelated structs to it.

Hint: Focus on what callers need rather than all data each type stores.

Activity 2

Provide shared behavior

Add a protocol extension method that prints or returns a formatted description for every conforming value.

Hint: Build the default from protocol requirements.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

Does a protocol provide one concrete stored representation?

Reveal answer

No. It defines requirements or capabilities that conforming types implement.

QUESTION 2

Can an extension add a stored property to an existing type?

  1. A.No
  2. B.Yes, always
  3. C.Only to classes
  4. D.Only to protocols
Reveal answer

No

Extensions can add computed properties and behavior, but not new stored properties.

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

Why use protocols in Swift?

Short answer: Protocols let code depend on required capabilities instead of one concrete type.

Explore the detailed answer

A protocol establishes a contract that multiple types can satisfy. This supports substitution, focused interfaces, testing seams, and generic algorithms. Protocol extensions can share behavior while conforming types retain their own data and identity.

Possible follow-ups

  • • What can a protocol require?
  • • What are protocol extension defaults?
08

Learning stage

Key Takeaways

Protocols create capability boundaries; extensions add focused behavior and conformance. Introduce them when they simplify real variation, not only to add abstraction.

  • Protocols define requirements.
  • Concrete types opt into conformance.
  • Extensions add behavior without subclassing.
  • Protocol extensions can share implementations.