In the previous episode of Swift Fundamentals, we briefly talked about variables and constants. You learned that the var keyword declares a variable and the let keyword declares a constant. Variables and constants both store values that can be referenced by a name.

There's one key difference that we didn't discuss, though. The value stored in a constant cannot be changed. The compiler throws an error if you try to modify the value of a constant. Try it out.

Creating Another Playground

Create a new playground by choosing the blank iOS template and name it Variables Constants and Types.

Creating an Interactive Playground in Xcode

Creating an Interactive Playground in Xcode

Choose a location to save the playground and remove the lines Xcode has added for us.

Creating an Interactive Playground in Xcode

Variables and Constants

Use the let keyword to declare a constant. Name the constant numberOfWheels and assign it the value 4.

let numberOfWheels = 4

The compiler throws an error if we try to assign the value 2 to the numberOfWheels constant.

numberOfWheels = 2

We're in luck, though. Xcode gives us a hint to fix the problem. Click the red circle on the left to find out what Xcode suggests we do.

The value stored in a constant cannot be changed.

The value stored in a constant cannot be changed.

Xcode is clear about the change we need to make to fix the issue. In Swift, a variable is mutable whereas a constant is immutable. Xcode suggests turning the numberOfWheels constant into a variable by replacing the let keyword with the var keyword. In other words, Xcode suggests making numberOfWheels mutable. The value stored in a variable can be changed.

Click the Fix button to go with Xcode's suggestion. Notice that the only difference with the original code snippet is the var keyword.

var numberOfWheels = 4
numberOfWheels = 2

Why would anyone want to use constants? Variables are much more flexible. Whenever you use or encounter a constant, you can be sure its value won't change. For a developer, that's a bold promise. Constants add safety to your code and safety happens to be a fundamental concept of the Swift programming language.

Use constants for values that won't change. Use variables for values that will change.

Types and Type Inference

Swift is a statically typed language. This means that every variable and constant has a type. The numberOfWheels variable is of type Int, an integer. Why is that? How does the compiler know that?

var numberOfWheels = 4
numberOfWheels = 2

In most cases, the compiler is clever enough to infer the type of variables and constants. The compiler inspects the value we assigned to numberOfWheels and infers that numberOfWheels should be of type Int. This is known as type inference, a powerful feature of the compiler.

The type of a variable or constant cannot be changed. Let's assign a string to the numberOfWheels variable by wrapping the number in a pair of double quotes.

var numberOfWheels = 4
numberOfWheels = "2"

It doesn't take long for the compiler to complain. The error message shows us what the problem is. We attempt to assign a value of type String to a variable of type Int. That's a no go in Swift.

The type of a variable or constant cannot be changed.

The type of a variable or constant cannot be changed.

Type inference is convenient, but't is also possible to explicitly specify the type of a variable or constant. In this example, we explicitly declare the maximumSpeed constant as a Float. We tell the compiler that maximumSpeed is of type Float. That's what the colon in the expression stands for.

let maximumSpeed: Float = 210

Common Types

The Swift standard library defines the fundamental ingredients to build applications that are powered by the Swift programming language, including a range of types. Let's take a quick look at the most common types.

Numbers

We already covered integers and floats. The standard library also defines the Double type if you need more precision.

let a: Int = 1
let b: Float = 0.15
let c: Double = 45.5464623466

Strings

Strings are pretty flexible in Swift. Take a look at these examples.

let str1 = "Hello"
let str2 = String("World")
let str3 = "\(str1), \(str2)"

On the first line of code, we declare a constant str1 using a string literal. As in many other programming languages, a literal value is a value that appears directly in your source code.

let str1 = "Hello"

On the second line of code, we declare another constant str2 using an initializer. We cover initializers later in Swift Fundamentals.

let str2 = String("World")

On the last line of code, we declare a third constant str3 using a combination of a string literal and string interpolation.

let str3 = "\(str1), \(str2)"

String interpolation allows you to create a string from a combination of variables, constants, and expressions by including the values in a string literal. Notice that each value is wrapped in parentheses and escaped with a backslash. That's how string interpolation works in Swift.

Collections

The Swift standard library declares three powerful collection types, arrays, dictionaries, and sets. We cover collections in the next episode of Swift Fundamentals. This example gives you a taste of collections in Swift.

let array = [1, 2, 3]
let dictionary = ["a": 1, "b": 2, "c": 3]
let set = Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

What's Next?

Remember from this episode that variables are mutable and constants are immutable. Constants are just as important as variables and they add safety to the code you write. Every variable and constant has a type and the compiler uses type inference to figure out the type of a variable or constant.

In the next episode, we take a look at the collection types defined in the Swift standard library.