Difference between Struct and Classes explained in Swift


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"
//prints Tim

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

//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"
//prints DW Bank  
//prints DW Bank  

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


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.


Next Posts

The ultimate guide to iOS Unit Testing with Swift and Xcode

Unit testing is a testing method where you can test “unit” of code whether it is working as you want or not. In Xcode, use XCTest framework to perform unit tests.

Jun 29, 2020 . 2 min read     Xcode Testing

Read More »

Custom Operators in Swift Empty space for void

Swift provides a very useful feature which is to define your own custom operators. It is a very confusing topic in the beginning but it is very powerful to use.It increases our code readability..

May 04, 2020 . 2 min read     Swift

Read More »

Protocol Extensions in Swift Empty space for void

A protocol can defines a set of methods that can be adopted by any class, but we can’t write code inside.On the other hand, extensions gives us the power to write code inside methods..

May 01, 2020 . 1 min read     Swift

Read More »