Swift — Classes Vs Struct

Imad Ali Mohammad
3 min readFeb 12, 2023

5 Key differences between Classes and Struct in Swift

Structures and classes are general-purpose, flexible constructs that become the building blocks of your program’s code.

Classes are reference types, sharing a single copy of their data.

Struct are value types, keeping a unique copy of their data.

1. Struct gets an initializer for free

We all love the free giveaways. With struct, we get free initializers based on our property declaration. And from Swift 5.1, we even get initializers with default values. Classes won’t get initializers for free.

2. Classes allow inheritance, while Struct don’t

Classes allow inheritance, Inheritance enables one class to inherit the characteristics of another.

Swift is designed as a protocol-oriented programming language. And struct fits perfectly to this paradigm.

Why is Swift called POP? A: The protocol is a very powerful feature of the Swift programming language. Protocols are used to define a “blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.

Reason to use Protocol

Classes provide single inheritance and struct doesn’t support inheritance. Thus protocols were introduced.

  • Multiple inheritances are achieved using protocols
  • It can be used by not only classes but also by structures and enumerations.
  • It prefers to use value type instead of reference type.
  • It has a protocol extension that gives us common functionality to all types that conforms to a protocol.

3. Classes can be de-initialized, while Struct’s can’t

There are cases inside our classes when we need to add some code inside to manually deallocate or stop something. With structs, this is not allowed anymore and works as default.

4. Thread mutation safety

Some of the most dreadful and hard-to-debug bugs in programming are dealing with thread deadlocks. And this happens when two or more threads try to write at the same time on the same space, and they get blocked waiting for each other. This can’t happen with structs since they are value types and can mutate safely.

5. Stop worrying about retain cycles with structs

What are retain cycles? This is the state when two objects hold strong references to one another. Since the first object’s reference count cannot be 0 until the second object is released, and the second object’s reference count cannot be 0 until the first objet is released neither object can be released! It is self-evident that it is not possible to create retain cycles with value types, as they are passed by value.

When using structs and escaping closures, we can safely forget those weak and unowned keywords, as those, apply only to classes. One worry less to think about.

So, just like Apple suggests, we should always start with a struct by default until proven otherwise and change it to a class.

Edit:

Add On: Similarities Between Structs and Classes

Here are the similarities between structs and classes:

  • Use properties to store values
  • Define methods
  • Define initializers
  • Allow the extension to expand its functionality
  • Can conform to protocols

--

--

Imad Ali Mohammad

Senior iOS Engineer, Passionate about coding and building amazing apps.