100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Protocols in Swift

Learn how Swift protocols define blueprints of methods and properties that structs, classes, and enums can conform to.

Protocols & GenericsIntermediate8 min readJul 8, 2026
Analogies

Introduction

A protocol in Swift defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. Any struct, class, or enum can adopt a protocol and provide an actual implementation of those requirements, at which point it is said to conform to the protocol. Protocols are similar to interfaces in languages like Java or C#, but Swift protocols are more flexible because value types (structs and enums), not just classes, can conform to them.

🏏

Cricket analogy: A protocol is like the ICC's laws of the game defining requirements — bowling action legality, run-scoring rules — that any format from Test to T20 must conform to, similar to how any struct, class, or enum can adopt and implement a protocol's requirements.

Syntax

swift
protocol Drawable {
    var name: String { get }
    func draw()
}

struct Circle: Drawable {
    var name: String
    func draw() {
        print("Drawing circle: \(name)")
    }
}

Explanation

The protocol Drawable declares a read-only property requirement name and a method requirement draw(). The struct Circle conforms to Drawable by writing a colon after the type name followed by the protocol name, and then supplying concrete implementations for every requirement. If Circle failed to implement draw() or name, the compiler would raise an error, because conformance is checked and enforced at compile time. A single type can conform to multiple protocols by separating them with commas, e.g. struct Circle: Drawable, Equatable.

🏏

Cricket analogy: Drawable's name and draw() are like a bowler's registered name and required action; Circle conforms by writing 'RightArmFast: Bowler' and supplying the actual bowling technique — skip either and the umpire (compiler) calls it a no-ball at compile time, and a player can conform to both Bowler and Fielder.

Example

swift
protocol Vehicle {
    var wheels: Int { get }
    func describe() -> String
}

enum Bicycle: Vehicle {
    case mountain, road

    var wheels: Int { 2 }

    func describe() -> String {
        return "A bicycle with \(wheels) wheels"
    }
}

let ride = Bicycle.mountain
print(ride.describe())

Output

swift
A bicycle with 2 wheels

Key Takeaways

  • Protocols define requirements (methods, properties, initializers) without providing implementations.
  • Structs, classes, and enums can all conform to protocols, unlike class-only inheritance.
  • A type can conform to multiple protocols at once, separated by commas.
  • Conformance is checked at compile time, so missing requirements produce a build error.
  • Protocols enable polymorphism across unrelated types that share behavior.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#ProtocolsInSwift#Protocols#Syntax#Explanation#Example#StudyNotes#SkillVeris