0
0
Swiftprogramming~10 mins

Convenience initializers in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Convenience initializers
Call Convenience Init
Perform Custom Setup
Call Designated Init
Complete Initialization
Object Ready
A convenience initializer calls another initializer in the same class to reuse setup code, then completes object creation.
Execution Sample
Swift
class Car {
  var color: String
  var model: String

  init(model: String, color: String) {
    self.model = model
    self.color = color
  }

  convenience init(model: String) {
    self.init(model: model, color: "Black")
  }
}

let car = Car(model: "Sedan")
Creates a Car object using a convenience initializer that sets a default color.
Execution Table
StepActionEvaluationResult
1Call convenience init with model="Sedan"convenience init(model: String)Starts convenience initializer
2Inside convenience init, call designated init with model="Sedan", color="Black"self.init(model: model, color: "Black")Calls designated initializer
3Designated init sets self.model = "Sedan"self.model = modelmodel property set to "Sedan"
4Designated init sets self.color = "Black"self.color = colorcolor property set to "Black"
5Designated init completesinit completeObject properties initialized
6Convenience init completesconvenience init completeObject ready for use
💡 Initialization ends after convenience initializer calls designated initializer and completes.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
modelnil"Sedan""Sedan""Sedan"
colornilnil"Black""Black"
Key Moments - 2 Insights
Why does the convenience initializer call the designated initializer?
Because in Swift, convenience initializers must call a designated initializer to ensure all properties are properly initialized, as shown in step 2 of the execution_table.
Can a convenience initializer set properties directly without calling another initializer?
No, convenience initializers cannot set all properties directly; they must call a designated initializer first, which sets the properties, as seen in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'color' after step 4?
A"Sedan"
B"Black"
Cnil
D"Red"
💡 Hint
Check the variable_tracker row for 'color' after step 4.
At which step does the designated initializer complete its work?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Action' column in the execution_table for when 'init complete' happens.
If the convenience initializer did not call the designated initializer, what would happen?
AThe code would not compile because all properties must be initialized.
BThe object would be fully initialized anyway.
CThe convenience initializer would set default values automatically.
DThe designated initializer would be called automatically.
💡 Hint
Recall Swift's rule that convenience initializers must call a designated initializer (see key_moments).
Concept Snapshot
Convenience initializers in Swift:
- Use 'convenience init' keyword
- Must call another initializer in the same class
- Help provide default or simpler ways to create objects
- Cannot fully initialize properties themselves
- Always call a designated initializer to complete setup
Full Transcript
This example shows how a convenience initializer in Swift works. When we create a Car object with only a model, the convenience initializer runs first. It calls the designated initializer with a default color. The designated initializer sets the model and color properties. Then initialization finishes, and the object is ready. Convenience initializers help avoid repeating code by reusing designated initializers. They must call a designated initializer to ensure all properties are set properly. This flow ensures safe and complete object creation.