Junior — Middle
How can you determine if two objects of the same type refer to the same instance in memory?
sobes.tech AI
Answer from AI
In iOS (Swift), to check whether two objects of the same class refer to the same instance in memory, the identity operator === is used.
It compares references to objects, not their content. If a === b returns true, it means a and b point to the same object.
Example:
class Person {
var name: String
init(name: String) {
self.name = name
}
}
let person1 = Person(name: "Alice")
let person2 = person1
let person3 = Person(name: "Alice")
print(person1 === person2) // true, both variables refer to the same object
print(person1 === person3) // false, different objects with the same data
For structures (value types), such a check does not exist, as they are copied upon assignment.