Skip to content
tezvyn:

Value vs. Reference Semantics in Swift

Source: swift.orgEasyHow cards are made

Value types are like emailing a document copy; changes don't affect the original. Reference types are like a shared Google Doc link; everyone edits the same instance. In Swift, structs are copies, while classes are shared references.

Why it exists

To control how data is shared and mutated in a program. Sometimes you need a unique, safe copy of data that can't be changed unexpectedly from elsewhere. Other times, you need multiple parts of an app to coordinate by working on a single, shared piece of information. Swift provides both models to solve these distinct problems.

The mental model

Think of sharing a document. A value type is like emailing a file; your friend gets their own copy to edit, and their changes don't affect your original document. A reference type is like sending a link to a Google Doc; any change your friend makes is visible to you immediately because you are both looking at the exact same document in the cloud.

How it works

When you assign a value type (like a struct or enum) to a new variable, Swift creates a full copy of the data. The new variable holds an entirely independent instance. When you assign a reference type (like a class), Swift only copies the reference—think of it as the memory address—not the data itself. Both the original and new variables now point to the exact same object in memory. Modifying the object through one variable is visible through the other because they share the one underlying instance.

When to use it

Use value types (struct) by default for your data models in Swift. They make code easier to reason about because a variable's state won't be changed by another part of your program unexpectedly. This prevents an entire class of bugs related to shared mutable state. Use reference types (class) only when you specifically need shared state and a single source of truth, like for a network manager or a database connection that must be accessed from many places.

When not to use it

Avoid using reference types (class) for simple data containers, like a model for a user profile or a settings object. This can lead to “action at a distance” bugs, where changing an object on one screen accidentally alters it on another. These bugs are notoriously difficult to track down because the cause and effect are far apart in the codebase.

One canonical example

Imagine a Document type. If it's a struct (value type), this code: var myDoc = Document(text: "Original"); var friendDoc = myDoc; friendDoc.text = "Edited"; leaves myDoc.text as "Original". The friendDoc is a separate copy. If Document is a class (reference type), the same code results in both variables pointing to the same object. Changing friendDoc.text to "Edited" also changes myDoc.text to "Edited", because there's only one shared document.

Interview question

When a struct and a class instance are independently passed to functions that modify them, which statement accurately describes the effect on the original instances?

  • a.Both the original struct and class are modified.
  • b.The original class is modified, but the original struct remains unchanged.Correct
  • c.The original struct is modified, but the original class remains unchanged.
  • d.Neither the original struct nor the original class is modified.
Why?

Structs are value types, so passing them to a function creates a copy, and modifications within the function do not affect the original. Classes are reference types, meaning the function receives a reference to the original instance, so changes made through that reference will modify the original object.

Just read this? Test yourself on what you have been reading.

Read the original → swift.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on swift — each one lists the topics its interview covers.

See open roles