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 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,863 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