Class vs Struct — iOS Swift

Vinod Basnure
4 min readNov 12, 2021

If you reading this, it means you were asked this question in any of the iOS Development interviews that you attended, which might or might not have gone your way, but don’t you worry, I am here to give you every insight that matters on this topic.

Not just for the sake of interview, but as an iOS developer who uses Swift as the programming language, it’s our responsibility to know this and knowing this will only make us a better iOS developer, and at the end of the day that is what matters the most.

Let’s dive right into it..

Class

Major difference between Class & Structs in Swift is:

  • Class is a reference type
  • Struct is a value type
What’s this now ???

Don’t you worry, I have got you covered…

Reference Type:

  • An instance of reference type shares single copy of data. That means if you create an instance of a reference type and assign to a variable and if you pass that variable to a function or if you assign that variable to another variable, you are just passing a reference to the instance you just created and if you try to access and modify from any place using that reference, the actual instance gets changed.
  • Alongside Classes, Functions and Closures are reference types.

Value Type:

  • Each instance of this type creates a new copy of data every time it’s passed around in code. And each instance keep unique set of data with itself. Imagine if you created an instance of value type and if you pass around this instance, it’s another copy is made and that’s passed so if you make changes to the copy that’s passed the original instance will have no change.
  • Primitive data types like Int, String, Double and also Swift’s data types like Array, Dictionary, Set, Enum, Tuple are value types.

Now we will look at the other differences between Classes and Structures.

Things only Classes can do

  • Inheritance
  • Type Casting
  • ARC (Automatic Reference Counting)
  • Having Deinitializers
  • Objective-C Interoperability

Things that Classes and Structures have in common:

  • Define properties to store values
  • Define methods to provide functionality
  • Be extended
  • Conform to protocols
  • Define intialisers
  • Define Subscripts to provide access to their variables
Where should I use Class and where to use Structs ?

Classes can be used under the following scenarios:

  1. When you need Objective-C interoperability in your application. Which means you are working on a project in which you have to expose your swift code to Objective-C environment and may be vice versa.
  2. And when you have to control the identity of the data you are modelling. That basically means if you are passing your data from one class to multiple classes and if your data is accessed in a multi-threaded environment and you want to protect the identity of your data you can go for using classes.

Structs to rescue when:

  1. Well, the Apple documentation says just use Structs by default. Means if you can believe you can achieve the same functionality using that you wanted to classes for then you can use Structs as well provided you aren’t worried about the state of your identity of your data and you don’t have Objective-C Interoperability in your app.
  2. And to achieve this functionality you can use Structs along with Protocols.

Guess what, the more you avoid classes, the more you don’t have to worry about the retain cycles and memory management in your app.

We are just one final hurdle away from finishing our topic. And that is as follows

Memory Allocation of Classes and Structs

  • Classes which are of Reference types are stored on Heap Memory
  • Structs which are of Value types are stored on Stack Memory.

In Swift Reference types are stored on Heap memory and Value types are stored on Stack memory, that’s the reason Classes are stored on Heap memory and Structs are on Stack Memory. But both are stored in Computer’s RAM.

Heap follows Dynamic Memory allocation and Stack follows Static Memory allocation.

For more information on Static vs Dynamic memory allocation you can check this article from StackOverFlow.

That’s it… See ya!!!

If you liked this article you can share this to your fellow iOS developer friends and please give a clap so that it can reach others.

completion image.

--

--