Difference between Struct and Classes explained in Swift

Swift Classes Structures

Posted on 06 May 2020 . 2 min read


Structures and Classes are basic templates for any application which consists of properties and methods implements for behaviour . You can define structure or class in a single file and there are lot of similarities between both structures and classes like defining properties, methods, initializers etc.


The object refer to class in another languages is called instance of class in Swift.


Difference between Structures and Classes

  • Inheritance: Classes can inherit characteristics of another class which is not possible in Structures.
  • Deinitialization: Classes instances can use deinitializer to free up memory after it is no longer in need which structures don’t have.
  • ARC(Automatic Reference Counting): It only applies on instances of classes because classes are reference type. It can’t be used in structures because structures are value types.
  • Type casting: It allows us to check type of an instance of class at runtime, which is drawback of structures.


Defining a struct in Swift

struct Customer {
   var name: String
   let account: String
}


Defining a class in Swift

class Bank {
   var bankName: String
   let location: String
   
   init(bankName: String, location: String) {
       self.bankName = bankName
       self.location = location
   }
}


Difference between Reference types and Value types

As we mentioned earlier, classes are reference types and structures are value types.The examples below will differentiate how classes and structures are different types.


A value type means that it can copy value when assign it to variable or constant.


In the example below, declares constant and set to instance of Customer structure. Then, declare another variable and set constant value to variable. Now, both constant and variable have same characteristics.

let ana = Customer(name: "Ana", account: "AC4832000001245864")
var john = ana


Let’s set name property of john to “Tim” and when we check name property of john it changed.

john.name = "Tim"
print("\(john.name)")
//prints Tim


But the name property of ana still has old value “Ana”

print("\(ana.name)")
//prints Ana

On the other hand, reference types can’t copy when assign it to constants or variables.


In the examples below, declares constant and set to instance of Bank class. Then, declare another variable and set constant value to variable.


Now, change bankName property of dwBank and when we check bankName properties of dwBank and stiBank they changed both and have same value.

let stiBank = Bank(bankName: "STI Bank", location: "10th Street")
var dwBank = stiBank 
dwBank.bankName = "DW Bank"
print("\(dwBank.bankName)")
//prints DW Bank  
print("\(stiBank.bankName)")
//prints DW Bank  


So, because of classes are reference types both stiBank and dwBank refers to same Bank instance.


Conclusion

There is not much difference between structures and classes. But classes have more advantages than structures. So, decide wisely whether to use classes or structures according your need.


Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.

Thanks!


Share this article



Written By

Generic placeholder image

Gurjit Singh

I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me for more updates:


Discover articles by topics

SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView

Related Articles


Deep Dive into Autorelease Pools in Swift

In the realm of software development, memory management plays a crucial role in ensuring the efficient allocation and deallocation of memory...

2024-01-28 . 4 min read     Swift Autorelease

Read More »

Swift enum equatable: with or without associated values

Swift enums provide a powerful way to model a set of related values. Enums can be equipped with associated values, allowing them to represen...

2024-01-24 . 3 min read     Swift Enums

Read More »

How to create Date Picker in SwiftUI

Use a DatePicker when creating a view that enables users to choose both a calendar date and, if needed, a specific time.In SwiftUI, you can ...

2024-01-16 . 2 min read     SwiftUI DatePicker

Read More »

Getting started with Swiftlint to enforce Swift style

SwiftLint is a tool that ensures Swift code adheres to defined style guidelines. It automates code review by identifying and suggesting impr...

2023-12-29 . 4 min read     Swift SwiftLint

Read More »

How to use Lottie animation in SwiftUI

Lottie is a fantastic tool for incorporating high-quality animations into your SwiftUI projects. There are several ways to add Lottie to pro...

2023-12-13 . 2 min read     SwiftUI Lottie

Read More »

How to get index in Foreach in SwiftUI

In SwiftUI, the ForEach is used to iterate over a collection of data and create views dynamically based on that data....

2023-12-07 . 3 min read     SwiftUI ForEach

Read More »