Sobes.tech
Junior — Middle

How do you determine the equality of two objects in the context of comparing their references?

sobes.tech AI

Answer from AI

Equality of two objects by reference means that both variables point to the same instance in memory. In Swift, the identity operator === is used to compare references.

Example:

class Person {
    var name: String
    init(name: String) {
        self.name = name
    }
}

let personA = Person(name: "Alice")
let personB = personA
let personC = Person(name: "Alice")

print(personA === personB) // true, both references point to the same object
print(personA === personC) // false, different objects

Thus, to check if two variables point to the same object, use ===, not ==, which usually checks for content equality.