NSNotification removeObserver问题

我要么是脑部受损,要么是缺乏NSNotificationCenter

问题是,如果我创build一个观察者,并在下一行将尝试删除它像这样:

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

我明白了

 *** 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.' 

我添加和删除观察员线后,只是为了说明一个观点。 在我的代码中,我将在dealloc中使用remove。

那么,为什么它告诉我,我没有添加和观察者在一开始呢?

您正在删除keypath的观察者,而不是通知名称。 删除应该是这样的:

 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ClearVisibleMaps" object:nil];