Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNotification removeObserver problem

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?

like image 696
Cyprian Avatar asked May 11 '11 09:05

Cyprian


1 Answers

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];
like image 172
Eimantas Avatar answered Sep 23 '22 19:09

Eimantas