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?✗ Incorrect
=== checks if two references point to the same object instance.Which operator would you use to check if two class instances are NOT the same object?
✗ Incorrect
!== checks if two references do NOT point to the same instance.Can
=== be used to compare two structs?✗ Incorrect
Identity operators only work with class instances, not value types like structs.
What will
obj1 === obj2 return if both variables point to the same object?✗ Incorrect
If both references point to the same instance,
=== returns true.Which operator checks if two variables have equal content, not identity?
✗ Incorrect
== checks for value equality, not identity.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.