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
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
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
A bicycle with 2 wheelsKey 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
1. Which Swift types are allowed to conform to a protocol?
2. What happens if a type does not implement a required method from a protocol it claims to conform to?
3. How does a struct declare that it conforms to a protocol named Drawable?
4. Can a single type conform to more than one protocol?
Was this page helpful?
You May Also Like
Extensions in Swift
Discover how Swift extensions add new functionality to existing types without modifying their original source code.
Protocol-Oriented Programming in Swift
Explore Swift's protocol-oriented design philosophy, which favors composing small protocols over deep class inheritance.
Generics in Swift
Understand how Swift generics let you write flexible, reusable, type-safe code that works with any type.
Structs vs Classes in Swift
Structs use value semantics while classes use reference semantics — the defining choice you make for every custom Swift type.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics