为什么NSNotificationCenter在发布时会抛出异常?

Jam*_*low 0 iphone cocoa cocoa-touch exception nsnotificationcenter

这不是一个考虑思考的问题 - 为什么NSNotificationCenter在发布时抛出异常?我还是iPhone开发的新手,因此还不知道Cocoa的内部,所以理解为什么会很好.

我将defaultCenter分配给变量,调用addObserver:selector:name:object然后释放上一个变量,但调用[notify release]会使应用程序崩溃.我在代码中没有做任何奇怪的事情,所以找出它为什么这样做会很有趣.

有人遇到过这个问题吗?

Jam*_*uld 5

根据我所知的NSNotifcation,您不应该将defaultcenter分配给变量,而是执行以下操作:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showLogin) name:@"IncorrectLogin" object:nil];
Run Code Online (Sandbox Code Playgroud)

在该片段中,您调用默认中心并为特定邮件注册当前对象.

然后将消息发布到通知中心,您可以使用:

[[NSNotificationCenter defaultCenter] postNotificationName:@"IncorrectLogin" object:nil];
Run Code Online (Sandbox Code Playgroud)

我在我的所有代码中使用上述内容,并且没有任何问题.