Value Types and Immutability
See how let prevents mutation of value types.
Value Types and Immutability is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Structs Are Value Types
In Swift, struct and enum are value types. Assigning one to a new variable copies it.
struct Point { var x: Int; var y: Int }
var a = Point(x: 1, y: 2)
var copy = a
copy.x = 99
print(a.x, copy.x)let Locks the Whole Value
Declaring a struct with let makes the entire value immutable, including all its properties.
struct Counter { var value: Int }
let c = Counter(value: 0)
// c.value = 5 // compile error
print(c.value)var Allows Mutation
Use var when you intend to change a struct's properties after creation.
struct Counter { var value: Int }
var c = Counter(value: 0)
c.value = 5
print(c.value)Copy on Assignment
Each variable holds its own independent copy. Changing one never affects the other.
struct Box { var label: String }
var first = Box(label: "A")
var second = first
second.label = "B"
print(first.label, second.label)Value Semantics in Functions
Passing a struct to a function passes a copy. The original outside the function is unchanged.
struct Score { var points: Int }
func bump(_ s: Score) {
var local = s
local.points += 10
}
var s = Score(points: 0)
bump(s)
print(s.points)Immutable Properties
A property declared with let inside a struct can never change, even on a var instance.
struct User { let id: Int; var name: String }
var u = User(id: 1, name: "Ada")
u.name = "Bob"
// u.id = 2 // compile error
print(u.id, u.name)Arrays Are Value Types Too
Swift's Array, Dictionary, and Set are structs, so they copy on assignment.
var a = [1, 2, 3]
var b = a
b.append(4)
print(a)
print(b)Strings Are Value Types
String is also a value type. Mutating a copy leaves the original alone.
var s1 = "hi"
var s2 = s1
s2 += " there"
print(s1)
print(s2)Why Value Types Help
Value semantics prevent accidental shared-state bugs. You can reason about a value locally without worrying who else holds a reference.
struct Config { var retries: Int }
let defaultConfig = Config(retries: 3)
var custom = defaultConfig
custom.retries = 5
print(defaultConfig.retries, custom.retries)Immutability and Safety
Marking values let wherever possible makes code safer and clearer. The compiler enforces that they never change.
let limit = 100
let name = "stable"
print(limit, name)Reassigning a let Variable Fails
You cannot reassign a let binding, nor mutate the value it holds.
let pi = 3.14
// pi = 3.15 // compile error
print(pi)Quick Check
Test value-type behavior.
Recap
Structs and enums are value types: they copy on assignment and when passed to functions. A let binding locks the whole value, and a let property can never change. This gives predictable, isolated state.
struct P { var n: Int }
var a = P(n: 1)
var b = a
b.n = 9
print(a.n, b.n)Frequently asked questions
Is the “Value Types and Immutability” lesson free?
Yes — the full text of “Value Types and Immutability” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Value Types and Immutability”?
See how let prevents mutation of value types. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Value Types and Immutability” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Value Types and Immutability
- The mutating Keyword
- Mutating and self Reassignment
- Non-mutating Alternatives