Megawatt Apps

Swift - Variables and Constants

Oct 3, 2016

Most programming languages let you create variables. A variable is a callable piece of code that stores data. For example, instead of having to type out "The United States of America" every time you want to use that sentence, you can store that sentence in a variable called "US". Now every time you want to say "The United States of America" you can instead just type in US, and it would be the equivalent of typing in that entire sentence.

The difference between swift and most programming languages is that swift allows you to create constants as well as variables. What are constants? What are variables? We will get to that, but first let's go over variables.

To create variables in swift, you use the following syntax:

var nameOfVariable: String = "Piece of Data"

Let's break this down starting from the left. Var is our keyword here that lets swift know we want to create a variable. The nameOfVariable can be anything we want, we simply have to assign it a name based on what kind of data it is going to store. If it's an address for example, we can name the variable address. It wouldn't make sense to name it address if it were storing your age for example.

The ": String" is completely optional. It's only there so we can explicitly tell swift that we want to create a variable of data type String. String being replaceable with any of the other data types. We aren't going to go over the different data types here, if you want to learn more visit our Swift Tutorial Data Types. Because this part is optional, you can actually still write the above code as:

var nameOfVariable = "Piece of Data"

And still have it working perfectly.

After the equal sign we finally have our data. Because the data is enclosed inside of strings, it shows that this data is of string type.

We can access this variable by simply calling in the name of the variable, in this case "nameOfVariable"

Now we'll move on to a unique feature of swift, or at least I haven't encountered this in other program languages. We're going to go over constants and how they differ from variables. Constants and variables are similar in that they both store data, but they store data differently.

To quickly summarize the difference between the two, constants are known as immutable, or cannot be changed. Variables are mutable, which means they can be changed. How you set a constant is by using "Let" instead of Var, otherwise they work exactly the same.

To create a constant of the above variable, all we have to do is change the keyword from var to let:

let nameOfVariable: String = "Piece of Data"

let nameOfVariable = "Piece of Data"

Again data typing is optional.

When do you use constants instead of var? You use constants for when a value will never change. For example you can create a constant for the speed of light, the value of pi, the date of your birth, and so on. For variables, you want to create variables when the value can change. For example you'd want to use a variable for your age, your weight, your velocity, and so on.

That's the main difference between constants and variables. I hope you learned how to create constants and how to create variables. If you have any questions, leave them down in the comments.