0
0
Swiftprogramming~10 mins

Protocol declaration syntax in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Protocol declaration syntax
Start Protocol Declaration
Write 'protocol' keyword
Name the Protocol
Define Requirements (properties, methods)
Close Protocol Declaration
End
This flow shows how to declare a protocol: start with the keyword, name it, add required properties and methods, then close.
Execution Sample
Swift
protocol Vehicle {
    var numberOfWheels: Int { get }
    func drive()
}
Declares a protocol named Vehicle with a read-only property and a method requirement.
Execution Table
StepActionCode PartResult
1Start protocol declarationprotocolBegin defining a protocol
2Name the protocolVehicleProtocol named Vehicle created
3Declare property requirementvar numberOfWheels: Int { get }Read-only property required
4Declare method requirementfunc drive()Method drive() required
5Close protocol declaration}Protocol Vehicle fully declared
💡 Protocol declaration ends after closing brace '}'
Variable Tracker
ElementStartAfter Step 2After Step 3After Step 4Final
Protocol NamenullVehicleVehicleVehicleVehicle
PropertiesnullnullnumberOfWheels (read-only)numberOfWheels (read-only)numberOfWheels (read-only)
Methodsnullnullnulldrive()drive()
Key Moments - 2 Insights
Why do we use { get } after the property declaration?
The { get } means the property must be readable (a getter), but it does not have to be writable. See execution_table step 3.
Can protocols contain code implementations inside?
No, protocols only declare requirements (like properties and methods), but do not provide code. This is shown in execution_table steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is declared at step 3?
AA read-only property requirement
BA method implementation
CThe protocol name
DClosing the protocol
💡 Hint
Check the 'Code Part' and 'Result' columns at step 3 in execution_table
At which step is the protocol name defined?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Code Part' columns in execution_table
If we add { get set } to the property, how would variable_tracker change?
AProtocol name would change
BMethods would change
CProperties would be writable and readable
DNo change at all
💡 Hint
Refer to variable_tracker row 'Properties' and understand { get set } means read and write
Concept Snapshot
protocol ProtocolName {
    var propertyName: Type { get / get set }
    func methodName()
}
- Use 'protocol' keyword to declare
- Define required properties and methods
- Properties can be read-only ({ get }) or read-write ({ get set })
Full Transcript
This visual trace shows how to declare a protocol in Swift. First, write the keyword 'protocol', then name your protocol. Next, declare required properties with { get } to indicate read-only or { get set } for read-write. Then declare required methods without implementation. Finally, close the declaration with a brace. Protocols only specify what is required, not how it works.