Objective c NSMutableArray未添加对象

Objective c NSMutableArray未添加对象,objective-c,nsmutablearray,Objective C,Nsmutablearray,我一直试图获取数组计数,但它一直返回0,我认为它没有正确添加对象。我把我的属性放在头文件中,并在layer.m文件中合成它。我在超级初始化中分配了空间 NSMutableArray *asteroids; @property (strong) NSMutableArray *asteroids; @synthesize asteroids; self.asteroids = [[NSMutableArray alloc] init]; -(void)findTiles { CGP

我一直试图获取数组计数,但它一直返回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]);
            }

        }
    }
}
NSMutableArray*小行星;
@属性(强)NSMutableArray*小行星;
@合成小行星;
self.asternos=[[NSMutableArray alloc]init];
-(void)findTiles
{
CGPoint tilecoord1;
int-tileGid1;
int aheadcount=200;
CCSprite*tileonPos[aheadcount];
整数=0;

对于(int x=0;x无论你在其中一个中初始化小行星的方法是什么,当
findTiles
运行或根本不运行时都不会运行。如果没有更多信息,就不可能说得更多,但这几乎可以肯定。无论你在其中一个中初始化小行星的方法是什么,在
findT时都不会运行iles
正在运行或根本没有运行。如果没有更多信息,不可能说得更多,但这几乎可以肯定是正在发生的事情。

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

尝试:
[self.asteroids addObject:tileonPos[amounts]];
[u asteroids addObject:tileonPos[amounts]];


另外,您是否确定
self.asteroids=[[NSMutableArray alloc]init];
在init中执行?

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

尝试:
[self.asteroids addObject:tileonPos[amounts]];
[u asteroids addObject:tileonPos[amounts]];


另外,您是否确定
self.asteroids=[[NSMutableArray alloc]init];
在init中执行?

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

self.asteroids = [@[] mutableCopy];

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

self.asteroids = [@[] mutableCopy];

首先检查此代码是否通过if语句。
tileGid1==1
可能永远不会计算为TRUE(是)。我假设您已将定义和启动分离!可能的重复项,请首先检查此代码是否通过if语句。
tileGid1==1
可能永远不会计算为TRUE(是).我想你已经把定义和启动分开了!可能是,