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 trialChris Stromberg
Courses Plus Student 13,389 PointsWhat 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?
1 Answer
Steven Deutsch
21,046 PointsHey 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.
Good Luck
Chris Stromberg
Courses Plus Student 13,389 PointsChris Stromberg
Courses Plus Student 13,389 PointsSo if we did not provide our own designated initializer, and created this viewController with only the storyboard, we would not have to call
init? (coder aDecoder: NSCoder)
Correct?
Steven Deutsch
21,046 PointsSteven Deutsch
21,046 PointsCorrect. This only occurs when implementing your own designated initializer. You may be doing this to either create an instance of the ViewController using a nib file or just creating an initializer to give values to some added properties. The reason for this is because when you add your own initializer you lose all initializer inheritance from the superclass. Thus, we have to redefine the required aDecoder initializer that UIViewController normally takes care of for us behind the scenes.
Carla Morales
610 PointsCarla Morales
610 PointsBut why does it happen? Because the implementation of the method is the same in this case. So i think that maybe if the initializer change, the initializer init?(coder aDecoder: NSCoder) should change too in some other cases. Would be helpful to explore other examples, but is ok.
Chris Smith
6,791 PointsChris Smith
6,791 PointsHi Steven, I might seem really silly here, but I am desperately trying to understand the following block of code. Do we use the required init? because we want to use the page classes designated initialiser? Could you please explain what that line of code means, along with the super.init(nibName: nil, bundle: nil).
Many thanks.
var page: Page?