Understanding Types, Variables, and Constants

Discover what you can do with data types, constants, and variables in Swift. Try them yourself with the provided exercises.

Understanding data in Swift

When we look at the different types of software that run on computer systems and mobile devices, from financial applications to graphics-intensive games, it is easy to forget that computers are really just binary machines.

Binary systems work in terms of 1 and 0, true or false, or set and unset. All the data sitting in RAM, stored on disk drives, and flowing through circuit boards and buses is nothing more than sequences of 1s and 0s. Each 1 or 0 is referred to as a bit. Bits are grouped together in blocks of eight with each group being referred to as a byte. When people talk about 32-bit and 64-bit computer systems, they are referencing the number of bits that can be handled simultaneously by the CPU bus. Foe example, a 64-bit CPU, for example, is able to handle data in 64-bit blocks, resulting in faster performance than a 32-bit based system.

Humans, of course, don’t think in binary. We work with decimal numbers, letters, and words. In order for a human to easily (“easily” being a subjective term in this context) program a computer, some middle ground between human thinking and computer processing is needed. This is where programming languages such as Swift come into play. Programming languages allow humans to express instructions to a computer in terms and structures that we understand, and then compile that down to a format that a CPU can execute.

One of the fundamentals of any program involves data and programming languages such as Swift that defines a set of data types that allow us to work with data in a format we understand when programming. For example, if we want to store a number in a Swift program, we could do so with syntax similar to the following:

var mynumber = 10

In the above example, we have created a variable named mynumber, and then assigned to it the value of 10. When we compile the source code down to the machine code used by the CPU, the number 10 is seen by the computer in binary as:


Swift variables

Variables are essentially locations in computer memory reserved for storing the data used by an application. Each variable is given a name by the programmer and assigned a value. The name assigned to the variable may then be used in the Swift code to access the value assigned to that variable. This access can involve either reading the value of the variable or changing the value. It is, of course, the ability to change the value of variables that gives them the name variable.

Swift constants

A constant is like a variable because it provides a named location in memory that stores a data value. Constants differ in one significant way. That is, once you assign a value to a constant, you cannot subsequently change it.

Constants are particularly useful if there is a value that is used repeatedly throughout the application code. Rather than use the value each time, it makes the code easier to read if the value is first assigned to a constant, which is then referenced in the code. For example, the reason you used the value 5 in an expression might not be clear to everyone that reads your Swift code. Instead of the value 5, if you use a constant named interestRate, the purpose of the value becomes much clearer. If you had multiple references to interesrRate across your program, you could just change the declaration of the constant rather than changing a value each time it’s referenced. This is one of the biggest advantages of constants.

As with variables, constants have a type, a name, and a value. Unlike variables, however, once a value has been assigned to a constant, that value cannot subsequently be changed.

Declaring constants and variables

Variables are declared using the var keyword and may be initialized with a value at creation time. If the variable is declared without an initial value, it must be declared as being optional (a topic that will be covered later in the course). The following example demonstrates the use of a Swift variable. The code begins by assigning a value of 10 to a variable named userCount. The Swift print statement is then used to display the current value of the variable. A different value is then assigned to the variable, and the new value is printed once again.

Press + to interact
var userCount = 10
userCount = 20

Constants are declared using the let keyword as follows:

Press + to interact
let maxUserCount = 20

Since maxUserCount has been declared above as a constant, the Swift compiler will report an error if we attempt to change the assigned value:

Press + to interact
let maxUserCount = 20
maxUserCount = 30 // Invalid code

As we will see later, the only time that a value may be assigned to a constant after it has been declared is when using type annotation.

Note: For greater code efficiency and execution performance, Apple recommends the use of constants rather than variables whenever possible.

Type annotations and type inference

Swift is categorized as a type safe programming language. This essentially means that once the data type of a variable has been identified, that variable cannot subsequently be used to store data of any other type without inducing a compilation error. This contrasts with loosely typed programming languages where a variable, once declared, can subsequently be used to store other data types.

There are two ways in which the type of a constant or variable will be identified. One approach is to use a type annotation at the point the variable or constant is declared in the code. This is achieved by placing a colon (:) after the constant or variable name followed by the type declaration. The following line of code, for example, declares a variable named userCount as being of type Int (integer):

var userCount: Int = 10

In the absence of a type annotation in a declaration, the Swift compiler uses a technique referred to as type inference to identify the type of the constant or variable. When relying on type inference, the compiler looks to see what type of value is being assigned to the constant or variable at the point that it is initialized, and then it uses that as the type. For example, consider the following variable and constant declarations:

var signalStrength = 2.231
let companyName = "My Company"

During compilation of the above lines of code, Swift will infer that the signalStrength variable is of type Double (type inference in Swift defaults to Double for all floating-point numbers) and that the companyName constant is of type String.

When a constant is declared without a type annotation, it must be assigned a value at the point of declaration:

let bookTitle = "Swift Programming Essentials"

However, if a type annotation is used when the constant is declared the value can be assigned later in the code. For example:

Press + to interact
let courseTitle: String
courseTitle = "Swift Programming Essentials"

Lesson recap

  • Variables and constants provide storage for values.
  • Variables are declared using the var keyword.
  • Constants are declared using the let keyword.
  • For greater code efficiency, the use of constants instead of variables is recommended whenever possible.
  • Variables are mutable (the assigned value may be changed).
  • Constants are immutable (the assigned value cannot be changed).
  • Type annotation involves specifying the variable type when it is declared.
  • Type inference allows the compiler to infer the data type based on the value being assigned.


Test your knowledge of Swift types, variables, and constants.


What keyword is used to declare a variable in Swift?







Question 1 of 40 attempted


Use these exercises to put the theory covered in this lesson into practice.

Exercise 1

Declare a String variable named myString, which is initialized with a value that reads “Hello Swift” using type annotation.

// write your code here

Exercise 2

Declare an uninitialized integer constant named myInt, and then assign it a value of 10.

// write your code here

Exercise 3

Correct the following code so that it no longer causes a compiler error:

let userCount = 10
userCount = 20