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.
struct Customer {
var name: String
let account: String
}
class Bank {
var bankName: String
let location: String
init(bankName: String, location: String) {
self.bankName = bankName
self.location = location
}
}
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.
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!
Written By

Gurjit Singh
I’m Computer Science graduate and CompTIA Security+ certified SOC Analyst and Mobile Application Security Engineer with 10+ years of cross-platform development experience across iOS, Android, and web.
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
Static analysis of an iOS app using MobSF, identifying credential misuse, security misconfigurations, and privacy issues....
2026-05-29 . 4 min read MobSF SAST
Transitioning from software development into cybersecurity is one of the most natural career moves in tech today....
2026-05-26 . 4 min read Security Certificates
Swift 6.1, officially released in March 2025, continues the evolution of Apple's powerful and expressive programming language....
2025-08-12 . 3 min read Swift 6.1
In any programming language, working with strings is essential, and Swift is no different.Whether you are building iOS apps......
2024-10-17 . 3 min read String Concatenation
With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms....
2024-07-09 . 3 min read UIHostingController
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