1.1 · Value vs reference types

The single most load-bearing decision in a Swift API: does an assignment copy the data, or copy a pointer to shared data?

Mental model. Ask “if I hand this to someone and they change it, do I see the change?” If yes → reference. If no → value. Everything else (struct vs class vs actor) follows from that answer.

Value vs reference

What an assignment really copies, and where the bytes end up.

  1. What is the difference between a value type and a reference type in Swift? When do you choose each? 1.1.1 · L1
    Value types (struct, enum, tuple) are copied on assignment and on passing into a function. Swift's compiler elides many copies, and stdlib types like Array add…
  2. What is the difference between struct, class, and actor? 1.1.2 · L2
    Struct — value semantics, no inheritance, members must be mutating to change self, no deinit. Class — reference semantics, inheritance, deinit, identity via…
  3. Where do value types live — stack or heap? 1.1.3 · L3
    It depends on escape analysis. A small struct that does not escape is usually stored inline in its stack frame or in registers. But a struct is moved to the…

Choosing the right type

Turning the semantics into a decision you can defend in review.

  1. When would you still use a class in a Swift-first codebase? 1.1.4 · L5
    Use a class when you need shared mutable identity that value copies would break: a socket, a video player, a URLSession, a cache. Also when you need deinit for…
  2. How do you design a public Swift type: struct vs class vs actor vs enum? 1.1.5 · L8
    Start with ownership. If copies should be independent → struct or enum. If there is one world object → class. If mutation must be serialized → actor, or a…
  3. When is teaching “always use structs” harmful on a team? 1.1.6 · L8
    When it produces giant mutable structs copied across threads, or when UIKit/NSManagedObject gets wrapped in cargo-cult structs that still share references…

Copy-on-write

How value types stay cheap even when they hold a lot of data.

  1. What is copy-on-write? Which types use it? 1.1.7 · L2
    Copy-on-write delays the actual buffer copy until a unique reference is about to mutate. On assignment, both values point at the same heap buffer with a…

Special types

Types that tell the compiler something about control flow.

  1. What is Never? Where do you see it? 1.1.8 · L2
    Never is an uninhabited type: it has no values, so a function returning Never can never return normally. It signals “this path does not continue” — fatalError…

More in Swift language