Difference between Struct and Class in Swift
If you are just starting out in Swift it’s very easy to get confused with structure and classes. Don’t worry I will help you understand it better.

So before I start talking about the differences here are some similarities between both of them. Structure and Classes both can :
- Define properties to store values
- Define methods to provide functionality
- Define subscripts to provide access to their values using subscript syntax
- Define initialisers to set up their initial state
- Be extended to expand their functionality beyond a default implementation
- Conform to protocols to provide standard functionality of a certain kind
Hmm, thats a lot of similarities right. Still the things that really separates one from the other are as follows:
- Inheritance
Classes have a property called inheritance which is not available in Structure and basically what inheritance means is that a sub-class can inherit properties from a Super class and also add some more properties of its own on top of the properties it is inheriting. Let me give you an example to help you understand it better.
Suppose we are building an inventory software for a store and to do that we build a “Product” class.

But as we were developing our inventory program, we realise that the store also has promotions running on some products, so our program also need to check for how much discount store are giving on those products in addition to attributes: ‘price’ and ‘shelfLife’ and to do that we develop another class as follows:

Here the ‘Product’ class act as a superclass of ‘promotionalProducts’ class and what that means is that ‘promotionalProducts’ class will now also have a discount attribute in addition to all attributes that are present in ‘Product’ class.

2. Immutability
Structs are immutable. When a method(function) that is declared in a struct tries to change an attribute, it does not allow any change.

In order to allow a change in attribute we need to specify that the function is a ‘mutating’ function in struct.

So what actually happens here is that, when the mutating function gets triggered the old struct gets destroyed and a new struct is formed with the updated values.
Because all of the constants are immutable by default they cannot be used with a mutating method. If you are using a mutating method it is compulsory that all the attributes of a struct are variables.
3. Structures are passed by value
One of the key difference between a struct and a class is that the struct is passed around by values. To understand it better let me give you an example.
Let’s assume that I have a notebook and I want to give you that notebook. So when I am giving you that notebook what I am essentially doing here is that I am giving you a copy of notebook.

So now if you scribble your name on the notebook it does not affect the original notebook and it will remain as it was.

Let’s look at this programatically:
So I have a product struct with a discountPrice method as follows,

Using the ‘Product’ struct I create two variables namely noteBook1 and noteBook2 with initial price value set as 10. Here noteBook2 is a copy of noteBook1 but when I discount the price on the noteBook1 by 1 the price of the noteBook2 does not get affected.

This means that the structure is not just updating its value or is referencing the value through the noteBook1 but actually noteBook2 is a completely new structure. Similar to the example: when I am giving you noteBook2 it is actually a completely new copy of noteBook1, while the original (noteBook1) remains unaltered with me and does really affect what you do with your copy.
Now you must be thinking, pff obviously changes to noteBook1 are not going to affect noteBook2, its just a copy. Right! but let’s look at the similar example using class and see what happens. That brings us to our fourth difference.
4. Classes are passed by reference
Let’s assume the earlier notebook example once again. I have a notebook and I want to give it to you but this time instead of giving you a copy of my notebook I am just giving you a reference.

So if I have four notebooks and I want to give you a notebook that is titled ‘Prajval Raval’, I will give you a reference to it i.e. “://bluefolder/prajvalraval”. That means you are getting access to my original notebook and all the properties or text that comes with it and thats what happens when you pass data using class.
Let’s look at this programatically:
So I have a product class with a ‘discountPrice’ method as follows,

Using the ‘Product’ class I create two variables namely noteBook1 and noteBook2 with initial price value set as 10. Here noteBook2 is a copy of noteBook1 but when I discount the price on the noteBook1 by 1, this time the price of noteBook2 also gets changed by 1.

It is an advantage as well as downside to class. Downside🤔, how you ask. In the example I gave you the reference to my original notebook that means if for some reason you decide to destroy the notebook that I gave you, it pretty much destroys the notebook for all of us.

Similarly if a subclass mutates a property in the the superclass, that it’s inheriting from, the values affected are not just limited to that subclass but all the subclasses that are inheriting from that mutated superclass gets affected.
Conclusion:
In the end it really narrows down to your use case and what works better for you. However, Apple recommends that you use a struct . To read in more detail about Structure and Class I suggest you read the official Swift documentation here.
Thank you for reading this article I hope this article helped you. Please don’t forget to give a 👏 so others also get a chance to read it. Also if you have some feedback for me, feel free to send a connection request on my LinkedIn profile .