NSNotification removeObserver问题 [英] NSNotification removeObserver problem

查看:91
本文介绍了NSNotification removeObserver问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要么脑子受损了,要么我缺少一些NSNotificationCenter的缺席

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

我得到

*** 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。

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?

推荐答案

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

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

这篇关于NSNotification removeObserver问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆