Swift also supports object-oriented programming language. A class is a blueprint and an object is nothing but it is defined as an instance of a class. A class can be defined in the class using the class keyword in Swift. We can initialize the values for the stored properties or the data members in the particular class. The keyword to create initialization for the data members and properties of a particular class is init(). This article focuses on discussing initialization in Swift.
INIT () For Stored Data Members
The init() responsibility is to initialize the instance of a class that is an object. The initialization in Swift is used for assigning the values for the stored properties. The other feature of Swift is that it provides the deinitializer that takes the functionality of memory management after the deallocation of the particular class instances. The main important thing that the initializer is used for the classes is
- The init() is used to create a default value for the stored properties that are present in the class and structures.
- The init() is also used to assign the default value for the property.
- The init() can
- have parameters or it can be created without the parameters.
- To initialize an instance the init() is used.
Role of INIT() in a Class
Syntax:
class class_name { init() { // initialization of the members }}
Example 1:
Program to initialize the data members by passing the parameters in the init() in a class:
Swift
// Program to initialize the data members
// by passing the parameters in the init()
class Geeks
{
// Members in the class
var name : String
var roll : Int
// Initializer in the class
init(name : String , roll : Int)
{
self.name = name
self.roll = roll
}
}
// Created an instance of type Geeks named gfg
var gfg = Geeks(name : "Geek",roll : 404)
print(gfg.name,gfg.roll,separator:" ",terminator:".")
Explanations:
- In this first we create a class named Geeks and with the data members name and rollno.
- In the class, we create the init() function with the parameters passed so that we can assign the values to the data members in the class.
- we created an instance for the class named Geeks with the variable named gfg and passed a string and int.
- we printed the name and roll no that were assigned by us.
- Another important thing is that we use self which is nothing but the reference of that particular instance.
Example 2:
Program to initialize the data member without passing the parameters in the init:
Swift
// Program to initialize the data member
// without passing the parameters in the init
class Student
{
// Data Members
var name : String
var roll : Int
// No parameters are passed here in the init
init()
{
name = "Prateek"
roll = 66
}
}
// The instance of class student with the name 'instance'
var instance = Student()
// Printing the name and roll for the object named instance
print(instance.name,instance.roll,separator : " ",terminator : ".")
Explanation:
- In the above program, we have first created a class with the name Student and with the data members' name and roll no.
- In this, we initialized the init() with no parameters passed in it and so we have assigned the default values in the init() with the values prateek and 66
- After that, we created a variable with the name instance of the type Student class.
- Now we have printed the data members' values with the help of the created variable of type student.
Role of INIT() in the Structure
Example 1:
Program to initialize the data members in the structure by using the init() along with the parameters and without using the init():
Swift
// Structure Without having the init function
struct Area
{
// DATA MEMBERS by having the default values
// in the structure
var length = 4
var breadth = 5
}
// Another structure by having the init function
struct Area_Rect
{
var length : Int
var breadth : Int
// DATA MEMBERS
init()
{
// Assigning the values with the help of
// the init() function
length = 2
breadth = 3
}
}
var obj1 = Area()
var obj2 = Area_Rect()
print("Without init function the values ", obj1.length,
obj1.breadth, separator:" ", terminator:"\n")
print("With Passing the init function the values ", obj2.length,
obj2.breadth, separator : " ", terminator: ".")
OutputWithout init function the values 4 5
With Passing the init function the values 2 3.
Explanation:
- First, we created two structures in the above program one with the name Area and one with the name Area_Rect both structures contain the members named length and breadth.
- In the structure Area there is no init() function initialized we have passed the default values for the member's length and breadth.
- In the structure Area_Rect we have created an init() function and we have not passed the values in the init() we will assign the values for length and breadth.
- Then we have to print the values of the data members present in both the structures. This program is similar to the above program.
Example 2:
Program to initialize the variables in the structure by passing the parameters into the init function:
Swift
// Program to initialize the variables in the
// structure by passing the parameters into the init function
struct Car
{
// DataMembers of the class
var speed : Double
var name : String
// Assigning the values from the init
init(speed : Double , name : String)
{
self.speed = speed
self.name = name
}
}
// Instance named 'type' of type Car
var type = Car(speed : 223.45,name : "Ferrari")
print("The car speed is \(type.speed) KMPH.",
"The car name is \(type.name)",
separator: "\n", terminator: ".")
OutputThe car speed is 223.45 KMPH.
The car name is Ferrari.
Similar Reads
Zero Initialization in C++ Setting the initial value of an object to zero is called zero initialization. Syntax: static T object; Tt = {} ; T {} ; char array [n] = " "; Zero initialization is performed in the following situations:- Zero is initialized for every named variable with static or thread-local storage duration that
3 min read
Swift - Deinitialization and How its Works? Deinitialization is the process to deallocate memory space that means when we finished our work with any memory then we will remove our pointer that is pointing to that memory so that the memory can be used for other purposes. In other words, it is the cycle to let loose unused space which was invol
4 min read
Swift - Inheritance Inheritance is one of the fundamental concepts of object-oriented programming (OOP) languages. It allows us to create a new class from an existing class and reuse the existing code and functionality. In this article, we will learn about inheritance in Swift and how to use it to create subclasses, ov
15+ min read
Strings in Swift Swift 4 strings have requested an assortment of characters, for example, "Welcome GeeksforGeeks" and they are addressed by the Swift 4 information type String, which thus addresses an assortment of upsides of Character type. Strings in Swift are Unicode right and region obtuse and are intended to be
8 min read
Optional Chaining in Swift In Swift, optional chaining is a process for calling methods, properties, and subscripts on an optional that might currently be nil. If the optional contains a value, the method, property, or subscript is called normally. If the optional is nil, the method, property, or subscript call is ignored and
9 min read