0
0
Swiftprogramming~10 mins

Protocol as types in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Protocol as types
Define Protocol
Create Structs/Classes
Use Protocol as Type
Call Protocol Methods
Execute Concrete Implementation
Output Result
This flow shows how a protocol defines a blueprint, structs/classes conform to it, and the protocol is used as a type to call methods on different concrete types.
Execution Sample
Swift
protocol Vehicle {
    func drive() -> String
}

struct Car: Vehicle {
    func drive() -> String { "Car is driving" }
}

let myVehicle: Vehicle = Car()
print(myVehicle.drive())
Defines a Vehicle protocol, a Car struct conforming to it, then uses Vehicle as a type to call drive() on a Car instance.
Execution Table
StepActionEvaluationResult
1Define protocol Vehicle with drive()Protocol Vehicle createdVehicle requires drive() method
2Define struct Car conforming to VehicleCar implements drive()Car can be used as Vehicle
3Create instance myVehicle of type Vehicle assigned Car()myVehicle holds Car instancemyVehicle is Vehicle type holding concrete Car
4Call myVehicle.drive()Calls Car's drive()Returns "Car is driving"
5Print resultOutput to consoleCar is driving
💡 Execution ends after printing the drive() method result
Variable Tracker
VariableStartAfter Step 3After Step 4Final
myVehiclenilCar instanceCar instanceCar instance
Key Moments - 3 Insights
Why can we assign a Car instance to a variable of type Vehicle?
Because Car conforms to the Vehicle protocol, it can be treated as a Vehicle type, as shown in execution_table step 3.
When calling drive() on myVehicle, which implementation runs?
The Car's implementation runs because myVehicle holds a Car instance, as shown in execution_table step 4.
Can we create an instance of Vehicle directly?
No, protocols cannot be instantiated directly; they only define a blueprint, as implied in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the type of myVehicle after step 3?
ACar type directly
BVehicle holding a Car instance
CProtocol Vehicle itself
DNil
💡 Hint
Check execution_table row 3 under 'Result' and 'Evaluation'
At which step does the Car's drive() method get called?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table row 4 under 'Action' and 'Evaluation'
If Car did not conform to Vehicle, what would happen at step 3?
ACompilation error assigning Car to Vehicle
BRuntime error when calling drive()
CmyVehicle would hold Car instance anyway
Ddrive() would call default protocol implementation
💡 Hint
Protocols require conforming types; see key_moments about assignment and conformance
Concept Snapshot
protocol ProtocolName {
    func method() -> ReturnType
}

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

Use protocol as type:
let variable: ProtocolName = TypeName()
variable.method() calls concrete implementation.
Full Transcript
This example shows how a protocol defines a method requirement. A struct Car conforms to the Vehicle protocol by implementing the drive() method. We create a variable myVehicle of type Vehicle and assign it a Car instance. When we call drive() on myVehicle, the Car's implementation runs, demonstrating how protocols can be used as types to abstract over different concrete implementations.