Latest Post

SwiftUI interview questions and answer How to display SwiftUI sheet Half screen SwiftUI CollectionView using Vgrid

Structures and Classes in Swift

Difference between struct and class in swift : Struct is basically know as value type object where as class is called as reference type object.

Struct

Struct  is the value type object, it is the member-wise initialiser. it is called as value type because it passes the value to  the function,  when u use the struct whole thing is duplicated.it points the value of the  object. it shows he value of the object

Defining a Struct

struct School {

    var clothes: String

    var shoes: String

}

Struct can be initialised as shown below 

var stev = School(clothes: "Shirt", shoes: "puma")
Accessing  the member  of Struct in Swift 
stev.clothes

stev.shoes

Example : CGPoint , CGRect

Classes

Class is the reference type of object. it passes the reference data to the function . when class is used each object points to the same original  object if you change one they all will change. it show the reference of the real object

class School {

    var clothes: String

    var shoes: String

}

Class can be initialised as shown below

class School {

    var clothes: String

    var shoes: String

    init(clothes: String, shoes: String) {

        self.clothes = clothes

        self.shoes = shoes

    }

}

Accessing the properties of class

var rob = School(clothes: "Pants", shoes: "rebook")

rob.clothes

rob.shoes

Ex. UIView, UIViewController

Struct Vs Class swift

Difference between struct and class in swift

Additional capabilities  only available in class compare to struct in swift?

 * Type Casting : We can check the type of the class in runtime.

    * Reference Counting : Allow more than 1 reference to the class instance

 * Inheritance : A class can inherit the property or characteristic of other class but in struct we cannot.

 * De initialisation : we can de-initialise the class resource at any instant.

Memory Allocation of Struct and Class in swift

  • Struct are allocated in Stack memory .
  • Reference of the class  object can be created on the Stack but the properties of the class object are store in the heap.

Class and Struct  Dispatch method

 Struct are static Dispatch where as class uses method Dispatch but the compiler will use static dispatch

When to use Struct and When to use Class in swift

Class is used when we need the control identity , Choose Struct  when we no need of control identity

Key Difference between Class and Struct

  1. Inheritance: Classes support inheritance while structs do not. This means that a class can inherit properties and behavior from another class, whereas a struct cannot.
  2. Reference vs Value types: Classes are reference types, which means that when an instance of a class is copied, both the original instance and the new copy refer to the same underlying object. In contrast, structs are value types, which means that when an instance of a struct is copied, a new instance with its own independent values is created.
  3. Mutability: Classes and structs behave differently when it comes to mutability. In Swift, properties of a struct instance cannot be changed if the instance is declared as a constant (using the let keyword). On the other hand, properties of a class instance can be changed regardless of whether the instance is declared as a constant or a variable (using the let or var keyword).
  4. Memberwise Initializers: Structs automatically generate memberwise initializers, which allow you to initialize all of the struct’s properties at once. Classes do not have memberwise initializers automatically generated by the compiler.
  5. Copying: As structs are value types, when you pass them as parameters to functions or methods, they are copied, whereas classes are not copied. This means that passing a large instance of a struct to a function can be expensive in terms of memory usage, while passing a class instance does not incur such a cost.

Overall, structs are generally simpler and more lightweight than classes and are best used for small, simple data types. Classes are more powerful and flexible and are better suited for larger, more complex data types that require inheritance and reference semantics.