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?
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With