Identity comparison checks if two variables point to the exact same object in memory. It helps us know if they are truly the same instance, not just equal in value.
Identity comparison (===) in Swift
object1 === object2
The === operator works only with class instances (reference types).
It returns true if both variables refer to the same object, otherwise false.
class Person {} let personA = Person() let personB = personA let personC = Person() print(personA === personB) // true print(personA === personC) // false
class Car {} let car1 = Car() let car2 = car1 let car3 = car2 print(car1 === car3) // true
class Book {} let book1 = Book() let book2 = Book() print(book1 === book2) // false
This program creates three Dog instances. dog1 and dog2 point to the same object, so identity comparison is true. dog3 is a different object even though the name is the same.
class Dog { var name: String init(name: String) { self.name = name } } let dog1 = Dog(name: "Buddy") let dog2 = dog1 let dog3 = Dog(name: "Buddy") print(dog1 === dog2) // true print(dog1 === dog3) // false
Identity comparison (===) is different from equality comparison (==), which checks if values are equal.
Only class instances can be compared with ===, not structs or enums.
Identity comparison checks if two variables point to the exact same object.
Use === with class instances to compare their identity.
It returns true if both refer to the same object, otherwise false.
Practice
=== operator check in Swift?Solution
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.Step 2: Differentiate from value equality
Unlike==, which checks if values are equal,===checks identity, meaning the same instance.Final Answer:
If two variables refer to the exact same object instance -> Option AQuick Check:
Identity comparison = If two variables refer to the exact same object instance [OK]
- Confusing === with == for value equality
- Thinking === checks type equality
- Assuming === works with structs or enums
a and b are identical in Swift?Solution
Step 1: Recall identity operator syntax
In Swift, the identity operator to check if two references point to the same object is===.Step 2: Identify correct usage
The correct syntax isif a === b { }. The other options are either value equality, assignment, or invalid syntax.Final Answer:
if a === b { } -> Option AQuick Check:
Identity check syntax = if a === b { } [OK]
- Using == instead of === for identity
- Using single = which is assignment
- Typing invalid operators like ===
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 object references
Variablep2is assigned top1, so both point to the same object.p3is a new instance.Step 2: Evaluate identity comparisons
p1 === p2istruebecause they are the same instance.p1 === p3isfalsebecause they are different instances.Final Answer:
true\nfalse -> Option DQuick Check:
Same instance = true, different instance = false [OK]
- Assuming different variables always mean false
- Confusing === with == and expecting value equality
- Ignoring that p2 points to p1's instance
class Car {}
let car1 = Car()
let car2 = Car()
if car1 == car2 {
print("Same car")
}Solution
Step 1: Understand == operator requirements
By default, class instances cannot be compared with==unless the class conforms toEquatable.Step 2: Identify the error cause
The code tries to use==withoutCarconforming toEquatable, causing a compile error.Final Answer:
Cannot use == to compare class instances without Equatable conformance -> Option CQuick Check:
== needs Equatable for classes [OK]
- Thinking === causes error here
- Assuming == works automatically for classes
- Ignoring Equatable protocol requirement
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 identitySolution
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 cache.values.contains(where: { $0 === newObj }) { print("Found") } correctly usescache.values.contains(where: { $0 === newObj }). if cache.values.contains(newObj) { print("Found") } usescontainswithout 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.Final Answer:
if cache.values.contains(where: { $0 === newObj }) { print("Found") } -> Option BQuick Check:
Use contains(where:) with === for identity [OK]
- Using contains without where and identity check
- Checking keys instead of values
- Using == without Equatable conformance
Ready to prove it?
Pass the test and this pattern moves unknown
