奇怪的 NSNotificationCenter 崩溃

发布于 2024-09-25 10:01:17 字数 720 浏览 2 评论 0原文

我的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与风相奔跑 2024-10-02 10:01:17

我自己也有同样的问题。
将其添加到类中解决了问题

- (void) dealloc 
{

  [[NSNotificationCenter defaultCenter] removeObserver:self];

}

Had the same problem myself.
Adding this to the class solved the problem

- (void) dealloc 
{

  [[NSNotificationCenter defaultCenter] removeObserver:self];

}
北斗星光 2024-10-02 10:01:17

您的图块对象已被释放,但它仍然在通知中心注册以接收通知。如果这不是您所期望的,请尝试在图块的 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文