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
Tyler Amend
2,236 PointsConvenience Initializers
I am having trouble understanding the real difference between a convenience initializer and a designated initializer. I understand that a convenience initializer is more elegant and avoids the having to define the parameters over and over again by calling an initializer of the same class, but does it have any other benefit? I understand the concept of initializers, but the syntax is confusing me. Can anyone give me a baseline explanation? The video clips are a little short on these.
Thanks!
1 Answer
Jhoan Arango
14,575 PointsHello Tyler:
This is something that helped me a lot to understand the difference. And it comes from the iBook “The Swift Programming Language”, I recommend downloading it.
“Designated Initializers and Convenience Initializers
Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain. Classes tend to have very few designated initializers, and it is quite common for a class to have only one. Designated initializers are “funnel” points through which initialization takes place, and through which the initialization process continues up the superclass chain.”
Every class must have at least one designated initializer. In some cases, this requirement is satisfied by inheriting one or more designated initializers from a superclass, as described in Automatic Initializer Inheritance below.”
“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.
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.”
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2 Prerelease).” iBooks. https://itun.es/us/k5SW7.l
Hope this helps, but if you need visual examples on where to use them or not to use them, please let me know, and I can tailor some examples for you.
Good luck
Tyler Amend
2,236 PointsTyler Amend
2,236 PointsJhoan thank you for the thorough answer I really appreciate it. I imagine you would wish to use a convenience initializer in order to avoid a large amount of redundancies in your code?