Sobes.tech
Junior — Middle

Kuidas määratleda kahe objekti võrdus nende viidete võrdluse kontekstis?

sobes.tech AI

Vastus AI-lt

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

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 to the same object
print(personA === personC) // false, different objects

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