Swift – Initialization
Last Updated :
10 Jan, 2024
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
class Geeks
{
var name : String
var roll : Int
init (name : String , roll : Int )
{
self .name = name
self .roll = roll
}
}
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
class Student
{
var name : String
var roll : Int
init ()
{
name = "Prateek"
roll = 66
}
}
var instance = Student()
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
struct Area
{
var length = 4
var breadth = 5
}
struct Area_Rect
{
var length : Int
var breadth : Int
init ()
{
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: "." )
|
Output
Without 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
struct Car
{
var speed : Double
var name : String
init (speed : Double , name : String )
{
self .speed = speed
self .name = name
}
}
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: "." )
|
Output
The car speed is 223.45 KMPH.
The car name is Ferrari.
Share your thoughts in the comments
Please Login to comment...