12

I am either brain damaged or I am lacking of some understending of NSNotificationCenter

The problem is that if I create an observer and in the next line will try to delete it like so:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllVisibleMapViews) name:@"ClearVisibleMaps" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"ClearVisibleMaps"];

I get

*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MyApp 0x592db70> for the key path "ClearVisibleMaps" from <NSNotificationCenter 0x4e0fbb0> because it is not registered as an observer.'

I add and remove observer line after line just to make a point. In my code I will be using remove in the dealloc.

So any ideas why it does tell me that I didn't add and observer in the first place?

Cyprian
  • 9,423
  • 4
  • 39
  • 73

1 Answers1

24

You're removing observer for keypath, not for notification name. The removal should be something like this:

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:@"ClearVisibleMaps"
                                              object:nil];
Eimantas
  • 48,927
  • 17
  • 132
  • 168