Equality vs Identity
Now that you understand the difference between value and reference types, you are ready to learn about equality and identity. Equality refers to two instances having the same values for their observable characteristics, such as two instances of the String type that have the same text. Identity, on the other hand, refers to whether two variables or constants point to the same instance in memory. Take a look at this sample code.
let x = 1 let y = 1 x == y // True
Two constants, x and y, are created.
They are both of type Int and hold on to the same value, 1.
Not surprisingly, the equality check, done via ==, evaluates to true.
This makes sense because x and y hold on to exactly the same value.
This is exactly ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access