Table of contents
1.
Introduction
2.
Working
3.
Example
3.1.
Output
4.
Frequently Asked Questions
4.1.
When do I need to utilize Deinit?
4.2.
In Swift, how does Deinit work?
4.3.
What is Deinit's name?
5.
Conclusion
Last Updated: Mar 27, 2024

Swift Deintialization

Author Mayank Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

To deallocate memory space before a class instance may be deallocated, the 'deinitializer' function must be used. To deallocate the memory areas held by system resources, use the phrase 'deinit.' Only class types are eligible for deinitialization.

When your instances are no longer needed, Swift 4 automatically deallocates them to free up resources. Automatic reference counting (ARC) is used in Swift 4 to manage memory for instances, as detailed in Automatic Reference Counting. When your instances are deallocated, you usually don't need to undertake any manual cleanup. When working with your resources, you may need to do some further cleanup yourself. If we develop a custom class to open and write data to a file, you may need to close the file before the class instance is deallocated.

When a class instance is no longer needed, the procedure of deinitialization is used. This reduces the amount of memory used by the system. To create a deinitializer, we utilize the deinit keyword.

Working

Swift frees up resources by automatically deallocating instances when they are no longer needed. Swift uses automatic reference counting (ARC) to manage instance memory, as detailed in Automatic Reference Counting. When your instances are deallocated, you usually don't need to undertake any manual cleanup. When working with your resources, you may need to do some further cleanup yourself. If we develop a custom class to open and write data to a file, you may need to close the file before the class instance is deallocated.

Each class declaration can only have one deinitializer. The deinitializer is written without parenthesis and takes no parameters:

deinit {

   // perform the deinitialization

}

Just before instance deallocation, deinitializers are called automatically. You are not permitted to call a deinitializer. Subclasses inherit superclass deinitializers, and the superclass deinitializer is automatically invoked at the end of a subclass deinitializer implementation. Even if a subclass doesn't offer its deinitializer, superclass deinitializers are always called.

A deinitializer can access all attributes of the instance it's called on and adjust its behavior based on those properties because an instance isn't deallocated until after its deinitializer is run.

Example

class Race {
  var laps: Int


  // define initializer
  init() {
    laps = 15
    print("The Race is Completed!!")
    print("Total laps:", laps)
  }


  // define deinitializer
  deinit {
    print("Memory is Deallocated.")
  }
}


// create an instance
var res: Race? = Race()


// deallocate object
res = nil

Output

The Race is Completed!!
Total laps: 15
Memory is Deallocated.

The res is set to nil. This disposes of the instance. Before the class instance is deallocated, the deinitializer is called automatically. The statement within it is then executed.

Frequently Asked Questions

When do I need to utilize Deinit?

Before a class instance is deallocated, a deinitializer is called. Deinitializers are written with the deinit keyword in the same way that initializers are written with the init keyword.

In Swift, how does Deinit work?

Swift Deinitialization is the process of calling 'deinitializer' to deallocate memory before a class instance is deallocated. The'deinit' keyword is used to free up memory that system resources have taken up. Only class types are eligible for deinitialization.

What is Deinit's name?

Deinit will not be triggered if you return nil from a failable initializer, init?(), or throw an error from a throwing initializer, init() throws. Before returning nil or throwing, you must clean up any manually allocated memory or other resources.

Conclusion

Let's point out some of the important things we learned throughout the article. Firstly, Deinitializers are only used in Swift when manually deallocating instances. Otherwise, Swift handles the deallocation for you. Secondly, Deinitializers are only applicable to classes, not structs, and lastly, there can only be one deinitializer per class. That's all from the article. I hope you all like it. 

I hope you all like this article. Want to learn more about Data Analysis? A few excellent courses can guide you in your learning process. You can also refer to our other courses like DSA and Machine Learning.

Happy Learning, Ninjas!

Live masterclass