Struct vs Class in Swift: When and Why to Choose a Structure

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Struct or Class? The Eternal Question of a Swift Developer



Everyone who starts writing in Swift inevitably faces this question sooner or later. It would seem that both constructs allow creating objects with data and methods. But why does this question gather thousands of views on Stack Overflow, and why do experienced developers increasingly prefer structures?



The answer is simple: because the choice between struct and class directly affects the performance, thread safety, and architecture of your application. In this article, we will break down not just the theory, but real examples and scenarios where Struct unequivocally beats Class.



If you write in Swift and still use classes "out of habit" from Objective-C — this article is for you. Get ready to rethink your approaches.


The Main Difference: Value Type vs Reference Type



Before diving into the details, let's recall the fundamental difference:



  • Struct is a value type. When assigned or passed to a function, a copy of the data is created.
  • Class is a reference type. When assigned or passed to a function, a reference to the same object in memory is passed.


It seems like a small thing. But all the consequences we will now discuss stem from this "atomic" difference.



Example 1: Unexpected Behavior of Class



class PersonClass {    var name: String    init(name: String) {        self.name = name    }}

struct PersonStruct { var name: String}

// Testing the classlet person1 = PersonClass(name: "Anna")let person2 = person1person2.name = "Maria"

print(person1.name) // Maria! Changed for person1 as wellprint(person2.name) // Maria


Notice the problem? By changing the name of person2, we inadvertently changed it for person1 as well. After all, it's the same object in memory. In a small example, this is not critical, but in a real project, such a "side effect" can lead to hard-to-find bugs.



// Testing the structvar person3 = PersonStruct(name: "Anna")var person4 = person3person4.name = "Maria"

print(person3.name) // Anna! Everything is in its placeprint(person4.name) // Maria


The struct behaves predictably: each variable owns its own independent copy of the data. This is safe and transparent.



When is Struct the Obvious Choice?



Apple, in its documentation, recommends using struct by default. Let's list specific scenarios where the structure wins without a doubt.



1. Data that Should Be Immutable



If you are working with models that should not change after creation (for example, results of a server request), struct with constant properties is the ideal choice.



struct UserProfile {    let id: Int    let name: String    let email: String}

// Created and forget. Data is protected from accidental changes.let profile = UserProfile(id: 1, name: "Ivan", email: "ivan@example.com")


With a class, you could still accidentally change name if the property was declared as var. With a struct, the compiler forces you to think about immutability.



2. Small, Self-Contained Entities



If your model describes something simple — coordinates, sizes, color, monetary amount — use struct. It is logical and efficient.



struct Point {    var x: Double    var y: Double}

struct Money { var amount: Double var currency: String}

struct Rectangle { var origin: Point var size: Size}


Such types do not need inheritance or reference management. They simply

Blogs

Book Recommendations