0
0
Swiftprogramming~10 mins

Protocol conformance via extension in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Protocol conformance via extension
Define Protocol
Define Type (struct/class)
Extend Type
Add Protocol Conformance in Extension
Use Type as Protocol
Call Protocol Methods
Execution of Protocol Methods
First, we define a protocol and a type. Then, we extend the type to add protocol conformance. Finally, we use the type as the protocol and call its methods.
Execution Sample
Swift
protocol Greetable {
    func greet() -> String
}

struct Person {
    let name: String
}

extension Person: Greetable {
    func greet() -> String {
        "Hello, \(name)!"
    }
}
This code defines a protocol Greetable, a struct Person, and then extends Person to conform to Greetable by implementing greet().
Execution Table
StepActionEvaluationResult
1Define protocol Greetable with greet()Protocol createdGreetable protocol exists
2Define struct Person with property nameType createdPerson struct exists with name property
3Extend Person to conform to GreetableAdd greet() methodPerson now conforms to Greetable
4Create instance p = Person(name: "Alice")Instance createdp.name = "Alice"
5Call p.greet()Calls greet() from extension"Hello, Alice!" returned
6Assign p to variable of type GreetableType castp as Greetable works
7Call greet() via Greetable variableCalls greet()"Hello, Alice!" returned
💡 All steps complete, protocol conformance via extension works as expected
Variable Tracker
VariableStartAfter Step 4After Step 5Final
pnilPerson(name: "Alice")Person(name: "Alice")Person(name: "Alice")
p.namenil"Alice""Alice""Alice"
greet() outputnilnil"Hello, Alice!""Hello, Alice!"
Key Moments - 3 Insights
Why can we add protocol conformance in an extension instead of in the original struct?
Extensions allow adding protocol conformance separately from the original type definition, as shown in step 3 of the execution_table.
Does the instance p have access to greet() even though it was added in an extension?
Yes, as shown in step 5, p.greet() works because the extension adds the method to Person.
Can we use the instance p as the protocol type Greetable?
Yes, step 6 shows p can be assigned to a Greetable variable and used to call greet().
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of p.greet() at step 5?
Anil
B"Hello, Person!"
C"Hello, Alice!"
DCompilation error
💡 Hint
Check the Result column at step 5 in the execution_table
At which step does Person gain conformance to Greetable?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the Action and Result columns in the execution_table for when conformance is added
If we remove the greet() method from the extension, what happens when calling p.greet() at step 5?
AIt returns "Hello, Alice!" anyway
BCompilation error because Person does not conform
CReturns nil
DRuns but returns empty string
💡 Hint
Refer to the key_moments about protocol conformance and method availability
Concept Snapshot
protocol ProtocolName {
    func method() -> ReturnType
}

struct TypeName { ... }

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

- Extensions can add protocol conformance
- Methods added in extensions are available on instances
- Enables separation of concerns and cleaner code
Full Transcript
This example shows how to make a Swift type conform to a protocol using an extension. First, we define a protocol named Greetable with a greet() method. Then, we define a struct Person with a name property. Next, we extend Person to conform to Greetable by implementing the greet() method in the extension. We create an instance of Person named p with the name "Alice". Calling p.greet() returns "Hello, Alice!". We can also assign p to a variable of type Greetable and call greet() through that variable. This demonstrates that protocol conformance can be added separately from the original type definition, and methods added in extensions are fully available on instances.