1

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.

hpique
  • 119,096
  • 131
  • 338
  • 476
  • This SO answer also states that it's not necessary: http://stackoverflow.com/a/34513/143378 – hpique Oct 06 '12 at 08:04

1 Answers1

2

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.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • Doesn't ARC support zeroing weak references now? Seems NSNotificationCenter could be weak-referencing under ARC? – nielsbot Oct 06 '12 at 09:07
  • It does support zeroing weak references, but you still need to remove observations yourself. – Abizern Oct 06 '12 at 09:23
  • Not really. It's in there somewhere. But the reasoning goes: ARC only manages object lifetimes, nothing else. – Abizern Oct 20 '12 at 18:46