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

iOS Intermediate Swift Object Initialization Designated and Convenience Initializers

Why do we need a convenience initializer to give the name property of vehicle a default value "Unnamed"

Why do we need a convenience initializer to give the name property of vehicle a default value "Unnamed"

Could we not have set up the class as:

class vehicle { let name: String

init(name: String = "Unnamed") { self.name = name } }

carriebarnett
carriebarnett
16,732 Points

I believe the reason is that with a convenience initializer it would allow you to create a vehicle() object with no value.

I found this link on stack overflow that you might find helpful. There is a lot of discussion about this topic here:

https://stackoverflow.com/questions/30896231/why-convenience-keyword-is-even-needed-in-swift