What is the difference between class and structure in swift ?

Hello everyone. Here we are again. Today we are going to look at the difference between structure and class in swift.

In swift, classes and structures are very similar. They have only a few differences. It is very important to know what the differences are rather than what is similar. Because these are the basic building blocks to make an efficient app. With that being said let’s begin with the similarity of these two :

  • Properties : This is used for storing data in classes and structures
  • Extensions : We can use extensions to extend our classes or structures.
  • Methods : It provides functionality in our classes or structures
  • Initializer : We use initializer to create a beginning function once our class or structure has been created . Structure has a free initializer. That means we don’t have to define an initializer function into structure while classes need it.
  • Subscripts : This provides access to the values using the subscript syntax.

So what about the differences between the two ?

  • Type : While classes use reference type, the structures use value type. I will explain what it is shortly.
  • Inheritance : Structure cannot inherit from other types while classes can. I think that is the only disadvantage of structure.
  • Deinitializer : Classes have an initializer to deallocate the occupied space in memory. Structure hasn’t a custom deinitializer.
  • Stored place : While class is stored in the HEAP on the RAM memory, structure is stored in STACK on the RAM memory. 
  • Immutability : Structure provides immutability. But class doesn’t provide as well as structure.

Apple document says, use struct as a default choice if you do not need an object-oriented architecture in that certain part of your code. What is value type and reference type ? Let me explain it in an example. We are going to create two seperate swift files for class and structure. My class file name is babies.swift and the structure file name is parents.swift . So let’s look at the content of these files.

babies.swift :

import Foundation
class Baby {
    var name : String
    var surname : String
    var gender : String
    init(nameValue: String, surnameValue : String, genderValue: String){
        self.name = nameValue
        self.surname = surnameValue
        self.gender = genderValue

parents.swift :

import Foundation
struct Parent {
    var fatherName : String
    var motherName : String
    var familySurname : String

As you can see there is no initializer in the Parent struct while the Baby class needs. Now we are going to create an instance from this class and structure in the main.swift file. The content of main.swift will be like :

import Foundation
let baby_1 = Baby(nameValue = "Alex", surnameValue = "Wilford", genderValue="Male")
var parent_1 = Parent(fatherName = "George",motherName="Rosaline",familySurname = "Wilford")

If we run the main.swift, the baby’s name and parent’s family surname will be shown on the output. Please pay attention on defining type of instances. We defined parent_1 as let notation while we defined baby_1 as var. That is because if we want to change a property’s value of struct later, we must define it as var notation. If we define as let then it will give an error. As we’ve said, structure provides immutability.

To understand value and reference type clearly we are going to create a copy of baby_1 and parent_1. Then we will change the value of our new instances and check if the old instance’s value was the same or not. Let’s change the main.swift file content as shown below :

import Foundation
let baby_1 = Baby(nameValue = "Alex", surnameValue = "Wilford", genderValue="Male")
var parent_1 = Parent(fatherName = "George",motherName="Rosaline",familySurname = "Wilford")
let baby_2 = baby_1
var parent_2 = parent_1
baby_2.surname = "Starwalk"
parent_2.familySurname = "Starwalk"

When you run the main.swift file, you will see that baby_1 surname has been changed while parent_1 family surname remains the same. This is because when you create a copy of class it will show the same reference to the original class. When you do any changes in the clone class that changes will be applied to the original class as well. Unlike this, structure uses value type instead of reference type. That means when you create  a copy of a structure it will create a new instance. So any changes won’t affect any other structure.

In this post we learned what is the difference between :

  • classes and structures
  • var and let notation as we create an instance.
  • reference and value types

You can find the source codes in this link : https://github.com/vedat73/classes-and-structures-in-swift 

I hope this post was useful for you guys. See you soon …

You may also like...