Value vs Reference Types
Every Swift type is either a value type or a reference type, and the difference decides what happens when you assign or pass it around. Getting this right is one of the most important skills in Swift — it explains why structs feel "safe" and why classes can surprise you.
| Value types | Reference types | |
|---|---|---|
| Which types | struct, enum, tuples — including Int, String, Array, Dictionary |
class, closures, actors |
| On assignment | The value is copied | The reference is copied; both point to one object |
| Identity | None — only contents matter | Each object has an identity (===) |
| Thread-safety | Easier: copies are independent | Shared objects need care |
Side by side
Here are two types with identical code — only the keyword differs:
struct PointS { var x = 0 }
class PointC { var x = 0 }
var s1 = PointS()
var s2 = s1 // copy
s2.x = 99
let c1 = PointC()
let c2 = c1 // same object
c2.x = 99
print("struct:", s1.x, s2.x)
print("class: ", c1.x, c2.x)struct: 0 99
class: 99 99
With the struct, s1 keeps its own value. With the class, changing c2 is visible through c1, because there is only one object.
Passing to functions
The same rule applies to function parameters. A function receives a copy of a value type, but a reference to a class instance — so it can modify the caller's object.
struct Wallet { var cash: Int }
class Account { var cash: Int; init(cash: Int) { self.cash = cash } }
func spend(_ w: Wallet) -> Wallet {
var copy = w
copy.cash -= 10
return copy
}
func spend(_ a: Account) {
a.cash -= 10 // modifies the caller's object
}
let wallet = Wallet(cash: 50)
let newWallet = spend(wallet)
print("wallet:", wallet.cash, "new:", newWallet.cash)
let account = Account(cash: 50)
spend(account)
print("account:", account.cash)wallet: 50 new: 40
account: 40
To let a function change a value-type argument in place, mark the parameter inout and pass it with &:
func double(_ number: inout Int) {
number *= 2
}
var score = 21
double(&score)
print(score)42
Collections are value types too
Array, Dictionary, Set and String are structs. Copying an array gives you an independent array — but if the array holds class instances, both arrays share those objects:
class Task { var done = false }
var todayStructs = [1, 2, 3]
var copyStructs = todayStructs
copyStructs.append(4)
print(todayStructs.count, copyStructs.count)
let original = [Task(), Task()]
let copy = original // new array, same Task objects
copy[0].done = true
print(original[0].done)3 4
true
Note: Swift's collections use copy-on-write: the actual element storage is only duplicated when one of the copies is modified. Copying a large array is therefore cheap until you change it.
let means different things
| Declaration | Can you reassign the variable? | Can you change its properties? |
|---|---|---|
let p = PointS() |
No | No — the whole value is constant |
var p = PointS() |
Yes | Yes |
let c = PointC() |
No | Yes (its var properties) |
var c = PointC() |
Yes | Yes |
Which should I choose?
Use a struct (value type) when:
- the type is mostly data (a coordinate, a settings record, a game score),
- copies should be independent,
- you compare instances by their contents.
Use a class (reference type) when:
- you need a single shared instance whose changes everyone sees (a cache, a session),
- you need inheritance or a
deinit, - identity matters more than contents.
Tip: When in doubt, start with a struct. Apple's own guidance and SwiftUI are built around value types.
Test yourself
Which of these is a value type?
- A
classinstance Array<Int>- A closure
- An actor
let c1 = Obj(); let c2 = c1; c2.x = 5 where Obj is a class. What is c1.x?
- Its old value
- 5
- nil
- Compile error
How do you let a function modify an Int variable owned by the caller?
- Mark the parameter
inoutand pass&value - Return nothing
- Use
letinstead ofvar - It is impossible