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 Required Initializers

Wing Sun Cheung
Wing Sun Cheung
4,933 Points

Initializing 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

Erik Carlson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Carlson
Treehouse Project Reviewer

There'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.