NSNotification removeObserver problem

假装没事ソ 提交于 2019-12-05 14:24:32

问题


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?


回答1:


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];


来源:https://stackoverflow.com/questions/5961828/nsnotification-removeobserver-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!