Skip to content
Theory · Variables and types · lesson 1 of 7

Constants and variables

What you'll learn

What you'll learn:

  • declare constants with let and variables with var, and choose between them;
  • name Swift's basic types and state a type explicitly;
  • turn a value of one type into another with Int(…), Double(…) and String(…);
  • put values and expressions into a string with \( ).
An open box var getting a new tile, and a locked box let with a tile 42 inside

Picture a shelf of boxes. Each box has a label with a name, and inside it lies a value: a number, a word, or a "yes" or "no". The program finds the right box by its label and looks inside.

There are two kinds of boxes. One you can open, take the old value out of and put a new one in. The other is sealed the moment a value goes in, and it never opens again. In Swift the open box is called a variable, and the sealed one a constant.

Why does a program need boxes? It remembers values so it can read them on later lines: a player's name, a score, a number of tries. The player's name stays the same for the whole game, while the score grows. Here is how you write that in Swift:

SWIFT
let player = "Anna"
var score = 0
print(player, score)  // Anna 0
score = 50
print(player, score)  // Anna 50

Let's walk through the program line by line:

  1. let player = "Anna" makes a sealed box player and puts the text "Anna" in it.
  2. var score = 0 makes an open box score holding the number 0.
  3. print(player, score) looks into both boxes and prints Anna 0.
  4. score = 50 opens the box score and puts 50 in it instead of 0.
  5. The second print prints Anna 50: the name is the same, but the score has changed.

Two declaring words

To declare a constant or a variable is to make a named box for it. A constant and a variable are declared the same way; only the first word differs. The line var score = 0 has four parts:

  • var — which kind of box to make;
  • score — the name on the label;
  • = — the sign the value comes after;
  • 0 — the starting value.

let: a constant

The word let reads like plain English: let player = "Anna" means "let player be "Anna"". A constant gets its value once, when it is declared. You can read it as often as you like, but you cannot put anything else in it.

var: a variable

The word var is short for "variable". You can give a variable a new value as many times as you like while the program runs.

Names

Names in Swift are usually English words. The first word starts with a lowercase letter, and every next word with a capital: score, maxScore, playerName.

Assignment

The line score = 50 is an assignment: it puts a new value into a variable. There is no var in this line: the box already exists, so it is only opened.

An assignment first works out the right side, then puts the result into the box on the left. That is why a new value can be built from the old one:

SWIFT
score = score + 25
print(score)  // 75
  1. Swift reads the old value 50 from score.
  2. It adds 25 to it and gets 75.
  3. It puts 75 back into score, and 50 is gone.

Here the box picture is off. If you take something out of a real box, the box is left empty. But reading a value—in print(score) or in score + 25—takes nothing out: after the read, the box holds the same thing.

Press "Next step" and watch the two boxes: score changes on every assignment, while bonus holds 10 until the program ends.

LiveThe variable changes, the constant does notstep 1 of 9 · frames: 1

The constant bonus is sealed with the number 10 until the program ends.

Watch bonus: its value stays the same on every step

let first

Swift developers declare values with let and write var only when the value will change. The rule has two reasons:

  • a reader of the code sees at once that the value will not change and does not hunt for the line that rewrites it;
  • the compiler guards the constant and finds an accidental assignment to it before the program runs.

Interviewers like to ask: "What is the difference between let and var?" The answer has two parts:

  • let declares a constant: it gets its value once, and you cannot assign a new one;
  • var declares a variable: an assignment = can put a new value into it.

Common mistake

A value was declared with let, and then the code tried to change it:

SWIFT
let limit = 3
limit = 5 // ✗
TEXT
error: cannot assign to value: 'limit' is a 'let' constant

As you already know, the compiler checks the whole file before the program runs, so the program prints nothing, not even the lines above the error. Next to the error, Swift itself suggests the fix: change let to var. Do that if the value has to change: var limit = 3.

Key points

  • let declares a constant: it gets its value once, and you cannot change it.
  • var declares a variable, and an assignment = puts a new value into it.
  • An assignment works out the right side first, so score = score + 25 builds on the old value.
  • Declare with let, and write var when the value will change.