DEV Community

Gamya
Gamya

Posted on

Variables and Constants in Swift

Variables and Constants in Swift

Whenever you build software, you need to store information.

That information could be:

  • A wizard’s name
  • A ninja’s village
  • Your dog’s breed
  • A score, setting, or calculation result

In Swift, storing information starts with two core concepts:

Variables and constants

Let’s break them down using examples that are a little more fun than usual.


Variables: When Your Data Can Change

Here’s a simple Swift variable:

swift
var wizardName = "Harry Potter"
This creates a variable called wizardName.

A variable is a piece of data that can change over time as your program runs.

What’s happening in this line?

There are four important parts:

var
Tells Swift you’re creating a variable.

wizardName
The variable’s name.
You can choose any name, but descriptive names are best.

= (equals sign)
Assigns a value to the variable.

"Harry Potter"
A string value — text in Swift is written inside double quotes.

Swift doesn’t require semicolons at the end of lines, which keeps code clean and readable.

Changing a Variable’s Value

Because variables can change, you can update them whenever you want:
var ninjaName = "Naruto Uzumaki"
ninjaName = "Sasuke Uchiha"
ninjaName = "Kakashi Hatake"
What’s happening here?

The variable starts as "Naruto Uzumaki"

It’s updated to "Sasuke Uchiha"

Then updated again to "Kakashi Hatake"

Important rule:

You use var only once

After that, you just change the value

Each new assignment replaces the previous one.

Constants: When Your Data Should Never Change

Some values should never change.

For those, Swift gives us constants, created using let.
let hogwartsHouse = "Gryffindor"
This creates a constant called hogwartsHouse.

Once assigned, it cannot be changed.

If you try, Swift will stop you:
let hokageTitle = "Seventh Hokage"
hokageTitle = "Sixth Hokage"
❌ This will cause an error.

Why?
Because a constant is meant to stay constant.

The keyword “let” comes from mathematics—as in “let x be equal to 5.”
Important Rule (Seriously)

If Xcode shows errors because you tried to change a constant:

👉 Delete those lines.
Swift is protecting you from bugs.

Printing Values (Your Best Learning Tool)

When learning Swift, it’s incredibly helpful to see what’s inside your variables.

You can do this using print():
var dogBreed = "Golden Retriever"
print(dogBreed)

dogBreed = "Shiba Inu"
print(dogBreed)

dogBreed = "Border Collie"
print(dogBreed)
Each print() outputs the current value.

You won’t use print() much in real apps, but while learning, it’s your best friend.
Prefer Constants Over Variables

Here’s an important Swift habit to build early:

Use let by default.
Switch to var only when the value needs to change.
For example:
let animeSeries = "Naruto"
var episodeCount = 1
Why prefer constants?

Swift can optimize them better

Your code becomes safer

You avoid accidental changes

If something shouldn’t change, make it a constant.

Top comments (0)