Struct VS Class

When we need to decide how to store data and model behavior, there are two choices: Classes and Structs. In this article, I will explain what’s the difference between them:

https://c.tenor.com/2mcR5I9vrWAAAAAC/twins-twinsies.gif
Classes and Structs may look like the same person at first glance

 

At a glance: Class VS Struct

First, let’s take a look at what features classes and structs have in common:

  1. Both structs and classes can define properties to store values, and they can define functions.
  2. They can define initializers to set up their initial state, with init()
  3. They can be extended with extension
  4. They can conform to protocols, for example to support Protocol Oriented Programming
  5. They can work with generics to provide flexible and reusable types

Class support a few more capabilities that struct don’t have:

  1. Classes can inherit from another class, like you inherit from UIViewController to create your own view controller subclass
  2. Classes can be deinitialized, that is you can invoke a deinit() function before the class is destroyed
  3. Classes are reference types and structs are value types (this is important)

**Struct (Value Type) Explaination:

When you copy a value type (i.e., when it’s assigned, initialized or passed into a function), each instance keeps a unique copy of the data. If you change one instance, the other doesn’t change too.

//
//  ViewController.swift
//  Class&StructDifference
//
//  Created by JOYNAL ABEDIN on 4/9/22.
//

import UIKit

//MARK: - struct
struct InfoStruct {
    
    var name: String
    
    init(name: String) {
        self.name = name
    }
}

//MARK: - Class
class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        structCalling()
    }
    
    func structCalling(){
        let firstName = InfoStruct(name: "Joynal Abedin")
        var realName = firstName
        realName.name = "John Jackles"
        print("✅ struct: ",firstName.name)
        print("✅ struct: ",realName.name)
    }
    
}

Output:

✅ struct:Joynal Abedin

✅ struct:John Jackles

Here first of all, i created struct name InfoStruct and declare name properties and also initialize this properties. Then,

I passed value in InfoStruct & assigned value in firstName variable. Then put the variable another variable named realName. Now assigned new value in name properties using realName variable. And finally printed.

 

**Class (Reference Type) Explaination:

When you copy a reference type,  each instance shares the data. The reference ifself is copied, but not the data it references. When you change one, the other changes too.

//
//  ViewController.swift
//  Class&StructDifference
//
//  Created by JOYNAL ABEDIN on 4/9/22.
//

import UIKit

class InfoClass {
    
    var name: String
    
    init(name: String) {
        self.name = name
    }
}


class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        classCalling()
    }
    
    func classCalling(){
        let firstName = InfoClass(name: "Joynal Abedin")
        var realName = firstName
        realName.name = "John Jackles"
        print("🏛 class: ",firstName.name)
        print("🏛 class: ",realName.name)
    }
    
}

Output:

🏛 class:John Jackles

🏛 class:John Jackles

Github Repo: https://github.com/Joynal279/Class-StructDifference

Reference by: https://medium.com/@burakakyalcin/class-vs-struct-in-swift-f8c48eaacdba

https://www.appypie.com/struct-vs-class-swift-how-to

307 thoughts on “Struct VS Class

Leave a Reply to Бахрома для штор купить Cancel reply