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 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
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
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
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
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