将对象添加到 Objective-C NSMutableArray 会让我摆脱“For”的困境循环另一个数组

发布于 2024-12-24 04:04:25 字数 1593 浏览 3 评论 0原文

我试图在第二个数组的 For 循环内部使用第二个 NSMutableArray 中的对象填充一个 NSMutableArray 。当我将一个对象添加到第一个数组时,我被踢出了循环。这发生在 COCOS2D 应用程序中的“计划”回调内部。

奇怪的是,它在一种情况下工作正常,但在另一种情况下却不起作用。下面的代码示例中记录了它第一次工作的情况。第二次也在代码中注明,相关行用 5 个 * 表示。唯一根本上不同的是,第二个示例在 While 循环内部反复发生,不断填充和清空第二个 NSMutableArray。

显然发生了一些我不明白的事情。我希望有人能够抓住它。

预先感谢您的帮助!

以下代码是该方法的相关部分:

NSMutableArray *gameSprites = [[NSMutableArray alloc] init];
NSMutableArray *guns = [[NSMutableArray alloc] init];
NSMutableArray *newGuns = [[NSMutableArray alloc] init];

// START OF THE FIRST EXAMPLE 

// collect sprites that are guns
for (LPSprite *sprite in gameSprites)
{
    if ([sprite isGun])
    {
        [guns addObject:gun];
    }
}

// END OF THE FIRST EXAMPLE


// do work for all guns in gamesprite collection
int numGuns = [guns count];

while (numGuns > 0)
{

    // START OF THE SECOND EXAMPLE

    for (LPGun *gun in guns)
    {

        if (special condition that only occurs sometimes)
        {       
            LPGun *portalGun = [[LPGun alloc] init];

            // ***** When this line is executed, I get 'kicked' out of the method and execution starts again at the beginning of the method. No exception is thrown or noticed.
            [newGuns addObject:portalGun];
        }
    }

    numGuns = [newGuns count];

    // empty gun collection and add any 'new' guns to the collection so the above work can be repeated
    [guns removeAllObjects];

    for (LPGun *g in newGuns)
    {
        [guns addObject:g];
    }

    [newGuns removeAllObjects];

    // END OF THE SECOND EXAMPLE
}

I am attempting to populate one NSMutableArray with objects from a second NSMutableArray while inside of a For loop for the second array. When I add an object to the first array I am kicked out of the loop. This is occurring inside of a 'schedule' callback in a COCOS2D application.

The weird thing is it works fine in one case but not in another. The first time it works is noted below in the code sample. The second time is also noted in code, with the relevant line denoted with 5 *'s. The only thing that is fundamentally different is that the second example happens over and over inside of a While loop, constantly filling and emptying the second NSMutableArray.

Something is clearly happening that I just don't understand. I'm hoping that someone will be able to catch it.

Thanks in advance for the help!

The following code is the relevant part of the method:

NSMutableArray *gameSprites = [[NSMutableArray alloc] init];
NSMutableArray *guns = [[NSMutableArray alloc] init];
NSMutableArray *newGuns = [[NSMutableArray alloc] init];

// START OF THE FIRST EXAMPLE 

// collect sprites that are guns
for (LPSprite *sprite in gameSprites)
{
    if ([sprite isGun])
    {
        [guns addObject:gun];
    }
}

// END OF THE FIRST EXAMPLE


// do work for all guns in gamesprite collection
int numGuns = [guns count];

while (numGuns > 0)
{

    // START OF THE SECOND EXAMPLE

    for (LPGun *gun in guns)
    {

        if (special condition that only occurs sometimes)
        {       
            LPGun *portalGun = [[LPGun alloc] init];

            // ***** When this line is executed, I get 'kicked' out of the method and execution starts again at the beginning of the method. No exception is thrown or noticed.
            [newGuns addObject:portalGun];
        }
    }

    numGuns = [newGuns count];

    // empty gun collection and add any 'new' guns to the collection so the above work can be repeated
    [guns removeAllObjects];

    for (LPGun *g in newGuns)
    {
        [guns addObject:g];
    }

    [newGuns removeAllObjects];

    // END OF THE SECOND EXAMPLE
}

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

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

发布评论

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

评论(2

神仙妹妹 2024-12-31 04:04:25

1/ 按照惯例,最好使用最长的 init... 方法。尝试使用 initWithCapacity: 而不是 NSMutableArray 上的基本 init 方法。
2/ 此外,将对象添加到 NSMutableArray 后,您必须释放它,让该数组成为唯一的所有者。

1/ By convention it is better to use the longest init... method. Try to use initWithCapacity: instead of basic init method on NSMutableArray.
2/ Also after adding your object to the NSMutableArray you must release it to let the array to be the sole owner.

蓬勃野心 2024-12-31 04:04:25

正如我上面的评论之一所提到的:

添加一些日志记录有帮助。它迫使我查看控制台日志,该日志实际上抛出了一个错误(或者可能是警告),导致对象无法添加到数组中。添加上方的 init 行失败,因为找不到纹理。

所以我误解了这些症状,因此这里的主题可能会对将来访问它的人产生一些误导。

As mentioned in one of my comments above:

Adding some logging helped. It forced me to look at the console log which was actually spewing out an error (or maybe a warning) that was preventing the object from being added to the array. The init line just above the add was failing because a texture could not be found.

So I misinterpreted the symptoms, and as a result the topic here may be a bit misleading to people visiting it in the future.

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