Home » Swift Deinitialization

Swift Deinitialization

by Online Tutorials Library

Swift Deinitialization

Swift Deinitialization is a process where ‘deinitializer’ is called to deallocate the memory space before a class instance deallocated. The’deinit’ keyword is used to deallocate the memory spaces occupied by the system resources. Deinitialization is available only on class types.

How Deinitialization Works

Swift automatically deallocates instances which are no longer needed, to free up resources. Swift uses Automatic Reference Counting (ARC) to handle the memory management of Instances. Usually, you don?t need to perform manual cleanup when your instances are deallocated. But, when you work with your own resources, you need to perform some additional cleanup.

For example: If you create a custom class to open a file and write some data to it, you might need to close the file before the class instance is deallocated.

Syntax:

Swift Deinitialization Example

Output:

1  0  

Here, print = nil statement values of the counter retains the same since it is not deinitialized.

Output:

1  1  

You may also like