Identity comparison (===) in Swift
📖 Scenario: Imagine you have two boxes that can hold toys. Sometimes, you want to check if both boxes are actually the same box, not just two boxes with the same toys.
🎯 Goal: You will create two class instances representing boxes and use identity comparison === to check if they are the exact same box.
📋 What You'll Learn
Create a class called
Box with a property toy of type StringCreate two variables
box1 and box2 as instances of BoxCreate a third variable
box3 that points to the same instance as box1Use identity comparison
=== to check if box1 and box2 are the same instanceUse identity comparison
=== to check if box1 and box3 are the same instancePrint the results of both comparisons
💡 Why This Matters
🌍 Real World
Identity comparison helps when you want to know if two variables refer to the exact same object, like checking if two references point to the same user profile in an app.
💼 Career
Understanding identity comparison is important for Swift developers working with classes, memory management, and reference types to avoid bugs and write efficient code.
Progress0 / 4 steps