Swift Variables
A variable is a named container that holds a value. Think of it as a labeled box: the label is the variable name, and what's inside the box is the value. Swift gives you two types of containers — one you can change, and one you cannot.
var vs let
var — Changeable Value
Use var when the value needs to change later.
var score = 0
score = 10
score = 25The variable score starts at 0, changes to 10, then changes to 25. Swift allows all three of these steps because you declared it with var.
let — Fixed Value
Use let when the value stays the same forever.
let appName = "StudyTracker"
// appName = "NewName" ← This line would cause an errorOnce you set a let constant, you cannot change it. Swift enforces this rule at compile time, so you catch the mistake before your app even runs.
Diagram: var vs let
var score = 0 let pi = 3.14 [ Box: score ] [ Sealed Box: pi ] | 0 | | 3.14 | +-------+ +------------+ Can change Cannot change
Naming Rules
Use Camel Case
Swift names start with a lowercase letter. Each new word inside the name starts with an uppercase letter. This style is called camelCase.
var userName = "Alice"
var totalScore = 100
let maxAllowedAge = 120Names Must Start With a Letter
Variable names cannot start with a number. 2speed is invalid; speed2 is valid.
Swift Allows Unicode Names
Swift supports emoji and non-Latin characters in names, though plain English names are recommended for readability.
var 🎯 = "target" // Valid, but unusualType Inference
Swift figures out the type of a variable from the value you assign. You do not need to write the type yourself unless you want to be explicit.
var age = 25 // Swift infers: Int
var price = 4.99 // Swift infers: Double
var name = "Alice" // Swift infers: String
var isActive = true // Swift infers: BoolExplicit Type Annotation
Sometimes you want to declare the type clearly, especially when you are not assigning a value right away.
var temperature: Double
temperature = 36.6
var city: String
city = "Tokyo"The colon followed by the type name tells Swift exactly what kind of data this variable holds.
Changing a var Value
var lives = 3
print(lives) // Output: 3
lives -= 1
print(lives) // Output: 2
lives = 5
print(lives) // Output: 5Multiple Variables on One Line
Swift lets you declare multiple variables at once, though separate lines are cleaner for readability.
var x = 0, y = 0, z = 0Printing Variables
Use string interpolation to include a variable's value inside a printed message. Place the variable name inside \().
var playerName = "Alice"
var score = 42
print("Player \(playerName) scored \(score) points.")
// Output: Player Alice scored 42 points.Why Prefer let Over var?
Xcode warns you when you declare a var but never change it. Swift's design encourages you to use let by default and only switch to var when a change is actually needed. This reduces bugs caused by accidental value changes.
Diagram: When to Use Which
Does the value need to change?
|
Yes -+- No
| |
var let
score pi
count appVersion
username maxRetries
Summary
Use let for values that never change and var for values that do. Swift infers the type automatically, but you can annotate it explicitly when needed. Name variables with camelCase and print them using \(variableName) inside a string.
