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 Build an Interactive Story App with Swift Personalizing the Story Communicating Via Notifications

What's the importance of de-registering the class from being an observer?

In the video, Pasan wrote the code to set up the UIViewController class as an observer of a certain notification. Then he ran the app and everything worked normally.

Finally, he said that we need to de-register the class from the observer list in NotificationCenter, and he did so in the deinit{} method.

As the app did run normally without this last step, I didn't really get the importance of de-registering the class as an observer.

Why is it important to do this and what are the consequences if this is not done?

1 Answer

David Lin
David Lin
35,864 Points

You no longer need to de-register in iOS9.

In iOS8 and prior, if you didin't de-register in the destructor, the notification center would keep sending notifications to a no-longer existing object, thus crashing the app.

Ref: Unregistering NSNotificationCenter Observers in iOS 9