我的 NSNotificationCenter 有问题。现在它崩溃了,但是几天前,当我添加通知时,它工作正常。在此期间,我添加了一些与此无关的代码。
我有大约 10x10 块。每个图块在创建后立即将其自身添加为观察者:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerJumped) name:@"TestNot" object:nil];
在我的玩家类中,每次跳转结束时,我都会使用以下代码发布一个通知:
if (self.postNotifications == YES) {
//Also post the notification for all the Tiles.
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNot" object:self];
}
如果我在图块中使用 NSLog(),我可以看到大约3 或 4 个图块收到通知。之后,应用程序崩溃并出现 EXC_BAD_ACCESS。它显示 objc_msgSend() 选择器名称:playerJumped
。但我不明白为什么。我发现它与第一个一起工作而不是崩溃。
我这里有什么错误?你能帮帮我吗!
Sandro
编辑:有问题吗,因为大约 100 个对象收到了通知?
I have a problem with NSNotificationCenter. Now it crashes, but a few days ago, when I added the Notification, it worked properly. In the time between I added some code that has nothing to do with that.
I have about 10x10 tiles. Each tile adds itself as an observer as soon as it is created:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerJumped) name:@"TestNot" object:nil];
And in my player class, every time a jump ended, I post a Notification with the following code:
if (self.postNotifications == YES) {
//Also post the notification for all the Tiles.
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNot" object:self];
}
If I use NSLog() in the tiles, I can see that about 3 or 4 tiles receive the notification. And after that, the application crashes with a EXC_BAD_ACCESS. It says objc_msgSend() selector name: playerJumped
. But I don't get why. I see that it works with the first one than it crashes.
What's my error here? Can you please help me!
Sandro
EDIT: Is there a Problem, because the Notification is received by about 100 objects?
发布评论
评论(2)
我自己也有同样的问题。
将其添加到类中解决了问题
Had the same problem myself.
Adding this to the class solved the problem
您的图块对象已被释放,但它仍然在通知中心注册以接收通知。如果这不是您所期望的,请尝试在图块的 -dealloc 方法上添加断点。
Your tile object has been deallocated but it is still registered with the notificationCenter to receive notifications. Try adding a breakpoint on the tile's -dealloc method if this isn't what you are expecting.