What does the identity operator === check in Swift?
It checks if two references point to the exact same instance of a class in memory.
Click to reveal answer
beginner
How is === different from == in Swift?
=== checks if two variables refer to the same object instance, while == checks if two values are equal in content.
Click to reveal answer
intermediate
Can === be used with structs or enums in Swift?
No, <code>===</code> only works with class instances because structs and enums are value types, not reference types.
Click to reveal answer
beginner
Given two variables <code>a</code> and <code>b</code> of a class type, what does <code>a === b</code> return if they point to the same object?
It returns true because both variables reference the same instance in memory.
Click to reveal answer
intermediate
Why is identity comparison useful in Swift programming?
It helps determine if two variables refer to the same object, which is important when managing shared resources or checking object uniqueness.
Click to reveal answer
What does the === operator check in Swift?
AIf two variables refer to the same class instance
BIf two values are equal in content
CIf two structs have the same properties
DIf two enums have the same case
✗ Incorrect
=== checks if two references point to the exact same object instance.
Can you use === to compare two structs in Swift?
AYes, always
BOnly if they are optional
COnly if they conform to Equatable
DNo, because structs are value types
✗ Incorrect
Structs are value types, so === which checks identity, cannot be used.
What will a === b return if a and b are different instances with the same content?
Afalse
Btrue
CIt causes a compile error
DDepends on the class
✗ Incorrect
=== returns false because the instances are different, even if content matches.
Which operator should you use to check if two class instances have equal content?
A===
B!==
C==
D!=
✗ Incorrect
== checks for equality of content, not identity.
Why might you want to use identity comparison in your Swift code?
ATo compare two numbers
BTo check if two variables point to the same object
CTo sort an array
DTo convert a class to a struct
✗ Incorrect
Identity comparison tells you if two references point to the same object instance.
Explain the difference between === and == in Swift.
Think about what each operator compares: the object itself or its content.
You got /4 concepts.
When should you use identity comparison (===) in your Swift programs?
Consider situations where knowing if two things are exactly the same object matters.
You got /3 concepts.
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
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 A
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
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 is if a === b { }. The other options are either value equality, assignment, or invalid syntax.
Final Answer:
if a === b { } -> Option A
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
Step 1: Analyze object references
Variable p2 is assigned to p1, so both point to the same object. p3 is a new instance.
Step 2: Evaluate identity comparisons
p1 === p2 is true because they are the same instance. p1 === p3 is false because they are different instances.
Final Answer:
true\nfalse -> Option D
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
Step 1: Understand == operator requirements
By default, class instances cannot be compared with == unless the class conforms to Equatable.
Step 2: Identify the error cause
The code tries to use == without Car conforming to Equatable, causing a compile error.
Final Answer:
Cannot use == to compare class instances without Equatable conformance -> Option C
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
Step 1: Understand identity check in collections
To check if an instance exists by identity, use === inside a closure with contains(where:).
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.
Final Answer:
if cache.values.contains(where: { $0 === newObj }) { print("Found") } -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.