Before you start creating spectacular iOS apps in UiKit or SwiftUI it’s important to grasp the basic fundamentals of the Swift language.
With these fundamentals you will then have the foundation which you can creatively make whatever app your heart desires. Whether it be a Watch, iPhone, iPad or even Apple TV app.
Today we are going to explore one of the fundamentals of the Swift language which is the difference between the assignment operator that is represented with an = equal sign and the equality operator which is represented with an == sign.
Assignment Operator / =
First let’s look at an assignment operator and what it does in Swift.
An easy way to understand what an assignment operator means is to simply think of it as assigning a value to something.
For instance, let us dissect the example below:
var name = “Bob”
In this example we have a variable with the name of “name” then we have an = assignment operator sign and then a string of “Bob”.
This line of code is stating that the variable of “name” is being assigned to the string “Bob”.
Therefore, wherever you see “Bob” you also know it is referencing the variable “name” because it was assigned to it using the assignment operator.
Equality Operator / ==
Next, we will explore the equality operator and how it functions in Swift.
A simple way to understand the equality operator is to think of it as stating if different values are equal.
To get a better understanding we can examine the example below:
5 == 5 True
In the first example we have an integer of 5, then an == equality operator sign, followed by another integer of 5 and then finally a Boolean of True. (Don’t worry we will cover what are Integers and Booleans in the near future.)
This line of code of is stating that the integer of 5 is equal to the integer of 5 and that it is a True statement.
Remember the equality operator all it does is see if different values are equal which it does in the line of code above.
We know that 5 is equal to 5 of course which is why the Boolean statement of True is used.
Summary
Remember when you see = and == operator signs in Swift they are not the same thing.
An = assignment operator sign assigns value to something while a = = equality operator is used to verify if different values are equal to one another or not.