Search Your Question

Swift Initializers

Ans :

Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

Initializers : Initializers, are like special methods that can be called to create a new instance of a particular type.

init() {
// perform some initialization here
}

Customizing Initialization :

struct Human {
    var gender:Gender
    var age:Int = 10
    
    init(age:Int) { // initializer 1
        self.age = age
        self.gender = .unknown
    }
    
    init(age:Int, gender:Gender) { // initializer 2
        self.age = age
        self.gender = gender
    }
}
//------------------------------

let human2 = Human(age: 20)
let human3 = Human(age: 40, gender: .male)

We can have more than one initializers based on our requirement, with different parameter names, types. The swift compiler will decide which init method to call based on the argument label. 

Note that it is not possible to call these initializers without using argument labels. Argument labels must always be used in an initializer if they are defined, and omitting them is a compile-time error:

let human4 = Human() // error :cannot invoke initializer for type ‘Human’ with no arguments
let human5 = Human(40,.male) //error:error: missing argument labels 'age:gender:' in call

Default Initialisers :

Swift provides a default initializer for any structure or class that provides default values for all of its properties and does not provide at least one initializer itself.

class ShoppingListItem {
var nameString?
var quantity = 1
var purchased = false
}
var item = ShoppingListItem()

Memberwise Initializers for Structure Types :

Structure types automatically receive a memberwise initializer if they do not define any of their own custom initializers. 

struct Size {
var width, height :Double // stored properties without default values
}
-------- or --------------
struct Size {
var width = 10.0, height = 30.0 // stored properties with default values
}

In above both Size struct, there is no any init method defined. Still Size struct can be instantiated by 

let twoByTwo = Size(width: 2.0, height: 2.0)

If we provide custom initialisation, then memberwise initializer instantiation will be failed. See following :  

struct Size {
var width, height :Double
init(){
self.width = 10.0
self.height = 30.0
}
}
let sizeObj1 = Size(width: 2.0, height: 2.0)// error. argument passed to call that takes no arguments
let sizeObj2 = Size() // success.

Initializer Delegation for Value Types :

Initializers can call other initializers to perform part of an instance’s initialization. This process, known as initializer delegation.

struct Size {
 var width = 0.0, height = 0.0
}
struct Point {
 var x = 0.0, y = 0.0
}

struct Rect {
 var origin = Point()
 var size = Size()
 init() {}
 init(origin: Point, size: Size) {
   self.origin = origin
   self.size = size
 }

init(center: Point, size: Size) {
  let originX = center.x - (size.width / 2)
  let originY = center.y - (size.height / 2)
  self.init(origin: Point(x: originX, y: originY), size: size)
 }
}

Designated Initializers and Convenience Initializers :

Swift defines two kinds of initializers for class types to help ensure all stored properties receive an initial value.

Designated initializers
Convenience initializers

Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain.
Every class should have at least one designated initializer.

Designated initializers for classes are written in the same way as simple initializers for value types:

Convenience initializers are secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer’s parameters set to default values. You can also define a convenience initializer to create an instance of that class for a specific use case or input value type.

class HumanBeing {
var name: String
init(name: String) {
self.name = name
}
convenience init() {
self.init(name: “not set”)
// Convenience init call the designated init method
}
}
let humanBeingObj1 = HumanBeing() // calls convenience init
let humanBeingObj2 = HumanBeing(name: “abhilash”) // calls designated init


Failable Initializers :

We cannot always assume that the initialization will always succeed for a struct, enum or class. It can fail for several reasons. It is sometimes useful to define a class, structure, or enumeration for which initialization can fail.

You write a failable initializer by placing a question mark after the initkeyword (init?).

You cannot define a failable and a nonfailable initializer with the same parameter types and names.

 In swift, the initializers won’t return anything. But objective -C does. In swift, You write return nil to trigger an initialization failure, you do not use the return keyword to indicate initialization success.

struct Animal {
let species: String
init?(species: String) {
if species.isEmpty { return nil }
self.species = species
}

}

Check :

let someCreature = Animal(species: "Giraffe")
// someCreature is of type Animal?, not Animal
if let giraffe = someCreature {
print("An animal was initialized with a species of \(giraffe.species)")
}

// Prints "An animal was initialized with a species of Giraffe"


Failable Initializers for Enumerations :

You can use a failable initializer to select an appropriate enumeration case based on one or more parameters. The initializer can then fail if the provided parameters do not match an appropriate enumeration case.

enum TemperatureUnit {
case kelvin, celsius, fahrenheit
init?(symbol: Character) {
switch symbol {
case "K":
self = .kelvin
case "C":
self = .celsius
case "F":
self = .fahrenheit
default:
return nil
}
}

guard let fahrenheitUnit = TemperatureUnit(symbol: "X") else {
print("This is not a defined temperature unit, so initialization failed.")
}

// Prints "This is not a defined temperature unit, so initialization failed."

Failable Initializers for Enumerations with Raw Values :

Enumerations with raw values automatically receive a failable initializer, init?(rawValue:), that takes a parameter called rawValue of the appropriate raw-value type and selects a matching enumeration case if one is found, or triggers an initialization failure if no matching value exists.

enum TemperatureUnit: Character {
case kelvin = "K", celsius = "C", fahrenheit = "F"
}
let fahrenheitUnit = TemperatureUnit(rawValue: "F")
if fahrenheitUnit != nil {
print("This is a defined temperature unit, so initialization succeeded.")
}
// Prints "This is a defined temperature unit, so initialization succeeded."


Propagation of Initialization Failure :

If your subclass is having a failable initializer which in turn call its superclass failable designated initializer, then if either of the initialization failed means the entire initialization process fails immediately, and no further initialization code is executed.

class Product {
let name: String
init?(name: String) {
if name.isEmpty { return nil }
self.name = name
}
}
class CartItem: Product {
let quantity: Int
init?(name: String, quantity: Int) {
if quantity < 1 { return nil }
self.quantity = quantity
super.init(name: name)
}
}

if let zeroShirts = CartItem(name: "shirt", quantity: 0) { // fails
print("Item: \(zeroShirts.name), quantity: \(zeroShirts.quantity)")
} else {
print("Unable to initialize zero shirts")
}
// Prints "Unable to initialize zero shirts"

if let oneUnnamed = CartItem(name: "", quantity: 1) { // fails
print("Item: \(oneUnnamed.name), quantity: \(oneUnnamed.quantity)")
} else {
print("Unable to initialize one unnamed product")
}
// Prints "Unable to initialize one unnamed product"


Required Initializers in swift :

Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer.

You must also write the required modifier before every subclass implementation of a required initializer, to indicate that the initializer requirement applies to further subclasses in the chain. You do not write the override modifier when overriding a required designated initializer:

//required init
class classA {
required init() {
var a = 10
print(a)
}
}
class classB: classA {
required init() {
var b = 30
print(b)
}
}
//______________________
let objA = classA()
let objB = classB()
prints:
10
30
10

//______________________

class classC: classA { }
//______________________
let objC = classC() // prints 10 ..superclass init method gets called.


Note: You do not have to provide an explicit implementation of a required initializer if you can satisfy the requirement with an inherited initializer. In the above class classC , since we don’t have any stored properties unintialized at the time of initialization, we dont have to declare the required init explicitly.

No comments:

Post a Comment

Thanks