5

In swift there is the concept of designated initializer (which is the "normal" constructor we know from other languages, I assume).
But there is also something called Convenience Initializer, which I do understand how to write, but the point is lost on me.
As, If I understand correctly, you can achieve the same thing without writing the keyword convenience, or not?

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278

2 Answers2

1

As I understand, the only point in those initializers is convenience. Sometimes it happens that, we often need to create some object with same arguments over and over again. In that case, we can just add another init method which takes much less parameters and the remaining ones are hard coded.

For example, some Logger class

init(type: LoggerType, filepath: String, configurations: LoggerConfig, etc.)

It might be that we often use this logger with same arguments. To avoid duplicating code, we can add a convenience initializer with some default values

convenience init(){ self.init(type: LoggerType.SomeType, filepath: "/log", configurations: LoggerConfig.Default) }

Samurai Girl
  • 978
  • 2
  • 8
  • 13
0

Actually it's very easy to understand them: they are initializers with default parameters.

From the docs:

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.

Example:

class A {
    var a: Int
    var b : Int

    init() {
        a = 0
        b = 0
    }

    /*convenience*/ init(a: Int) {
        self.init()
        self.a = a
    }
}

In the above case, you cannot call self.init(), you have mark your initializer with the convenience keyword, otherwise it will be a compiler error. So you cannot chain two designated initializer from the same class.

Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
  • I get this is what they are used for, just not why I need to add the keyword convenience...I could just create them without it, Like in any other language. – Itay Moav -Malimovka Aug 20 '15 at 18:17
  • But maybe adding the explicit 'convenience' keyword is a good reminder for you to call another initializer. – Dániel Nagy Aug 20 '15 at 18:22
  • @Dániel, You might quote the next paragraph in the docs, too : `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.` – vadian Aug 20 '15 at 18:24
  • but that would work the same without the keyword `convenience` , that is the main thing that bother me, That I suspect I am missing on something here. – Itay Moav -Malimovka Aug 20 '15 at 18:40
  • @ItayMoav-Malimovka I added an example, it shows that you cannot chain designated initializers, so you have to use `convenience` ones in the above case. – Dániel Nagy Aug 20 '15 at 19:27
  • ahhhhhhhhhhhhhhhhhhhhhhh - Thanks – Itay Moav -Malimovka Aug 20 '15 at 19:42