25. Swift – Initialization
Swift
Classes, structures and enumerations once declared in Swift are initialized for preparing instance of a class. Initial value is initialized for stored property and also for new instances too the values are initialized to proceed further. The keyword to create initialization function is carried out by 'init()' method. Swift initializer differs from Objective-C that it does not return any values. Its function is to check for initialization of newly created instances before its processing. Swift also provides 'deinitialization' process for performing memory management operations once the instances are deallocated.
Initializer Role for Stored Properties Stored property have to initialize the instances for its classes and structures before processing the instances. Stored properties use initializer to assign and initialize values thereby eradicating the need to call property observers. Initializer is used in stored property
To create an initial value.
To assign default property value within the property definition.
To initialize an instance for a particular data type 'init()' is used. No arguments are passed inside the init() function.
Syntax init() { //New Instance initialization goes here }
Example struct rectangle { var length: Double var breadth: Double init() { length = 6 breadth = 12 } } var area = rectangle() 135