Equality in Swift

We’re all equal. Right?

Equals helps you to compare two (or more) values = and is no problem to most people coding in Swift. However, there is more to equality than just this.

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

• Be able to produce a “Hello, World!” iOS application (guide HERE)
• Some knowledge of value and reference types would be advantageous (guide HERE)
• For later in the discussion knowledge of the equatable protocol is advantageous, although not required (guide HERE)

Terminology

Assignment: Giving (setting) a variable a value

Equality: Numerically this is the relation between two operands (e.g 3=3 or 5<6; both of which would resolve to be true)

Identity: The relationship between two operands, that is they are identical or not and are the same instance.

Instance: An example or single occurrence of something

Operands: The value on which an operator is performed

Relational operator: An operator that tests the relation between two operands

Variable: A named value, that potentially can be changed as the program runs

The relationship between =, == and ===

Oh my! Those who have created programming languages have made this quite tricky indeed.

Allows us to create a calculation

`var sum = 3print (sum) // 3`

in this case we have created a `variable` called sum and placed 3 into that `variable`.

In programming we often call this = `assignment`

Equality allows you to test if things are equal to each other, that is `equality` and tests if two things are (for want of a better word) equal.

Let us see some examples using `Integer` types:

`3 == 3 // true10 == 10 // true2 == 7 // false`

We will see slightly more complex examples later in this example.

We can also check that things are not equals by using `!` to negate our operator

`1 != 2 // true7 != 7 // false2 != 7 // true`

This checks to see if two operands point to the same `instance`, that is, check that they are identical (no not, as the case maybe).

Because we are talking about instances comparing simple `Integers` will produce an error in Swift

`let variableOne = 3let variableTwo = 3variableOne == variableTwo // truevariableOne === variableTwo // error`

The error is:

Binary operator ‘===’ cannot be applied to two ‘Int’ operands

So at least this is clear; we can however make this comparison of `Classes`. I have had to set up an equality static function

`class Person {    var name: String    init (name: String) {         self.name = name    }    static func == (lhs: Person, rhs: Person) -> Bool {        return las.name == rhs.name    }}`

We then set up `Instances` of these types

`let dave = Person(name: "Dave")let anne = Person(name: "Anne")`

Now their names are different, so as you might have guessed:

`anne === dave // this is false, different instances`

and as before

`anne == dave // this is false, as they are different`

What if there are two Dave’s? Dave P and Dave?

`let dave = Person(name: "Dave")let daveP = Person(name: "Dave")dave === daveP // this is false, different instancesdave == daveP // this is true, as the names are the same`

Now as we are using reference so when we make a copy of the class, we are passing the reference around. Therefore the following makes sense (and, of course, is true):

`let dave = Person(name: "Dave")let daveF = davedave === daveF //true`

Conclusion:

That seemed reasonably tricky. Anyone who thought that equals just means equals will now know things can be a little but more tricky with =, == and === all in play.

Now pick wisely…!