Identity operators (=== and !==) in Swift - Time & Space Complexity
Let's see how checking if two objects are the same affects how long a program takes to run.
We want to know how the time to compare objects grows as we do more comparisons.
Analyze the time complexity of the following code snippet.
class Person {}
let people = [Person(), Person(), Person(), Person(), Person()]
let target = people[2]
for person in people {
if person === target {
print("Found the target person")
}
}
This code checks each person in a list to see if it is the exact same object as the target.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop goes through each person in the array.
- How many times: Once for every person in the list.
As the list gets bigger, the number of checks grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 identity checks |
| 100 | 100 identity checks |
| 1000 | 1000 identity checks |
Pattern observation: The number of checks grows directly with the number of people.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the list gets longer.
[X] Wrong: "Checking if two objects are the same is instant and does not depend on list size."
[OK] Correct: While each check is quick, doing many checks adds up as the list grows.
Understanding how identity checks scale helps you explain how your code handles object comparisons efficiently.
"What if we used a dictionary to find the target instead of a loop? How would the time complexity change?"
Practice
=== check in Swift?Solution
Step 1: Understand the purpose of
The======operator checks if two references point to the same object in memory.Step 2: Differentiate from value equality
Unlike==, which checks value equality,===checks identity (same instance).Final Answer:
If two variables refer to the exact same class instance -> Option CQuick Check:
===checks identity [OK]
- Confusing === with == for value equality
- Using === with structs or value types
- Thinking === checks type equality
a and b are NOT the same object in Swift?Solution
Step 1: Identify the correct identity operator for inequality
The operator to check if two references are NOT the same object is!==.Step 2: Verify syntax correctness
Options C and D have invalid operators. if a != b uses!=which checks value inequality, not identity.Final Answer:
if a !== b -> Option DQuick Check:
Use !== for identity inequality [OK]
- Using != instead of !== for identity check
- Typing invalid operators like !=== or !====
- Confusing identity operators with value operators
class Person {}
let p1 = Person()
let p2 = p1
let p3 = Person()
print(p1 === p2)
print(p1 === p3)What will be the output?
Solution
Step 1: Analyze the references
p2is assigned top1, so both point to the same object.p3is a new instance.Step 2: Evaluate identity comparisons
p1 === p2is true because they are the same instance.p1 === p3is false because they are different instances.Final Answer:
true\nfalse -> Option AQuick Check:
Same instance = true, different instance = false [OK]
- Assuming p1 and p3 are same because same class
- Confusing value equality with identity
- Forgetting p2 points to p1's instance
struct Point {
var x: Int
var y: Int
}
let p1 = Point(x: 1, y: 2)
let p2 = Point(x: 1, y: 2)
if p1 === p2 {
print("Same point")
}Solution
Step 1: Identify the types used
Pointis a struct, which is a value type in Swift.Step 2: Understand identity operator restrictions
Identity operators===and!==only work with class instances (reference types), not value types like structs.Final Answer:
Cannot use === with structs because they are value types -> Option BQuick Check:
=== only for class instances [OK]
- Trying to use === with value types
- Assuming structs have identity
- Ignoring compiler error message
Car and an array of Car instances. You want to find if a specific Car instance myCar is already in the array by identity (same object), not by value. Which Swift code correctly checks this?Solution
Step 1: Understand identity check in collections
To check if an instance exists by identity, use===inside a closure withcontains(where:).Step 2: Analyze options
if cars.contains(where: { $0 === myCar }) { print("Found") } uses===correctly. Options B and C use==orof:which check value equality or require Equatable conformance. if cars.filter({ $0 == myCar }).count > 0 { print("Found") } uses==which is value equality.Final Answer:
if cars.contains(where: { $0 === myCar }) { print("Found") } -> Option AQuick Check:
Use contains(where:) with === for identity [OK]
- Using contains(myCar) which checks value equality
- Assuming == checks identity
- Not using closure for identity check
Ready to prove it?
Pass the test and this pattern moves unknown
