Avatar ·

Difference between == and === in Swift

Hello everyone! Please explain the fundamental difference between the == and === operators in Swift? I understand that both are used for comparison, but when I try to compare two class instances, the result differs from what I expected.

Here's an example code that confuses me:

class Person {\n    var name: String\n    init(name: String) {\n        self.name = name\n    }\n}\n\nlet person1 = Person(name: "Alice")\nlet person2 = Person(name: "Alice")\n\nprint(person1 == person2) // Compilation error: Binary operator '==' cannot be applied to two 'Person' operands\nprint(person1 === person2) // false\n\nlet person3 = person1\nprint(person1 === person3) // true

I expected person1 == person2 to return true, but