In swift, Deinitialization is the process to deallocate or cleanup unused class instance objects to free a memory space occupied by the system resources for better memory management.
Generally, the deinitialization process is called immediately before a class instance is deallocated. By using deinit
keyword we can implement deinitialization process in applications same as initialization (init) process.
In swift, Deinitializers are available only with class types so by using deinitializers we can cleanup unused class instance objects.
Generally, Swift will perform automatic memory management by deallocating or freeing unused object instances by using Automatic Reference Counting (ARC). So we don’t need to perform any manual cleanup to deallocate unused objects. However, when we are building our own custom resources we might need to perform some additional custom cleanup before the class instance is deallocated.
Following is the syntax of defining the deinitialization process using deinit
keyword in swift programing language.
deinit
{
// Perform Deinitialization Process
}
In swift, we don’t need to call deinitializers manually those will call automatically just before the instance of deallocation. If we define a deinitializer in a class it can access all the properties and can modify the behavior of properties. The class object instance will not deallocate until the defined deinitializer is called.
Now we will see how to use deinitializers in class to cleanup instance objects with examples in swift programming language.
Following is the example of defining deinitializer in swift programming language.
var i = 100
class Calculate {
init() {
i = i + 50
}
deinit {
i = 0
}
}
var cins: Calculate? = Calculate()
print("Counter Val: \(i)")
cins = nil
print("Counter Val: \(i)")
If you observe above example we defined a deinitializer using deinit
keyword and created an instance for Calculate class and stored it an optional Calculate variable called cins. The optional variable will help us to track the values of class instance.
When we run the above program in swift playground we will get a result like as shown below.
Counter Val: 150
Counter Val: 0
The statement cins = nil will assign a nil to an optional cins variable that means no instance of Calculate class.
Following is another example of deinitializer. We have two main classes one is Bank and another is Customer. The customer can draw an amount from bank based on his requirement and that will be deducted from his account in a Bank. Here we will use deinitializer deinit method to deduct the amount from the custom account in Customer class.
class Bank {
static var amountInAccount = 20000
static func distribute(Amount amountRequested: Int) -> Int {
let amountToVend = min(amountRequested, amountInAccount)
amountInAccount -= amountToVend
return amountToVend
}
static func receive(Amount: Int) {
amountInAccount += Amount
}
}
class Customer {
var DollerInPurse: Int
init(Amount: Int) {
DollerInPurse = Bank.distribute(Amount: Amount)
}
func win(Amount: Int) {
DollerInPurse += Bank.distribute(Amount: Amount)
}
deinit {
Bank.receive(Amount:DollerInPurse)
}
}
print("Balance in Account: \(Bank.amountInAccount)")
var CustomerOne: Customer? = Customer(Amount: 1000)
print("Requested Amount to Withdraw: \(CustomerOne!.DollerInPurse)")
print("Balance After Withdraw: \(Bank.amountInAccount)")
CustomerOne = nil
print("Balance in Account: \(Bank.amountInAccount)")
If you observe above example we defined a two classes one is Bank and another one is Customer and performing deinitialization using deinit in Customer class.
When we run the above program in swift playground we will get a result like as shown below.
Balance in Account: 20000
Requested Amount to Withdraw: 1000
Balance After Withdraw: 19000
Balance in Account: 20000
The statement CustomerOne = nil will assign a nil to an optional CustomerOne variable that means no instance of Customer class.
This is how we can use deinitializer in a swift programming language to perform some additional cleanup before instance deallocated based on our requirements.