0
0
Swiftprogramming~5 mins

Identity operators (=== and !==) in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the identity operator === check in Swift?
The <code>===</code> operator checks if two references point to the exact same instance of a class in memory.
Click to reveal answer
beginner
What is the difference between == and === in Swift?
== checks if two values are equal in content, while === checks if two references point to the same object instance.
Click to reveal answer
beginner
What does the identity operator !== do?
The !== operator checks if two references do NOT point to the same instance of a class.
Click to reveal answer
intermediate
Can === and !== be used with value types like structs or enums?
No, <code>===</code> and <code>!==</code> only work with class instances because they compare object identity, not value equality.
Click to reveal answer
intermediate
Why would you use identity operators instead of equality operators?
Use identity operators to check if two variables refer to the exact same object, which is important when you want to know if they share the same instance, not just equal content.
Click to reveal answer
What does a === b check in Swift?
AIf a and b refer to the same class instance
BIf a and b have the same value
CIf a and b are both nil
DIf a and b are different types
Which operator would you use to check if two class instances are NOT the same object?
A==
B!=
C!==
D===
Can === be used to compare two structs?
AYes, always
BNo, only class instances
CYes, but only if they conform to Equatable
DNo, only enums
What will obj1 === obj2 return if both variables point to the same object?
AIt causes a compile error
Bfalse
Cnil
Dtrue
Which operator checks if two variables have equal content, not identity?
A==
B===
C!==
D!=
Explain the difference between the identity operators === and !== in Swift.
Think about whether two variables refer to the exact same object or not.
You got /3 concepts.
    When should you use identity operators instead of equality operators in Swift?
    Consider if you care about the actual object or just its data.
    You got /3 concepts.