Bird
Raised Fist0
Swiftprogramming~10 mins

Identity comparison (===) in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Identity comparison (===)
Create Object A
↓
Create Object B
↓
Compare A === B?
Yes→Same Object
No↓
Different Objects
This flow shows creating two objects and then checking if they are exactly the same object in memory using ===.
Execution Sample
Swift
class Person {}
let p1 = Person()
let p2 = p1
let p3 = Person()
print(p1 === p2)
print(p1 === p3)
This code creates two Person objects and compares their identity using ===.
Execution Table
StepActionEvaluationResult
1Create p1 as new Person()p1 points to Object1p1 -> Object1
2Assign p2 = p1p2 points to same Object1 as p1p2 -> Object1
3Create p3 as new Person()p3 points to Object2p3 -> Object2
4Evaluate p1 === p2Are p1 and p2 same object?true
5Evaluate p1 === p3Are p1 and p3 same object?false
💡 All comparisons done, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
p1nilObject1Object1Object1Object1
p2nilnilObject1Object1Object1
p3nilnilnilObject2Object2
Key Moments - 2 Insights
Why does p1 === p2 return true even though we didn't create a new object for p2?
Because p2 was assigned to p1, both variables point to the exact same object in memory, so identity comparison returns true (see execution_table step 4).
Why does p1 === p3 return false even though p3 is also a Person?
Because p3 is a new object created separately, so p1 and p3 point to different objects in memory, making identity comparison false (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does p2 point to after step 2?
Anil
BA new Person object different from p1
CThe same object as p1
DAn error
💡 Hint
Check variable_tracker row for p2 after step 2.
At which step does p3 get assigned a new object?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at execution_table step 3 and variable_tracker for p3.
If we changed p2 = p1 to p2 = Person(), what would p1 === p2 return?
Atrue
Bfalse
Cnil
DCompile error
💡 Hint
New object means different identity, see explanation in key_moments.
Concept Snapshot
Identity comparison (===) checks if two variables point to the exact same object in memory.
Use === only with class instances (reference types).
If two variables refer to the same object, === returns true.
If they refer to different objects, even if identical in content, === returns false.
Useful to check object identity, not equality of content.
Full Transcript
This visual execution shows how identity comparison works in Swift using the === operator. We create two Person objects and assign variables p1, p2, and p3. p2 is assigned to p1, so both point to the same object. p3 is a new object. When we compare p1 === p2, it returns true because they are the same object. When we compare p1 === p3, it returns false because they are different objects. The variable tracker shows how variables point to objects step by step. Key moments clarify why assignment copies references, not objects, and why new objects have different identities. The quiz tests understanding of these points. The snapshot summarizes the concept simply.

Practice

(1/5)
1. What does the === operator check in Swift?
easy
A. If two variables refer to the exact same object instance
B. If two variables have the same value
C. If two variables are of the same type
D. If two variables are both nil

Solution

  1. Step 1: Understand the purpose of ===

    The === operator in Swift is used to check if two references point to the exact same object in memory.
  2. Step 2: Differentiate from value equality

    Unlike ==, which checks if values are equal, === checks identity, meaning the same instance.
  3. Final Answer:

    If two variables refer to the exact same object instance -> Option A
  4. Quick Check:

    Identity comparison = If two variables refer to the exact same object instance [OK]
Hint: Remember: === means same object, not just equal value [OK]
Common Mistakes:
  • Confusing === with == for value equality
  • Thinking === checks type equality
  • Assuming === works with structs or enums
2. Which of the following is the correct syntax to check if two class instances a and b are identical in Swift?
easy
A. if a === b { }
B. if a == b { }
C. if a = b { }
D. if a is b { }

Solution

  1. Step 1: Recall identity operator syntax

    In Swift, the identity operator to check if two references point to the same object is ===.
  2. Step 2: Identify correct usage

    The correct syntax is if a === b { }. The other options are either value equality, assignment, or invalid syntax.
  3. Final Answer:

    if a === b { } -> Option A
  4. Quick Check:

    Identity check syntax = if a === b { } [OK]
Hint: Use three equals signs (===) for identity check [OK]
Common Mistakes:
  • Using == instead of === for identity
  • Using single = which is assignment
  • Typing invalid operators like ===
3. Consider the following Swift code:
class Person {}

let p1 = Person()
let p2 = p1
let p3 = Person()

print(p1 === p2)
print(p1 === p3)

What will be the output?
medium
A. false\nfalse
B. false\ntrue
C. true\ntrue
D. true\nfalse

Solution

  1. Step 1: Analyze object references

    Variable p2 is assigned to p1, so both point to the same object. p3 is a new instance.
  2. Step 2: Evaluate identity comparisons

    p1 === p2 is true because they are the same instance. p1 === p3 is false because they are different instances.
  3. Final Answer:

    true\nfalse -> Option D
  4. Quick Check:

    Same instance = true, different instance = false [OK]
Hint: Same variable means true; new instance means false [OK]
Common Mistakes:
  • Assuming different variables always mean false
  • Confusing === with == and expecting value equality
  • Ignoring that p2 points to p1's instance
4. The following Swift code gives a compile error. What is the problem?
class Car {}

let car1 = Car()
let car2 = Car()

if car1 == car2 {
    print("Same car")
}
medium
A. Cannot create instances of class Car
B. Missing parentheses in if statement
C. Cannot use == to compare class instances without Equatable conformance
D. Using === instead of ==

Solution

  1. Step 1: Understand == operator requirements

    By default, class instances cannot be compared with == unless the class conforms to Equatable.
  2. Step 2: Identify the error cause

    The code tries to use == without Car conforming to Equatable, causing a compile error.
  3. Final Answer:

    Cannot use == to compare class instances without Equatable conformance -> Option C
  4. Quick Check:

    == needs Equatable for classes [OK]
Hint: Use === for identity or add Equatable for == [OK]
Common Mistakes:
  • Thinking === causes error here
  • Assuming == works automatically for classes
  • Ignoring Equatable protocol requirement
5. You have a cache dictionary storing class instances by keys. You want to check if a newly created instance newObj is already stored in the cache by identity. Which Swift code correctly checks this?
class Data {}

var cache: [String: Data] = [:]
let newObj = Data()

// Check if newObj is already in cache by identity
hard
A. if cache.values.contains(newObj) { print("Found") }
B. if cache.values.contains(where: { $0 === newObj }) { print("Found") }
C. if cache.keys.contains(where: { $0 === newObj }) { print("Found") }
D. if cache.values.contains(where: { $0 == newObj }) { print("Found") }

Solution

  1. Step 1: Understand identity check in collections

    To check if an instance exists by identity, use === inside a closure with contains(where:).
  2. Step 2: Analyze options

    if cache.values.contains(where: { $0 === newObj }) { print("Found") } correctly uses cache.values.contains(where: { $0 === newObj }). if cache.values.contains(newObj) { print("Found") } uses contains without identity check, which requires Equatable. if cache.keys.contains(where: { $0 === newObj }) { print("Found") } incorrectly checks keys (strings) with identity. if cache.values.contains(where: { $0 == newObj }) { print("Found") } uses == which needs Equatable.
  3. Final Answer:

    if cache.values.contains(where: { $0 === newObj }) { print("Found") } -> Option B
  4. Quick Check:

    Use contains(where:) with === for identity [OK]
Hint: Use contains(where:) and === to check identity in collections [OK]
Common Mistakes:
  • Using contains without where and identity check
  • Checking keys instead of values
  • Using == without Equatable conformance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard