Understanding variables and constants is one of the first steps in learning Swift. These building blocks will enable you to store, update, and manage data in your programs. In this post, we’ll cover what variables and constants are, how to use them, and why you should prefer one over the other depending on your use-case.

What Are Variables?

A variable is a named space in memory where you can store information that might change over time.

In Swift, variables are declared using the var keyword. You can think of a variable as a box with a label on it: you can peek at its contents and swap them out for something else when you need to.

Example:

var username = "Alice"
print(username) // Alice

username = "Bob"
print(username) // Bob

As shown above, after we set username to "Alice", we can later change it to "Bob".

What Are Constants?

A constant, by contrast, is a named space in memory whose contents cannot be changed after they are set the first time. This is helpful for values you know will never change during the execution of your program, like configuration data or things like the number of days in a week.

Constants are declared using the let keyword in Swift.

Example:

let pi = 3.14159
print(pi) // 3.14159

// pi = 3 // This line would cause a compile-time error!

If you try to change the value of a constant, Swift will stop you with a compile-time error. This is a great way to write safer code because you know certain values won’t accidentally be changed.

Choosing Between var and let

A general rule in Swift is: prefer let unless you know you need to change the value. This leads to safer, more predictable code. You can always change a let to a var later as your needs evolve, but starting with let makes your intentions clear.

For example, if you’re assigning a value that will never change, such as an API endpoint or a constant from mathematics, use let:

let maxScore = 100
let baseUrl = "https://api.example.com"

If a value may change, such as a score that updates during a game, use var:

var currentScore = 0
currentScore += 10 // Score increased!

Type Annotations

Swift can usually figure out the type of your variable or constant from the value you assign, but you can also specify it explicitly:

let year: Int = 2024
var temperature: Double = 36.6

This helps with code clarity and prevents mistakes.

Multiple Assignments

You can declare several variables or constants on the same line, but consider readability:

var x = 0, y = 1, z = 2
let red = "Red", green = "Green", blue = "Blue"

Why Bother with Constants?

Using constants in your code is a great habit because:

  • Swift can optimize your code more effectively.
  • It helps other programmers understand your intentions.
  • It prevents bugs caused by accidental changes.

Summary Table

KeywordCan Change?Example
varYesvar counter = 0
letNolet pi = 3.14

For more on Swift basics, check out other posts in this category.


Understanding variables and constants is fundamental to programming in Swift; once you’ve mastered these concepts, you’ll be ready to build more complex structures like functions or data types.

Happy coding! 🚀