Swift values and variables
Hub › iOS › Beginner › Swift values and variables
Goal
You will read and write the two Swift keywords that hold values: let and var. After this page you will be able to declare typed and inferred values and tell the compiler when something is allowed to change.
Prerequisites
let, var, and type inference
Swift has two ways to name a value:
letmakes a constant. Once set, the name always points to the same value.varmakes a variable. The name can be reassigned later.
Swift is statically typed: every value has a type known at compile time. You can write the type yourself or let the compiler infer it from the assigned value.
The three built-in types you will use constantly in SwiftUI are String, Int, and Bool. Arrays are written [Element], so an array of strings is [String].
For the full Swift language reference, see the Swift book. This page covers only the pieces the rest of the tier needs.
Try it in a playground
In Xcode, choose File → New → Playground, pick Blank under the iOS tab, save it as SwiftBasics.playground. Replace the contents with:
import Foundation
let name: String = "Mei"
var streak: Int = 3
let isActive: Bool = true
streak = streak + 1
let items: [String] = ["Read", "Walk", "Sleep"]
let firstItem = items[0]
print(name, streak, isActive, firstItem)Run the playground by clicking the play button next to the last line, or press Shift-Enter. The bottom panel shows:
Mei 4 true ReadTwo things to notice:
firstItemhas no explicit type. Swift infersStringfromitems[0].- Trying to reassign
name = "Aarav"would fail with a compiler error, becausenameislet. Try it — you will seeCannot assign to value: 'name' is a 'let' constant.
When to pick which
Default to let. Only reach for var when you actually need to reassign. SwiftUI views read constants more than they read variables — most properties on a view are let-ish by default.