Is convenience keyword really necessary in swift?

133 Views Asked by At

The convenience keyword in swift completely confused me. It doesn't seem useful at all. Calling other initializer (or say constructor) in same or super class is a very common feature in object oriented languages, like Java, C# and etc. If any member is not initialized, the compiler gives a warning. It seems the only thing it does is to restrict the call to other initializer to same class, which makes it seems even more useless. So, why bother having this keyword at all?

I saw some other threads online discussing about this but none of them is really convincing.

Does anyone know the real purpose of this keyword?

1

There are 1 best solutions below

4
Yakiv Kovalskyi On

From The Swift Programming Language:

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.

So, basically, they are for convenience.