Thursday, September 17, 2020

iOS Swift 5 Structs VS Classes Deep dive

 


iOS Swift 5 Structs Vs Classes:

Hello there!.. Let us deep dive into the Structs vs Classes and see how similar they and also how different they are.

How difference Structs and classes are:

  1. Initializations

Classes required manual initializer using init method in it.

Example :

Class PersonClass

{

var firstName : String?

var lastName : String?

var age : Int?

init(firstName : String , lastName : String, age : Int)

{

self.firstName = firstName

self.lastName = lastName

self.age = age

}

}

Structs have memberwise initializers where we don’t manually have to initialize using init method and also objects in the Struct don’t have to be optionals.

Example:

Struct PersonStruct

{

var firstName : String?

var lastName : String?

var age : Int?

}

        Why we don’t need optionals for objects in structs? This will be answered when we explain about Stack and Heap later in this document.

  1. Value Type VS Reference Type

Structs are value type where classes are reference type

To understand the concept let's relate this with a day to day example, Imagine Struct is an excel sheet which we share with someone so that they have a copy where they can edit But class is like google docs when you share it with someone the same document will get shared and both of them will edit the same doc.


    What is meant by Reference Type?

     When you copy a class variable to a new variable, We are copying the reference of the class variable to the new variable. If we change the value of one variable, that will affect the other as well.


Lets see the example:

        var person1 = PersonStruct( firstName : “Daisy” , lastName : “Rose”, age : 5)

var person2 = person1   (Here are we copying the reference of person1 to person 2)

person2.firstName = “Jasmine”

print(person1.firstname) (This will print “Jasmine” Because person2 

is a reference of person1)

What is meant by Value type?

            When you copy a Struct variable to a new variable, we are not copying the reference of it. But We are copying the object value and creating the whole new object.


Let me explain with an example.

var person1 = PersonStruct( firstName : “Daisy” , lastName : “Rose”, age : 5)

var person2 = person1   (Here are we copying the value of person1 to person 2 and

creating a new object lets see what that mean)

person2.firstName = “Jasmine”

print(person1.firstname) (this will print “Daisy” but not “Jasmine” Because person2 

is not a reference of person1 but the copy of person1 and 

Person2 is a whole new object)

In short, person1 and person2 are  independent on each other

Even though it's very confusing, we will try to clear the air by the end of this document.

  1. Functions

Functions of a class are mutable where functions in the Struct are immutable. 

When you create a function which modifies struct , the function should be muted using “mutating” keyword

Eg : 

Struct PersonStruct

{

var firstName : String?

var lastName : String?

var age : Int?

mutating func uppercaseFirstName

{

}

}


  1. Inheritance & Subclassing

Here you find the answers for a very important question, i.e, When to use Structs and When to use classes?


    Inheritance and Subclassing is supported in classes where they are not in Structs, When there is not dependency we will surely go for Structs.

This makes Structs light weight 

  1. Stack VS Heap

When you create a variable of Structs, it will get created in stack and removed when it returns. Stack is very efficient and very fast. This is the reason why we don't have to make variables optionals. When the object is not having a value stack will delete the object.

When you create a variable of classes, it will get created in Heap and It is not easy to clear the memory , But Heap is a huge amount of memory where system can request and dynamically allocate memory.

Similarities of Structs and Classes are:

  • Both have function

  • Both have confirmed to protocols

  • Both have computed properties

Quick Distinguishing:

Struct

Class

Value Type(Copy)

Reference Type(Share)

Memberwise initializer

Have to initialize with init method

Saved in Stack, Stack will get clear when the object is not in use or when it return

Saved in Heap, where a huge amount of memory & system can request dynamic allocated memory. Clear is the memory is not easy

Functions are immutable in struct where we have to make mutable using “mutating” keyword when the function will modify the struct

Functions are mutable in class

Conformed to protocols

Conformed to protocols

Have Functions

Have Functions

Have computed properties

Have computed properties


No

Inheritance, Subclassing and superclasses are available 

Recommended to useIf Needed to use
FastSlow

SwiftUI is built on mostly using Structs, lots of view structs are created and destroyed very quickly and perform well

UIKit is built using classes, which uses lot of subclassing, superclasses and inheritance