Class vs Struct in Swift

Manasa M P
4 min readDec 28, 2020

Class and struct are general-purpose of a template to defines bellow characteristics.

  • Property
  • Method/functions
  • Initializers
  • Protocol
  • Extension

Class and struct looks similar except the keyword used to define. Below example is just a template definition.

1. Struct and 2. Class

What are common things in struct and class?

  • Both has Property, function, initializer, protocol and extension.
  • Define subscript to provide access to their values.
  • Failable init can be used in both struct and class
  • both can access property/function using dot syntax . e.g: structObjectName.propertyname or classObjectName.propertyname
  • both as default init method i.e .init()
struct and class initializer.

To know more on initializer in swift. click here

Things Available Only in Class:

  • deinit can be used only with class.
  • Classes can inherit the characteristics of another class. Struct can’t inherit from other struct.
  • Reference counting allows more than one references.
  • Type of class instance can be checked at runtime.

Things Available Only in Struct:

  • Struct has default member wise initializer. For class we need to define explicitly.
1. struct has default member wise initializer and 2. For class you need to explicitly define intializer
  • Structs have the benefit of mutation in safety as you can trust that no other part of your app is changing the data at the same time.
  • Because of mutation in safety benefit, struct is helpful in multi-threading.
mutating keyword used to modify self property in swift.

Difference between struct and class:

  • Class uses keyword class and Struct uses keyword struct .
  • For modifying the self property in struct from a function you need to define mutating at the beginning of function. Class doesn’t required anything.
  • One of the most difference is value vs reference type.
  • If you create struct instance with let property, you can’t modify its properties. If you create a class instance with let property, you can modify its properties. i.e:

To resolve above error, you can change let to var for the struct object i.e var b = B()

Value vs reference types:

  • Classes are reference type : which means reference to a location in the memory where class instance is stored.
  • Struct and enumeration are value type: which means each reference has it’s own copy of data
  • If you share/assign instance of class to different references, all these references will have a single shared data. That means any changes in that class instance will be available to every reference
  • If you share/assign instance of struct to different references, every reference will creates its own memory to store the data. Any changes in struct in one reference will not affect in other reference.
1. output:varsha, deeksha 2. varsha, varsha

In above example for struct, bstruct and aStruct refer to different memory location. Because of this changing name in bstruct reference will not affect the data in aStruct reference

In above example for class, b and a refer to same memory location. Because of this changing name in b reference will affect the data in a reference.

1. A,B,C are referring to same class object if we share the data through class. 2. A,B,C are referring to different struct object if we share the data through struct.

Why class and struct are different?

Memory allocation for both class and struct is different. Because of this difference, class and struct behaves differently while sharing its instances.

Value types are stored in stack memory and reference type are stored in heap memory.

Finally you need to decide which one we need to use whether class or struct. My recommendation is to go with struct.

How can you decide which one is better?

  • If you want to compare object by reference using === then go for class and If you want to compare object by reference using == then go fo struct.
  • Use struct when you need mutable copy and use class when you need single copy.

To know more about stack and heap memory management in swift. Please click here

I hope you find this blog helpful. If you enjoyed it, feel free to hit the clap button below 👏 to help others find it! and follow me on Medium. Thanks for reading. ❤❤❤❤

--

--