1
    NSMutableArray *no1=[[NSMutableArray alloc]init];
    NSMutableArray *no2=[[NSMutableArray alloc]init];

    for(int i=0;i<3;i++)
    {
        for (int j=0;j<=i;j++)
        {
            NSString *no_str=[NSString stringWithFormat:@"%d",j];
            [no1 addObject:no_str];
        }
        [no2 addObject:no1];
        [no1 removeAllObjects];
    }
    NSLog(@"Final:%@",no2);

我得到如下输出:

决赛:( ( 0, 1, 2 ), ( 0, 1, 2 ), ( 0, 1, 2 ) )

但我需要这样的输出:

决赛:( ( 0 ), ( 0, 1 ), ( 0, 1, 2 ) )

请任何人帮助我。

4

2 回答 2

0

在此处输入图像描述

我假设你使用的是 ARC,所以这段代码会很好用。

NSMutableArray *no1;
NSMutableArray *no2=[[NSMutableArray alloc]init];

for(int i=0;i<3;i++)
{
    no1=[[NSMutableArray alloc]init];
    for (int j=0;j<=i;j++)
    {
        NSString *no_str=[NSString stringWithFormat:@"%d",j];
        [no1 addObject:no_str];
    }
    [no2 addObject:no1];
}
NSLog(@"Final:%@",no2);

编辑:即使你 alloc init the no1 结果也是一样的。

于 2012-11-26T07:45:47.393 回答
0

更新代码:-

NSMutableArray *no1;

    NSMutableArray *no2=[[NSMutableArray alloc]init];

    for(int i=0;i<3;i++)
    {
        no1=[[NSMutableArray alloc]init];
        for (int j=0;j<=i;j++)
        {
            NSString *no_str=[NSString stringWithFormat:@"%d",j];
            [no1 addObject:no_str];
        }
        [no2 addObject:no1];
        [no1 release];
    }
    NSLog(@"Final:%@",no2);
于 2012-11-26T07:42:47.937 回答