奇怪的NSNotificationCenter崩溃

San*_*ier 4 iphone objective-c nsnotificationcenter

嘿伙计们,我还有另外一个问题.这次是使用NSNotificationCenter.现在它崩溃了,但几天前,当我添加通知时,它运行正常.在我之间的时间添加了一些与此无关的代码......

我有大约10x10的瓷砖.每个图块在创建后立即将其自身添加为观察者:

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

在我的播放器类中,每次跳转结束时,我都会使用以下代码发布通知:

if (self.postNotifications == YES) {
    //Also post the notification for all the Tiles.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNot" object:self];
}
Run Code Online (Sandbox Code Playgroud)

如果我在磁贴中使用NSLog(),我可以看到大约3或4个磁贴接收通知.之后,应用程序崩溃了EXC_BAD_ACCESS.它说objc_msgSend() selector name: playerJumped.但我不明白为什么.我发现它适用于第一个而不是崩溃.我的错误是什么?你能帮我么!桑德罗

编辑:是否有问题,因为大约100个对象收到通知?

Tom*_*lbu 10

我自己也有同样的问题.将其添加到课程中解决了问题

- (void) dealloc 
{

  [[NSNotificationCenter defaultCenter] removeObserver:self];

}
Run Code Online (Sandbox Code Playgroud)


hoo*_*oop 9

您的tile对象已被解除分配,但仍在notificationCenter中注册以接收通知.如果这不是您期望的那样,请尝试在tile的-dealloc方法上添加断点.