<__nscfset :0x74957b0>在枚举时发生突变

发布于 2024-12-11 18:31:48 字数 719 浏览 0 评论 0原文

我无法理解为什么我的代码崩溃了:

<__NSCFSet:0x74957b0>枚举时发生突变

我读过类似的主题,但他们的问题是代码在不同的线程中调用。我的代码总是在线程 1 中调用。

它有时会崩溃。

这是发生这种情况的代码:

- (void)processReceivedResponse:(JTResponse *)aResponse {

    NSParameterAssert(aResponse);

    id <JTRequestDelegate> delegate = [self processResponseWithReceiver:aResponse];

    if (delegate == nil) {

        for (JTObserver *someObserver in observers) {

            if (someObserver.requestType == aResponse.type && 
                     ![someObserver.delegate isEqual:delegate]) {

                [someObserver.delegate didReceiveResponse:aResponse];
            }
        }
    }
}

I can't undarstand why my code is crashes:

<__NSCFSet: 0x74957b0> was mutated while being enumerated

Previously I read simular topics but their problem was that the code calls in different threads. My code always calls in Thread 1.

It crashes times to time.

Here is a code where it happens:

- (void)processReceivedResponse:(JTResponse *)aResponse {

    NSParameterAssert(aResponse);

    id <JTRequestDelegate> delegate = [self processResponseWithReceiver:aResponse];

    if (delegate == nil) {

        for (JTObserver *someObserver in observers) {

            if (someObserver.requestType == aResponse.type && 
                     ![someObserver.delegate isEqual:delegate]) {

                [someObserver.delegate didReceiveResponse:aResponse];
            }
        }
    }
}

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

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

发布评论

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

评论(2

我们的影子 2024-12-18 18:31:48

您收到的错误是由于您在循环遍历“观察者”集时更改了“观察者”集而引起的。

仅从您发布的片段中很难判断这可能是什么。您在 someObserver 上调用的委托中是否有某些内容更改了“观察者”集?

The error you're getting is caused by something changing the "observers" set while you're looping through it.

It's hard to tell what that might be from just the snippet you posted. Is something in the delegate you're invoking on someObserver changing the "observers" set?

内心旳酸楚 2024-12-18 18:31:48

您可以尝试简单地复制observers集并循环遍历该副本:

for (JTObserver *someObserver in [[observers copy] autorelease]) {
    ...
}

如果您使用ARC(自动引用计数),则不需要使用autorelease

You could try simply copying the observers set and looping over the copy:

for (JTObserver *someObserver in [[observers copy] autorelease]) {
    ...
}

If you're using ARC (automatic reference counting), you don't need to use autorelease.

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