8

Swift - Initialization - GeeksforGeeks

 1 year ago
source link: https://www.geeksforgeeks.org/swift-initialization/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Swift – Initialization

Practice

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 thattakes 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

  1. The init() is used to create a default value for the stored properties that are present in the class and structures.
  2. The init() is also used to assign the default value for the property.
  3. The init() can
  4. have parameters or it can be created without the parameters.
  5. 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:".")
Output
Geek 404.


Explanations:

  1. In this first we create a class named Geeks and with the data members name and rollno.
  2. 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.
  3. we created an instance for the class named Geeks with the variable named gfg and passed a string and int.
  4. we printed the name and roll no that were assigned by us.
  5. 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 : ".")
Output
Prateek 66.


Explanation:

  1. In the above program, we have first created a class with the name Student and with the data members’ name and roll no.
  2. 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
  3. After that, we created a variable with the name instance of the type Student class.
  4. 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: ".")
Output
Without init function the values  4 5
With Passing the init function the values  2 3.


Explanation:

  1. 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.
  2. In the structure Area there is no init() function initialized we have passed the default values for the member’s length and breadth.
  3. 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.
  4. 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: ".")
Output
The car speed is 223.45 KMPH.
The car name is Ferrari.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated : 10 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments

</div


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK