Search code examples
macoscocoaautomatic-ref-countingnsnotificationcenterweak-references

No longer necessary to remove notification observers in Cocoa?


In this post about Garbage Collection the author states:

in Mac OS X 10.6 and later NSNotificationCenter is weak referenced so you no longer need do to the following in your code

[[NSNotificationCenter defaultCenter] removeObserver:self
      name:kObservationName
      object:nil];

Is this officially documented somewhere? The documentation of removeObserver: still says:

Be sure to invoke this method (or removeObserver:name:object:) before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated.


Solution

  • If you are building a Garbage Collected Application, then it is true that you don't need to unregister an object for receiving observations or notifications.

    But Garbage Collection is deprecated now, and not something you should be using for new projects.

    If you are using ARC or MRC, then you still need to remove objects. And the best place to do that is usually in the dealloc method.