Swift Structs vs. Classes: Value vs. Reference Types
In Swift, a struct is a copied value (like a new document), while a class is a shared reference (like a link to one document). Use structs for simple data like coordinates; use classes for shared state like a user session.
Why it exists
Swift provides two ways to define custom data types to solve two different problems. One tool is for creating simple, self-contained values that are safe to pass around (structs). The other is for creating complex objects that have a distinct identity and can be shared and modified from multiple places (classes). This distinction gives developers precise control over how data is stored and manipulated, which directly impacts performance and code correctness.
The mental model
A struct is a value type. Think of it as a blueprint for a unique, independent object. When you assign or pass a struct, you create a complete copy. Changing the copy leaves the original untouched. A class is a reference type. Think of it as a blueprint for an object that lives at a specific address in memory. When you assign or pass a class instance, you're just copying the address, not the object itself. All copies of the address point to the same, single object.
How it works
Structs are value types, typically stored on the stack, which is fast for allocation and deallocation. Because they are copied on assignment, you can reason about them locally without worrying about remote changes. Many of Swift's fundamental types, like String, Array, and Dictionary, are implemented as structs with copy-on-write optimizations to make copying efficient.
Classes are reference types, stored on the heap. Instances are managed by Automatic Reference Counting (ARC), which tracks how many references point to an object and deallocates it when the count hits zero. Classes support inheritance, allowing one class to inherit characteristics from another. They also support deinitializers to clean up resources before an instance is deallocated.
When to use it
Use a struct by default. They are safer because they prevent shared mutable state, a common source of bugs. They are perfect for modeling data that doesn't have a unique identity, like a Point on a coordinate plane, a Size, or a Color. If you can say that two instances with the same internal values are interchangeable, you want a struct.
Use a class when you need a single, shared identity. This is for objects that represent a specific entity, like a UserManager, a NetworkService, or a ViewController. If multiple parts of your app need to interact with the exact same instance, you need a class. Classes are also required for interoperability with Objective-C frameworks.
When not to use it
Avoid using a class for simple data aggregates. If you use a class for something like a Point, you risk one part of your code changing the point's coordinates and unknowingly affecting a completely different part of the app that holds a reference to that same point. This is called "spooky action at a distance."
One canonical example
Consider a struct Point { var x = 0, y = 0 }. If you create var p1 = Point() and then var p2 = p1, they are two separate instances. If you then write p2.x = 100, the value of p1.x remains 0. Now consider a class Window { var title = "" }. If you create var w1 = Window() and then var w2 = w1, both w1 and w2 refer to the same object. If you write w2.title = "Main", the value of w1.title also becomes "Main".
Interview question
When should a developer generally prefer using a Swift struct over a class for a new data type?
- a.When the data type needs to be managed by Automatic Reference Counting (ARC).
- b.When the data is simple, self-contained, and changes to a copy should not affect the original.Correct
- c.When the data represents a unique entity that needs a shared identity across the application.
- d.When the data type requires inheritance from another type.
Why? this is the answer
Structs are value types, meaning they are copied on assignment, and changes to the copy do not affect the original. This makes them suitable for simple, self-contained data where independent copies are desired. Classes are used for shared identity and inheritance, and are managed by ARC.
Just read this? Test yourself on what you have been reading.
Read the original → docs.swift.org
- #swift
- #ios
- #value types
- #reference types
- #memory management
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.
We are hiring for this. Open roles that interview on swift — each one lists the topics its interview covers.
See open roles