Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Value vs Reference Types Final Exam - Solution

nydia subur
nydia subur
1,672 Points

convenience init & designated init

i don't understand why we need to use convenience init. how does it make things easier when the same thing can be done with designated init ?

Moderator Note: Deleted 6 duplicate questions posted in the Community.

1 Answer

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

Hey Nydia,

Convenience initialisers are indeed very similar to designated initialisers in the sense that they perform the same task. However, when creating custom programs we may come upon a very repetitive pattern where a class is initialised to the same values over and over again. For example, imagine we had a class, Fruit, that specified the fruit's amount, color, size, shape, weight, taste, etc. Then, using this class, we created an instance of each of the fruits in your home after going grocery shopping. Suppose most of the fruit you buy are apples. Then, suppose you always bought apples that were red, 10cm in diameter, round, .4 kg, and tasted like apples do; except you didn't always buy the same amount. In this case, it'd be a bit tedious to initialise Fruit objects to the same parameters every time you went shopping:

class Fruit {
       var name: String
       var amount: Int
       var color: String
       var size: Int
       var shape: String
       var weight: Double
       var taste: String

       init (name: String, amount: Int, color: String, size: Int, shape: String, weight: Double, taste: String) {
            self.name = name
            self.amount = amount
            self.color = color
            self.size = size
            self.shape = shape
            self.weight = weight
            self.taste = taste
       }
}

// First buy
var apples1 = Fruit(name: "apple", amount: 5,  color: "red", size: 10, shape: "round", weight: 0.4, taste: "apple")

// Second buy
var apples2 = Fruit(name: "apple", amount: 7, color: "red", size: 10, shape: "round", weight: 0.4, taste: "apple")

// Third buy
var apples3 = Fruit(name: "apple", amount: 3, color: "red", size: 10, shape: "round", weight: 0.4, taste: "apple")

As you can see, this can get quite repetitive. Luckily, we have convenience initialisers that allow us to perform this repetitive task without having to repeat ourselves:

class Fruit {
    var name: String
    var amount: Int
    var color: String
    var size: Int
    var shape: String
    var weight: Double
    var taste: String

    init (name: String, amount: Int, color: String, size: Int, shape: String, weight: Double, taste: String) {
        self.name = name
        self.amount = amount
        self.color = color
        self.size = size
        self.shape = shape
        self.weight = weight
        self.taste = taste
    }

    // This way, you only need to specify the amount, and get over the hurdle of the rest of the parameters.
    convenience init (appleAmount: Int) {
        self.init(name: "apple", amount: appleAmount, color: "red", size: 10, shape: "round", weight: 0.4, taste: "apple"){
        }
    }
}

// First buy
var apples1 = Fruit(appleAmount: 5) // 5 red apples with size 10, shape round, weight 0.4, taste like apples.

// Second buy
var apples2 = Fruit(appleAmount: 7) // 7 red apples with size 10, shape round, weight 0.4, taste like apples.

// Third buy
var apples3 = Fruit(appleAmount: 3) // 3 red apples with size 10, shape round, weight 0.4, taste like apples.

You can read more on this here

Hope this helps, Rodrigo