Difference between Struct and Classes explained in Swift

Swift Classes Structures

06 May 2020 . 2 min read       @gurjitpt


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


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


Generic placeholder image

WRITTEN BY

Gurjit Singh

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


Next Posts

How to use enum in Swift Empty space for void

Enumerations (enum) enables you to write code in a type safe way. Enum is very useful while defining common type of values. You don't have to provide...

Mar 7, 2023 . 2 min read     Swift Enum

Read More »

Optional unwrapping syntax in Swift 5.7 Empty space for void

Optional unwrapping is one of the most used patterns in iOS development. Swift 5.7 introduces new features included a new way to unwrap optional values...

Jun 14, 2022 . 2 min read     Optional unwrapping

Read More »

Property observers in Swift Empty space for void

It's a common pattern or technique in various programming languages when we want to perform some action when a value is changed. Property ...

May 24, 2021 . 2 min read     Property Observers

Read More »