A little gist about class and struct: Deciding when to use class or struct when designing your app

Ayodeji Ayankola
3 min readJul 9, 2024

--

Classes:
⁠ — ⁠They are reference types: For example, when we create a house class with a specific address, whenever we give that house address to someone, we are still referencing the house, and everyone who has the address is still referencing the house.
Reference Counting: Swift provides built-in memory management functionality for class instances using automatic reference counting (ARC). This means that in the example above, it tracks how many people (or references) have the address to the house. When someone stops having the address, the reference count reduces by one, and vice versa. At the moment, no one has a reference to it. The count becomes zero. Since classes have to allocate memory and create objects, we need to be aware that it is widely used in the object-oriented paradigm (every new instance is a creation of an object), and memory allocation is done in HEAP for dynamic memory allocation, which means it can handle objects that are created and destroyed as the app runs.

Struct:
⁠ -⁠They are value types: For example, when we create a defined rule on how a clock is to be built (blueprint). Every time someone or an organization follows the rules in creating their type of clock, everyone will have a different copy/design/brand of the blueprint even though they follow the blueprint for creating it (we can see this in everything we use or follow as a design pattern in a part of our codebase). Since there is no reference to the blueprint, structures are stored in a memory area called the stack, which is faster for certain types of memory allocation. So there is less work to be done since it doesn’t need to keep track of how many copies of the blueprint exist.

So what are some differences?
⁠- Struct is generally faster when it comes to performance since no reference counting is needed. Keep in mind that STACK is currently faster compared to the heap when it comes to memory allocation, even though it is smaller than the heap in size.

⁠-Since we do not have multiple references pointing to the same instances, there are little to no issues with multiple threads modifying the same instance simultaneously. This can generally help avoid memory issues like strong reference cycles, which can lead to memory leaks.

My final thought

⁠ — ⁠Use classes when you have a shared instance (mutable state) across and when you need to use inheritance to create a group of related classes that share some common features amongst others.
⁠ ⁠- Use structs when you are working with basic data types that don’t need complicated behaviour and when you want each instance to be a unique copy so changes to one won’t affect others. You need better performance with less memory management, especially in a multithreaded environment.

For further read using Apple documentation: https://developer.apple.com/documentation/swift/choosing-between-structures-and-classes

The code attached summarises my thought

--

--

Ayodeji Ayankola
Ayodeji Ayankola

Written by Ayodeji Ayankola

I am an iOS Engineer with over four years of experience in developing cutting-edge mobile applications that enhance user experiences and drive business growth.

No responses yet