0
0
Swiftprogramming~10 mins

Adding initializers via extension in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Adding initializers via extension
Define original struct/class
Create extension block
Add new initializer inside extension
Use new initializer to create instance
Instance created with extended initializer
You start with a struct or class, then add an extension where you define a new initializer, and finally use that initializer to create an instance.
Execution Sample
Swift
struct Point {
    var x: Int
    var y: Int
}

extension Point {
    init() {
        self.x = 0
        self.y = 0
    }
}

let origin = Point()
This code adds a new initializer to Point via extension that sets x and y to zero, then creates a Point instance using it.
Execution Table
StepActionEvaluationResult
1Define struct Point with x and yPoint has properties x:Int, y:IntPoint type ready
2Create extension for PointExtension block openedExtension ready to add members
3Add init() initializer in extensioninit() sets x=0, y=0New initializer added
4Call Point() to create originCalls new init()origin.x=0, origin.y=0
5Use origin instanceorigin has x=0, y=0Instance created successfully
💡 All steps complete, instance created using extension initializer
Variable Tracker
VariableStartAfter Step 4Final
origin.xundefined00
origin.yundefined00
Key Moments - 2 Insights
Why can we add an initializer in an extension instead of inside the original struct?
Extensions let us add new initializers without changing the original code, as shown in step 3 of the execution_table.
Does the new initializer replace the default initializer?
No, the new initializer is added alongside existing ones. The code calls the new init() in step 4, not replacing others.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what values does origin have after step 4?
Aorigin.x = 0, origin.y = 0
Borigin.x = undefined, origin.y = undefined
Corigin.x = 1, origin.y = 1
Dorigin.x = nil, origin.y = nil
💡 Hint
Check the 'Result' column in step 4 of execution_table
At which step is the new initializer added to Point?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column describing adding init() in execution_table
If we remove the extension, what happens when calling Point()?
AIt uses the initializer from extension anyway
BIt fails because no initializer is defined
CIt still works with default initializer
DIt creates an instance with x and y as nil
💡 Hint
Remember Swift structs have a default memberwise initializer unless overridden
Concept Snapshot
struct MyType { var a: Int }
extension MyType {
  init() { self.a = 0 }
}
Use extension to add initializers without changing original code.
Call new init() to create instances with custom defaults.
Full Transcript
We start by defining a struct Point with two properties x and y. Then, we create an extension to add a new initializer that sets both x and y to zero. When we call Point() using this new initializer, it creates an instance with x=0 and y=0. This shows how extensions let us add initializers without modifying the original struct. The execution table traces each step from defining the struct, adding the extension and initializer, to creating an instance. The variable tracker shows origin.x and origin.y changing from undefined to 0 after initialization. Common confusions include why we can add initializers in extensions and whether the new initializer replaces existing ones. The visual quiz tests understanding of values after initialization, when the initializer is added, and what happens if the extension is removed.