Introduction
Preparing structure, an instance of a class, or enumeration for use is known as initialization. Setting an initial value for each stored attribute on that instance and any other setup or initialization required before the new instance is available for use is part of this procedure.
Initializers, which are special methods that may be called to produce a new instance of a certain type, are used to accomplish this initialization process. Swift initializers do not return a value, unlike Objective-C initializers. Their main responsibility is to guarantee that new instances of a type are properly initialized before being used for the first time.
Class instances can additionally implement a deinitializer, which performs any custom cleanup shortly before the class instance is deallocated.
Syntax:
Swift uses the init() method to create an initializer. For example,
class Example {
...
// create an initializer
init() {
// perform initialization
...
}
}
Recommended Topic, procedure call in compiler design
Initializer Role for Stored Properties
Before processing the instances, the stored property must first initialize the instances for their classes and structures. The initializer is used to assign and initialize values in stored properties, eliminating the requirement for property observers. A stored property uses an initializer.
- To establish an initial value.
- Within the property definition, set the default property value.
- 'init()' is used to create a specific data type instance. The init() function does not take any arguments.
Setting Property Values by Default
The Init() function in Swift 4 initializes the stored property values. Additionally, while declaring class or structure members, the user has the option of defaulting property values. When a property has the same value throughout the program, rather than initializing it in init, we can declare it in the declaration section (). When inheritance is defined for classes or structures, setting property values by default enables the user.
struct rectangle {
var length = 6
var breadth = 12
}
var peri = rectangle()
print("perimeter of rectangle is \(peri.length*peri.breadth)")
The values are initialized directly in the declaration rather than expressing length and width in init().
Parameter Initialization
The user can use the init function in Swift 4 to initialize parameters as part of the initializer's declaration ().
struct Rectangle {
var length: Double
var breadth: Double
var area: Double
init(fromLength length: Double, fromBreadth breadth: Double) {
self.length = length
self.breadth = breadth
area = length * breadth
}
}
let ar = Rectangle(fromLength: 6, fromBreadth: 12)
print("area is: \(ar.area)")
Local and external variables
Similar to function and method parameters, initialization parameters have local and global parameter names. External parameter declaration is used to invoke the initializer, and local parameter declaration is used to access the initialized body. In contrast to function and method initializers, Swift 4 initializers do not specify which initializer is used to invoke which functions.
Swift 4 adds an automatic external name to each parameter in the init function (). This automatic external name is the same as the local name supplied before each initialization parameter.