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.

iOS Build an Interactive Story App with Swift 2 Creating the User Interface Programmatically Page Controllers

Chris Stromberg
PLUS
Chris Stromberg
Courses 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?

1 Answer

Steven Deutsch
Steven Deutsch
21,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.

Good Luck

Chris Stromberg
Chris Stromberg
Courses Plus Student 13,389 Points

So 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
Steven Deutsch
21,046 Points

Correct. 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.

But 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.

Hi 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?

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

init(page: Page){
    self.page = page
    super.init(nibName: nil, bundle: nil)
}