【问题标题】:NSMutableArray not adding objectNSMutableArray 不添加对象
【发布时间】:2013-03-05 01:02:43
【问题描述】:

我一直在尝试获取我的数组计数,但它一直返回 0,我认为它没有正确添加对象。我将我的属性放在头文件中并在 layer.m 文件中合成它。我在我的超级初始化中分配了空间。

NSMutableArray *asteroids;

@property (strong) NSMutableArray *asteroids;

@synthesize asteroids;

self.asteroids = [[NSMutableArray alloc] init];

-(void)findTiles
{
    CGPoint tilecoord1;
    int tileGid1;
    int aheadcount = 200;
    CCSprite *tileonPos[aheadcount];
    int amounts = 0;

    for(int x = 0; x<30; x++)
    {
        for(int y = 0; y<20; y++)
        {
            tilecoord1 = ccp(x+(480*currentlevel),y);
            tileGid1 = [bglayer tileGIDAt:tilecoord1];
            if(tileGid1 == 1)
            {
                tileonPos[amounts] = [bglayer tileAt:ccp(x,y)];
                amounts++;
                [asteroids addObject:tileonPos[amounts]];
                NSLog(@"%d",[asteroids count]);
            }

        }
    }
}

【问题讨论】:

标签: objective-c nsmutablearray


【解决方案1】:

findTiles 运行或根本不运行时,无论您在其中初始化asteroids 的任何方法都不会运行。没有更多信息,就不可能说得更多,但几乎可以肯定是这样。

【讨论】:

    【解决方案2】:

    初始化时调用self.asteroids = [[NSMutableArray alloc] init];,但添加时调用[asteroids addObject:tileonPos[amounts]];

    试试:[self.asteroids addObject:tileonPos[amounts]];[_asteroids addObject:tileonPos[amounts]];

    您还确定self.asteroids = [[NSMutableArray alloc] init]; 在init 中执行吗?

    【讨论】:

      【解决方案3】:

      如果您只打算在 findTiles 方法中将对象添加到“小行星”,请尝试在 for 循环之前在该方法中初始化数组。

      self.asteroids = [@[] mutableCopy];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-09
        • 1970-01-01
        • 2012-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-25
        相关资源
        最近更新 更多