Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Chris StrombergCourses Plus Student 13,389 Points
What exactly are we Initializing here? Why is this required? required init? (coder aDecoder: NSCoder)
I'm trying to follow along with the Build an Interactive Story App. I understand that we must initialize our properties within a class, but what exactly is
init? (coder aDecoder: NSCoder)
doing for us. I get that the storyboard is instantiated from a nib and it needs to be decoded, but why do we have to call this line of code?
Steven Deutsch21,046 Points
Hey Chris Stromberg,
The system saves the state of the application at different points through its use. Your UIViewController conforms to a protocol called NSCoding, which handles the archiving and unarchiving of the ViewController. The NSCoding protocol requires that you implement the required initializer init?(coder aDecoder: NSCoder). This initializer is what allows you to unarchive the ViewController using aDecoder object.
This is most likely occurring because you are providing your own designated initializer inside of your view controller, which causes you to lose initializer inheritance, thus requiring you to redefine the aDecoder required initializer.