Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are convenience required initializers in Swift?

I saw in this answer that the user specifies a convenience required init(). What exactly does this mean?

I understand that the required keyword is used for overriding a superclass's designated initializer, but what does the convenience required declarative do?

like image 553
Doug Smith Avatar asked Nov 14 '14 04:11

Doug Smith


People also ask

What is required Initializers in Swift?

An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer. For example, class Wall { ... // create an initializer init() { // perform initialization ... } }

What must a convenience initializer call?

A convenience initializer is a secondary initializer that must call a designated initializer of the same class. It is useful when you want to provide default values or other custom setup. A class does not require convenience initializers.

What are different types of initializers in Swift?

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

What is the difference between convenience and designated initializer?

A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up to the superclass chain. Convenience initializers are secondary, supporting initializers for a class.


2 Answers

A convenience required initializer is an initializer that is enforced onto all subclasses but is not the designated initializer. This means that said initializer will eventually call a designated initializer in its initialization chain.

Designated Initialisers

A designated initialiser is the canonical initializer for a class and the one which all required and convenience initialisers should call. The Docs say:

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.

Convenience Initialisers

A convenience initialiser is an initializer that sets up certain configuration information on a class...conveniently. Documentation:

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.

You do not have to provide convenience initializers if your class does not require them. Create convenience initializers whenever a shortcut to a common initialization pattern will save time or make initialization of the class clearer in intent

Required Initialisers

Required initializers can be thought of as a binding contract between a parents interface and subsequent subclasses. Its your means of enforcing that all your children are aware of and implement a certain set of initialisers.

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

like image 157
Daniel Galasko Avatar answered Oct 16 '22 06:10

Daniel Galasko


Declaring required initialiser of class as convenience lets subclasses easily inherit it and thus ommit its implementation

protocol P {
    var some: Int! {get}
    init(some: Int)
}

class C: P {
    private(set) var some: Int!
    convenience required init(some: Int) {
        self.init()
        self.some = some
    }
}

class D: C {
    // no need in required init(some: Int)...
}
like image 36
Vladimir Borodko Avatar answered Oct 16 '22 05:10

Vladimir Borodko