Skip to content
tezvyn:

Swift struct vs class: differences and when to choose each

Source: developer.apple.comEasyHow cards are made

Swift struct vs class: differences and when to choose each

Tests value vs reference semantics and let mutability. Strong answers contrast copy behavior with shared references, note allocation tendencies, and choose struct for value semantics or class for identity.

What's really being asked

The interviewer wants to see if you understand value semantics versus reference semantics, not just memorize a feature list. At the senior level, they care that you know how memory layout, mutability, and thread safety interact. They want to hear you reason about identity, shared mutable state, automatic reference counting, and when copy-on-write optimizations matter.

The full answer

First, structs are value types and classes are reference types. This means assigning a struct copies the instance, while assigning a class copies the reference to the same heap memory. Second, memory allocation: structs are typically allocated on the stack or inline within other values, whereas classes are always allocated on the heap and incur reference counting overhead through ARC. Third, mutability rules with let: a let struct is completely immutable because the entire value is fixed, but a let class only prevents reassigning the reference; mutable properties on that class can still be changed unless they are explicitly marked otherwise. Fourth, inheritance and identity: classes support inheritance and have object identity, so === works, while structs do not support inheritance. Fifth, decision criteria: choose struct for small data models, value semantics, and thread safety because each context gets its own copy; choose class when you need identity, inheritance, a deinitializer, Objective-C interoperability, or shared mutable state across multiple owners.

The mistakes people make

Claiming structs are always faster or always live on the stack. Swift may allocate structs on the heap when they are large, captured in closures, or part of a class. Another red flag is saying you always use classes for view models or everything in UIKit; senior candidates should explain that structs are preferred by default in Swift and classes are chosen deliberately. Also, confusing mutability by saying let makes a class immutable is a major mistake. Finally, ignoring that classes can create retain cycles while structs cannot because they are not reference counted.

What usually comes next

How does copy-on-write work with collections like Array and String? What happens when a struct contains a class property? How would you implement copy-on-write for a custom struct wrapping a buffer? Can you give an example of a race condition with a class but not a struct? When would you use a struct with a class-backed property to get value semantics?

A concrete example

Imagine a Canvas app where each stroke has a color and an array of points. If Stroke is a struct, undoing a stroke simply reverts the canvas array to the previous value without affecting other canvases. If Stroke were a class, copying the canvas array would share the same stroke references, so mutating a stroke for undo would corrupt the redo history or other canvas instances. Therefore, Stroke should be a struct, while the CanvasRenderer that manages a shared graphics context and coordinates with Core Graphics should be a class because it needs identity, shared mutable state, and a deinitializer to clean up resources.

Interview question

In Swift, how does declaring an instance with let affect property mutability for a struct compared to a class?

  • a.A let struct lives on the stack and cannot be mutated, while a let class on the heap allows property changes.
  • b.Using let makes both structs and classes completely immutable, preventing any property changes after initialization.
  • c.With let, a struct is fully immutable, but a class only fixes the reference so its variable properties can still be mutated.Correct
  • d.The let keyword blocks property changes in classes but not in structs, since structs are copied when modified.
Why?

The card explains that let makes a struct fully immutable, while for a class it only fixes the reference, allowing variable properties to still be mutated. Option B is wrong because it assumes let behaves identically for both, which is a common misconception.

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

Read the original → developer.apple.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

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