0
0
Swiftprogramming~10 mins

Classes are reference types in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Classes are reference types
Create instance A
Assign instance A to variable a
Assign variable a to variable b
Modify property via b
Check property via a
Both a and b show same updated value
This flow shows how assigning a class instance to another variable copies the reference, so both variables point to the same object.
Execution Sample
Swift
class Person {
    var name: String
    init(name: String) { self.name = name }
}

let a = Person(name: "Alice")
let b = a
b.name = "Bob"
print(a.name)
This code creates a Person instance, assigns it to two variables, changes the name via one variable, and prints the name via the other to show shared reference.
Execution Table
StepActionVariable aVariable bOutput
1Create Person instance with name 'Alice'Person(name: "Alice")nil
2Assign instance to variable aPerson(name: "Alice")nil
3Assign variable a to variable b (copy reference)Person(name: "Alice")Person(name: "Alice")
4Change b.name to 'Bob'Person(name: "Bob")Person(name: "Bob")
5Print a.namePerson(name: "Bob")Person(name: "Bob")Bob
💡 Execution ends after printing 'Bob', showing both variables reference the same object.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
anilPerson(name: "Alice")Person(name: "Alice")Person(name: "Alice")Person(name: "Bob")Person(name: "Bob")
bnilnilnilPerson(name: "Alice")Person(name: "Bob")Person(name: "Bob")
Key Moments - 2 Insights
Why does changing b.name also change a.name?
Because both a and b point to the same Person instance (see execution_table step 4), changing the property via b affects the same object a refers to.
Does assigning a to b create a new Person object?
No, assigning a to b copies the reference, not the object itself (see execution_table step 3). Both variables share the same instance.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the name property of the object referenced by a?
Anil
B"Alice"
C"Bob"
DUndefined
💡 Hint
Check the 'Variable a' column at step 4 in the execution_table.
At which step do variables a and b first reference the same Person instance?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at when variable b gets assigned in the execution_table.
If we changed b.name to "Charlie" instead of "Bob", what would a.name be at the end?
A"Alice"
B"Charlie"
C"Bob"
Dnil
💡 Hint
Since a and b reference the same object, changing b.name changes a.name too (see variable_tracker).
Concept Snapshot
Classes are reference types in Swift.
Assigning a class instance to a new variable copies the reference, not the object.
Changing properties via one variable affects the same shared object.
Use 'class' keyword to define reference types.
This differs from structs which are value types.
Full Transcript
This example shows that classes in Swift are reference types. When you create an instance of a class and assign it to a variable, that variable holds a reference to the object. If you assign that variable to another variable, both variables point to the same object. Changing a property through one variable changes it for the other because they share the same instance. This is different from value types like structs, where assignment copies the data. The execution table traces each step: creating the instance, assigning variables, modifying properties, and printing the result. The variable tracker shows how both variables reference the same object throughout. Understanding this helps avoid confusion when working with classes.