0
0
Swiftprogramming~10 mins

Identity operators (=== and !==) in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Identity operators (=== and !==)
Create two objects
Compare references with ===
Same object
Compare references with !==
Different object
This flow shows how Swift checks if two variables point to the exact same object using === or !== operators.
Execution Sample
Swift
class Person {}
let a = Person()
let b = a
let c = Person()
print(a === b)
print(a !== c)
This code creates two Person objects and compares their references using identity operators.
Execution Table
StepActionEvaluationResult
1Create object aa is a new Person instancea points to Person#1
2Assign b = ab points to same instance as ab points to Person#1
3Create object cc is a new Person instancec points to Person#2
4Evaluate a === bAre a and b the same object?true
5Evaluate a !== cAre a and c different objects?true
💡 All comparisons done, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
anilPerson#1Person#1Person#1Person#1
bnilnilPerson#1Person#1Person#1
cnilnilnilPerson#2Person#2
Key Moments - 2 Insights
Why does 'a === b' return true even though they are two variables?
Because both 'a' and 'b' point to the exact same object instance (Person#1), as shown in execution_table step 4.
Why does 'a !== c' return true even though both are Person objects?
Because 'a' and 'c' point to different instances (Person#1 vs Person#2), so their references are not identical (execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the variable_tracker after Step 2. What object does variable 'b' point to?
APerson#1
BPerson#2
Cnil
Da new object
💡 Hint
Check the 'b' row in variable_tracker at 'After Step 2' column.
At which step does the program create a new Person object different from 'a'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table step 3 where 'c' is created.
If we changed 'let b = a' to 'let b = Person()', what would 'a === b' evaluate to?
Atrue
Bfalse
Cnil
Derror
💡 Hint
If 'b' points to a new object, 'a === b' is false because they are different instances.
Concept Snapshot
Identity operators in Swift:
- '===' checks if two variables point to the same object instance.
- '!==' checks if two variables point to different object instances.
- Used only with class instances (reference types).
- Returns a Bool: true or false.
- Does NOT compare object content, only identity.
Full Transcript
This visual execution trace shows how Swift's identity operators '===' and '!==' work. We create two Person objects and assign variables to them. Variable 'a' points to Person#1. Variable 'b' is assigned to 'a', so it points to the same object. Variable 'c' is a new Person instance, Person#2. When we check 'a === b', it returns true because both point to the same object. When we check 'a !== c', it returns true because they point to different objects. The variable tracker shows how each variable points to objects after each step. This helps beginners understand that identity operators compare references, not content.