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 trialWing Sun Cheung
4,933 PointsInitializing the viewController class
Hi Team,
Pasan demonstrated the use of the required initialisation method by using:
class viewController: UIViewController { init() {} }
My question is, in our xcode where we actually build our app, howcome we never initialise our viewController class? why is it that classes MUST require a designated initialiser whilst in our viewController class we don't include it?
Thanks!
2 Answers
Vansh Agarwal
Courses Plus Student 3,965 PointsIt doesn't have any stored properties, that's why.
Erik Carlson
Treehouse Project ReviewerThere's nothing that makes a UIVIewController subclass special. It follows the same rules as any other class. The reason you usually don't have to create initializers for it is because you haven't created any stored properties that require initialization. So UIViewController.init is called to initialize all of its stored properties in the absence of your subclass' initializer. If you create stored properties that are Optionals such as IBOutlets, then they can remain nil, so they don't require an initializer. However, if you declare constants without assigning them, such as let someString: String
then you have to create an initializer.