Swift – Convenience Initializers

By | 03/05/2023

In this post, we will see what are the Convenience Initializers and how to use them in our Swift projects.
But first of all, what are the Convenience initializers?
In Swift, initializers are used to set up an instance of a class, structure, or enumeration with default values, as well as to allocate resources for that instance.
There are two types of initializers: designated initializer (the standard “init” method) and convenience initializers.
Convenience initializers are secondary initializers that serve to simplify the process of setting up an instance by calling one of the designated initializers with a set of default values or custom parameters. They are prefixed with the convenience keyword and, they are typically used to offer a more user-friendly way of initializing instances.
We can use them for many reasons, like for example default values for certain properties or, like an alternative ways of initializing an instance based on different parameters.

Let’s see some examples:

We open Playground and we create a Class called Person defined as follow:

class Person {
    var firstName: String
    var surName: String
    var age: Int
    var occupation: String
    
    // Designated Initializer
    init(firstName: String, surName: String, age: Int, occupation: String) {
        self.firstName = firstName
        self.surName = surName
        self.age = age
        self.occupation = occupation
    }
}


Then, we add this code to create an instance of Person and for printing the values passed in the init method:

let person1 = Person(firstName: "Damiano", surName: "Abballe", age: 48, occupation: "Software Architect")
print("Result for Designed Init")
print("Name: \(person1.firstName) - SurName: \(person1.surName) - Age: \(person1.age) - Occupation: \(person1.occupation)")


If we run the code, the following will be the result:


Now, we will add a Convenience Initializers without the “Occupation” parameter: the value ‘ND’ it will be set up for default:

// Convenience Initializer
convenience init(firstName: String, surName: String, age: Int) {
        self.init(firstName: firstName, surName: surName, age: age, occupation: "ND")
}

// Convenience Initializer
let person2 = Person(firstName: "Damiano", surName: "Abballe", age: 48)
print("Result for First Convenience Init")
print("Name: \(person2.firstName) - SurName: \(person2.surName) - Age: \(person2.age) - Occupation: \(person2.occupation)")


If we run the code, the following will be the result:


Finally, we will add another Convenience Initializer where there is a parameter called fullName that it is the union of FirstName and SurName separated by a blank:

// Convenience Initializer
convenience init(fullName: String, age: Int) {
        let nameComponents = fullName.split(separator: " ")
        let firstName = String(nameComponents[0])
        let surName = String(nameComponents[1])
        // In this Convenience Init, we call the first Convenience Init because, the Occupation is not a input parameter
        self.init(firstName: firstName, surName: surName, age: age)
}

// Convenience Initializer
let person3 = Person(fullName: "Damiano Abballe", age: 48)
print("Result for Second Convenience Init")
print("Name: \(person3.firstName) - SurName: \(person3.surName) - Age: \(person3.age) - Occupation: \(person3.occupation)")


If we run the code, the following will be the result:



Leave a Reply

Your email address will not be published. Required fields are marked *