0
0
Swiftprogramming~10 mins

Extending built-in types in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extending built-in types
Start
Define Extension
Add new method/property
Use extended type
Call new method/property
Get result
End
You start by defining an extension to a built-in type, add new methods or properties, then use the extended type to call those new features.
Execution Sample
Swift
extension Int {
    func squared() -> Int {
        return self * self
    }
}

let number = 5
print(number.squared())
This code adds a new method 'squared' to Int that returns the number multiplied by itself, then prints 25.
Execution Table
StepActionEvaluationResult
1Define extension Int with method squared()Extension createdInt now has squared() method
2Create constant number = 5number assigned 5number = 5
3Call number.squared()Calculate 5 * 525
4Print resultOutput 25Console shows 25
💡 Program ends after printing the squared value
Variable Tracker
VariableStartAfter Step 2After Step 3Final
numberundefined555
result of squared()undefinedundefined2525
Key Moments - 2 Insights
Why can we call squared() on an Int even though Int doesn't have it originally?
Because the extension adds the squared() method to Int before we call it, as shown in step 1 of the execution_table.
Does the original Int type change permanently after extension?
No, extensions add functionality only in the current scope or module; the original Int type remains unchanged elsewhere.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'number' after step 2?
Aundefined
B25
C5
D0
💡 Hint
Check the 'variable_tracker' table under 'number' after Step 2
At which step is the squared() method actually called?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table for when number.squared() is called
If we changed number to 3, what would be the output at step 4?
A6
B9
C3
D25
💡 Hint
Recall squared() returns number * number; see execution_table step 3 for calculation
Concept Snapshot
Swift extensions let you add new methods or properties to existing types.
Syntax: extension TypeName { func newMethod() { } }
You can call new methods on instances as if built-in.
Extensions do not modify original type permanently.
Useful to add functionality without subclassing.
Full Transcript
This example shows how to extend Swift's built-in Int type by adding a new method called squared(). The extension is defined first, adding the method that returns the number multiplied by itself. Then, a constant number is created with value 5. Calling number.squared() runs the new method, calculating 25. Finally, the result 25 is printed to the console. The execution table traces each step: defining the extension, creating the number, calling squared(), and printing the result. The variable tracker shows how 'number' stays 5 and the squared() result is 25. Key moments clarify that extensions add methods to types so you can call them like built-in methods, but the original type is not permanently changed. The quiz tests understanding of variable values and method calls during execution. The snapshot summarizes how extensions work in Swift simply and clearly.