Swift Programming For Complete Beginners Part 2:

Constants and Variables

Posted 5/8/2018.

You can find Part 1 here.

You've set up Xcode and wrote your first program, but you probably don't yet understand how the code you wrote works. That's OK, we'll get there! In this tutorial we'll learn about two of the basic building blocks of computer programs, constants and variables.

The concepts of "constant" and "variable" will be familiar from math and science, and have the same meaning here. We'll define these terms specifically in the computer science context and write some examples in Swift.

Constants

A constant is a value that cannot be changed. Before we discuss why we would want to declare a value in a program that cannot be changed, let's see some examples.

First, create a new project in Xcode called "HelloSwift". You can follow the same steps as in Part 1. Once again you should have a "main.swift" file. Go ahead and delete all of the code Xcode generated and you should be left with a blank file.

Now let's declare some constants. Constants in Swift are declared using the let keyword. Type the following lines in "main.swift":

let pi = 3.14159
let email = "test@example.com"
let four = 2 + 2

Notice that the last example is an expression. A constant can be the result of complicated logic, but once that result is declared as a constant value, it cannot be changed.

Now try to change the value of pi. Write the following line at the bottom:

pi = 3.15

Xcode will immediately warn you, Cannot assign to value: 'pi' is a 'let' constant. You literally cannot run the program if you try to change a constant value!

And in this example it makes sense. In math, the value of "pi" is universally understood as a constant. But what about the second example, let email = "test@example.com". Why would we ever want to prevent ourselves from being able to change this value?

The answer is that this self-imposed constraint makes our code safer because we can depend on the value of a constant never changing. Computer programs can get quite complicated. If we know a value should not change in our program, even if it's not a universally accepted value like "pi", enforcing that constraint helps prevent unexpected behavior.

In this case, we may have an app where the user enters an email address. For the purposes of the app, once entered the email address should not change. By declaring it as a constant, we are ensuring that no part of the program can change its value unexpectedly.

In programming, we use the word "immutable" to refer to this quality of a constant. Making our code immutable when we can helps us write complex logic with greater confidence, as Xcode will prevent us from changing values that we know should remain constant.

As a general rule, you should always declare values as constants unless they need to be variables.

Variables

Variables are values that can be changed. Unsurprisingly, we use the word "mutable" to refer to this quality of variables.

Variables in Swift are declared with the var keyword. Type the following lines at the bottom of the file:

var printedLines = 0
print(pi)
print(email)
print(four)
printedLines = 3
print(printedLines)
printedLines = 4

Here we're keeping track of how many times we have printed a line of text to the Console. The value of printedLines is declared as 0. Then we print three lines and change the value to 3.

Now that you've learned about constants and variables, try declaring a few of each and printing them to the Console. For each variable, print the value to the Console right after declaring it, then change the value, and finally print it again.

Review Questions

You should now be able to answer these questions:


In this tutorial you learned how to declare constants and variables in Swift. You may have noticed that we delcared different types of values. One was a number with a decimal (pi), one was text (email), and the others were integers (four, printedLines). We'll learn about Types next!