Protocol-oriented programming helps you write clear and reusable code by focusing on what things can do, not what they are.
Why protocol-oriented programming matters in Swift
protocol SomeProtocol { func doSomething() } struct SomeStruct: SomeProtocol { func doSomething() { // implementation } }
A protocol defines a blueprint of methods or properties.
Types like structs, classes, or enums can adopt protocols to promise they implement those methods.
Drivable and a struct Car that follows it.protocol Drivable { func drive() } struct Car: Drivable { func drive() { print("Car is driving") } }
id. The struct User provides it.protocol Identifiable { var id: String { get } } struct User: Identifiable { var id: String }
This program shows how different types can follow the same protocol and be used interchangeably.
protocol Greetable { func greet() } struct Person: Greetable { var name: String func greet() { print("Hello, my name is \(name)") } } struct Robot: Greetable { func greet() { print("Beep boop, I am a robot") } } func sayHello(to greeter: Greetable) { greeter.greet() } let alice = Person(name: "Alice") let bot = Robot() sayHello(to: alice) sayHello(to: bot)
Protocols let you write code that works with many types, making your code flexible.
Using protocols can reduce the need for complex class inheritance.
Swift's standard library uses protocols a lot, so learning them helps you understand Swift better.
Protocols define what actions or properties a type must have.
Protocol-oriented programming focuses on behavior, not just data.
This approach makes your code easier to reuse and maintain.
Practice
Solution
Step 1: Understand what protocols represent
Protocols specify a set of methods and properties that a type must have, acting as a blueprint.Step 2: Differentiate protocols from data storage
Protocols do not store data themselves; they only define behavior and requirements.Final Answer:
To define a blueprint of methods and properties that a type must implement -> Option AQuick Check:
Protocols = Blueprint of behavior [OK]
- Thinking protocols store data
- Confusing protocols with classes or structs
- Assuming protocols manage memory
Drivable in Swift?Solution
Step 1: Recall protocol declaration syntax
Protocols are declared using the keywordprotocolfollowed by the name and curly braces.Step 2: Identify correct method declaration inside protocol
Inside protocols, methods are declared without implementation, just the signature.Final Answer:
protocol Drivable { func drive() } -> Option DQuick Check:
Use 'protocol' keyword and method signature [OK]
- Using class or struct instead of protocol
- Trying to implement method inside protocol
- Incorrect keyword order
protocol Greetable {
func greet() -> String
}
struct Person: Greetable {
func greet() -> String {
"Hello!"
}
}
let p = Person()
print(p.greet())Solution
Step 1: Check protocol and struct conformance
The structPersonconforms toGreetableand implementsgreet()returning "Hello!".Step 2: Understand the print statement output
Callingp.greet()returns "Hello!", which is printed.Final Answer:
Hello! -> Option CQuick Check:
Protocol method implemented returns "Hello!" [OK]
- Assuming protocol methods print automatically
- Thinking missing implementation error occurs
- Confusing method name with output
protocol Runnable {
func run()
}
struct Athlete: Runnable {
// Missing run() implementation
}
let athlete = Athlete()
athlete.run()Solution
Step 1: Check protocol requirements
The protocolRunnablerequires arun()method to be implemented.Step 2: Verify struct conformance
The structAthletedoes not implementrun(), so it does not conform properly.Final Answer:
Athlete struct must implement run() method -> Option AQuick Check:
Protocol methods must be implemented in conforming types [OK]
- Assuming protocols can't be used with structs
- Thinking run() must return a value
- Believing instance creation is the error
Solution
Step 1: Understand protocol-oriented programming benefits
Protocols with default implementations allow behavior sharing without inheritance.Step 2: Evaluate options for reuse and maintenance
Extending a protocol with defaultlog()method lets many types reuse logging easily and maintain code in one place.Final Answer:
Define a protocolLoggablewith alog()method, then extend it with default implementation -> Option BQuick Check:
Protocols + extensions = reusable, maintainable code [OK]
- Relying on inheritance instead of protocols
- Duplicating code in each class
- Using global variables for behavior
Ready to prove it?
Pass the test and this pattern moves unknown
