- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift - Deinitialization
Deinitialization is the process of releasing memory resources when an instance of the class is not required. Deinitializers work only with classes, not with the structure or enumeration. It is not necessary that Swift will immediately call a deinitializer when the instance is no longer needed, it will be called before the instance is deallocated and this timing is managed by the automatic reference counting(ARC) system.
A class can have only one deinitializer.
Deinitializers are automatically called developers are not allowed to call them manually
The timing of calling the deinitializer is not explicitly controlled by the developer. It is automatically determined by the ARC.
Circular references can prevent deinitialization.
The deinitializer of the superclass is inherited by their subclasses and the deinitializer of the superclass is called after the implementation of the subclass deinitializer(even if the subclass does not have its own deinitializer).
As we know the instance is not deallocated until after its deinitializer is called, so it means that the deinitialzer can access and modify the instance’s property.
Defining Deinitializer in Class
In Swift, we can define a deinitializer in a class using “deinit” keyword. The code present inside the deinitializer will execute when the instance of the class is deallocated. The Deinitializer does not take any parameter.
Syntax
Following is the syntax of the deinitializer
class className{ // Properties and methods of class // Deinitializer deinit { // statement } }
Example
Swift program to create deinitializer.
// Defining class class Student { // Property of class var name: String // Initializer to initialize instance init(name: String) { self.name = name print("\(name) is initialized") } // Deinitializer to deinitialize instance deinit { print("\(name) is deinitialized") } } // Creating and initializing instances of the student class var object1: Student? = Student(name: "Mira") var object2: Student? = Student(name: "Jenni") // Deinitializers will be called when instances are deallocated object1 = nil object2 = nil
Output
It will produce the following output −
Mira is initialized Jenni is initialized Mira is deinitialized Jenni is deinitialized
Deallocating Memory Space Using Deinitializer
In Swift, deinitialization is handled by the Automatic Reference Counting(ARC). Automatic Reference Counting is a memory management mechanism that is used to keep track of the instance references and deallocate objects when they are no longer needed. Although Swift automatically deallocates memory resources we are not required to perform manual cleaning, but if we are using our resources, then we may have to clean them up manually. For example, if we create a custom class to open a file and write some data to it, we might need to close the file before the class instance is deallocated.
Now, we will see how the deinitializer works −
Step 1 − Create and initialize a new instance of the class using “init” initializer.
Step 2 − Use the instance of the class with strong references. The instance of the class will stay alive as long as there is at least one strong reference to that object.
Step 3 − When the last strong reference to an object is terminated at that time deinitializer will be automatically called by the ARC before the object is deallocated.
Step 4 − Now the code present in the deinitializer is executed and the instance of the given class will be deallocated. The memory is free for other objects.
So this is how deinitializer works.
Example
Swift program to demonstrate how deinitializer works.
var counter = 0; // for reference counting class baseclass { init() { counter++; } deinit { counter--; } } var print: baseclass? = baseclass() print(counter) print = nil print(counter)
Output
It will produce the following output −
1 0
When print = nil statement is omitted the values of the counter remain the same since it is not deinitialized.
Example
var counter = 0; // for reference counting class baseclass { init() { counter++; } deinit { counter--; } } var print: baseclass? = baseclass() print(counter) print(counter)
Output
It will produce the following output −
1 1