Value Types vs Reference Types

The interviewer asks

“What's the difference between value types and reference types in Swift? When would you use each?”

How to answer

A value type is copied on assignment and when passed, so every variable owns an independent value; a reference type is shared, so several variables point at one instance with an identity. Structs, enums and tuples are value types (and so are Array, Dictionary and String, which use copy-on-write to make copies cheap). Classes, actors and closures are reference types. Swift defaults to value types because local reasoning is easier: nothing changes behind your back, which is also why value types made of Sendable parts are automatically Sendable and can cross concurrency boundaries freely. I use a class when I need identity or shared mutable state (and an actor when that state is shared across tasks).

Key points

  • Value types: struct, enum, tuples; let makes the whole value immutable, var allows mutating methods
  • Reference types: class (identity via ===, inheritance, deinit, ARC), actor (reference + isolation), closures
  • A let reference only freezes the reference, not the object's properties
  • Copy-on-write: standard collections copy their buffer only when a shared buffer is mutated (isKnownUniquelyReferenced)
  • Concurrency: non-public structs/enums with Sendable members are inferred Sendable; classes need final + immutable Sendable state, a Mutex, or actor isolation
  • Noncopyable types (~Copyable): structs/enums that are moved, not copied, for unique ownership (file handles, tokens) with consuming/borrowing
  • InlineArray<N, Element> (Swift 6.2): fixed-size, inline storage value type with no heap allocation
  • A struct containing a class reference copies the reference, not the object (shared state leaks through)

If they dig deeper

Stack vs heap is an implementation detail (values can be boxed, e.g. in existentials or captured closures), so argue from semantics. ARC traffic as a cost of classes (and of structs containing many references). When final class beats a large struct (expensive copies, identity for UI or caching). @Observable models must be classes because observation relies on identity. Noncopyable types with deinit and consume; UniqueArray (Swift 6.4, iOS 27+) for a heap buffer with unique ownership.

Common pitfalls

Equating "value type" with "immutable" (a var struct is mutable) or "reference type" with "thread-unsafe" (actors are reference types). Putting a mutable class inside a struct and expecting value semantics. Using classes for plain data models by habit. Large structs mutated through many copies without COW. Marking a class @unchecked Sendable instead of making it a struct, an actor, or protecting it.

How to use it

swift
struct Point: Sendable { var x = 0, y = 0 }     // value: copied
var a = Point()
var b = a
b.x = 10                                         // a.x is still 0

final class Account {                            // reference: shared, has identity
    var balance = 0
}
let first = Account()
let second = first
second.balance = 100                             // first.balance is 100 too
print(first === second)                          // true

struct Wallet { var account = Account() }        // struct holding a reference
let w1 = Wallet()
let w2 = w1
w1.account.balance = 5                           // w2.account.balance is 5 as well

actor Ledger {                                   // shared mutable state across tasks
    private var entries: [Int] = []
    func add(_ amount: Int) { entries.append(amount) }
}

struct FileToken: ~Copyable {                    // noncopyable: unique owner
    let descriptor: Int32
    consuming func close() { print("closing \(descriptor)") }
}
func useToken() {
    let token = FileToken(descriptor: 3)
    // let copy = token                          // would move, not copy
    token.close()                                // consumed; can't be used again
}

Fixed-size inline storage (Swift 6.2):

swift
func rgbSum() -> Int {
    let rgb: InlineArray<3, Int> = [255, 128, 0] // no heap allocation
    var sum = 0
    for i in rgb.indices { sum += rgb[i] }
    return sum
}

Best practices

  • Default to structs and enums for models, DTOs and state
  • Use a final class for identity, shared mutable state, @Observable models, or framework subclassing
  • Use an actor (or @MainActor class) when mutable state is shared across tasks
  • Avoid mutable class references inside structs unless you implement copy-on-write
  • Reach for ~Copyable and InlineArray only in performance- or ownership-sensitive code

Related concepts

  • Copy-on-write, mutating functions, weak vs unowned, Sendable, actors, noncopyable types, InlineArray/Span

Further reading