1

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jarly
  • 231
  • 1
  • 4
  • 16
  • Rob, I've explained in my original text why I started this as a new question. None of the existing questions' answers looked convincing to me. If I just add comment to them, I don't think I will get a good answer. So, I decided to ask this as a new question. – jarly Jun 25 '17 at 21:03

1 Answers1

0

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.

Yakiv Kovalskyi
  • 1,737
  • 1
  • 15
  • 29
  • This describes why convenience initializers are optional, but not why the `convenience` *keyword* is required. – Ssswift Jun 23 '17 at 19:00
  • @Ssswift, what do you mean by saying "required"? You're free to avoid them and never use. – Yakiv Kovalskyi Jun 23 '17 at 23:51
  • You're free to avoid using convenience initializers, but if you want to make a convenience initializer, you are required to type the word `convenience` in your source file. No other language requires this. That's what the question is about. The syntax, not the feature. – Ssswift Jun 24 '17 at 00:31
  • Ssswift got my point. If it is optional and the same thing can be achieved without the keyword in other languages, why bother having it at all? I found some syntax in objective-c and swift are really unnecessary and over complicated. – jarly Jun 25 '17 at 20:57