0
0
Swiftprogramming~10 mins

Why protocol-oriented programming matters in Swift - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why protocol-oriented programming matters
Define Protocol
Create Structs/Classes
Conform to Protocol
Use Protocol Type
Call Protocol Methods
Benefit: Reuse & Flexibility
This flow shows how defining a protocol and making types conform to it allows flexible and reusable code.
Execution Sample
Swift
protocol Greetable {
    func greet() -> String
}

struct Person: Greetable {
    func greet() -> String { "Hello!" }
}

let p: Greetable = Person()
print(p.greet())
This code defines a protocol, makes a struct conform to it, and calls the protocol method via a protocol-typed variable.
Execution Table
StepActionEvaluationResult
1Define protocol Greetable with greet()Protocol createdGreetable protocol exists
2Define struct Person conforming to GreetablePerson implements greet()Person struct ready
3Create variable p of type Greetable assigned Person()p holds Person instancep is Person as Greetable
4Call p.greet()Calls Person.greet()Returns "Hello!"
5Print resultOutput to consoleHello!
💡 Program ends after printing greeting from protocol method
Variable Tracker
VariableStartAfter Step 3After Step 4Final
pnilPerson instance as GreetablePerson instance as GreetablePerson instance as Greetable
Key Moments - 2 Insights
Why can we call greet() on variable p even though p is of protocol type?
Because p is declared as Greetable and Person conforms to Greetable, so p must implement greet(). See execution_table step 4.
Why use a protocol type variable instead of a concrete type?
Using a protocol type allows flexibility to assign any conforming type to p, enabling reusable and interchangeable code. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of p after step 3?
AString "Hello!"
Bnil
CPerson instance as Greetable
DProtocol Greetable itself
💡 Hint
Check variable_tracker column 'After Step 3' for variable p
At which step does the program print the greeting message?
AStep 5
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row where action is 'Print result'
If Person did not conform to Greetable, what would happen at step 3?
Ap would hold Person instance anyway
BCompilation error because Person doesn't conform
Cp would be nil
Dp would hold a default implementation
💡 Hint
Recall that protocol conformance is required to assign to protocol type variable (see step 3)
Concept Snapshot
protocol ProtocolName {
    func method() -> ReturnType
}

struct TypeName: ProtocolName {
    func method() -> ReturnType { ... }
}

// Use protocol type for flexibility
let variable: ProtocolName = TypeName()
variable.method()

// Protocol-oriented programming enables reusable, flexible code.
Full Transcript
This example shows how protocol-oriented programming works in Swift. First, we define a protocol named Greetable with a greet() method. Then, we create a struct Person that conforms to Greetable by implementing greet(). We create a variable p of type Greetable and assign it a Person instance. When we call p.greet(), it calls Person's implementation and returns "Hello!". This approach allows us to write flexible code that can work with any type conforming to the protocol, making code reusable and easier to maintain.