I want to ask a question about the objective C. I used to ask how to declare a 2D array in the objective in the past and I get the 2D array. However, I modify some of the code and try to display the content of the array. I found that the input is wrong. Can anyone help me to point out the mistake.
我想问一个关于目标C的问题。我曾经问过如何在过去的目标中声明一个二维数组并得到二维数组。但是,我修改了一些代码并尝试显示数组的内容。我发现输入错了。任何人都可以帮我指出错误。
// Program: convent a 1D array into 2D array and retrieve the element in the array
// example
// dataArray[0]=First Name, [1]=Last Name, [2]=Tom, [3]=chan, [4]=May, ...
// I want to break the array into outerArray, interArray
NSString *temp;
outerDataArray = [[NSMutableArray alloc]init];
innerDataArray = [[NSMutableArray alloc]init];
for(int i=0; i<[dataArray count]; i++)
{
temp = [dataArray objectAtIndex:i];
[innerDataArray addObject:temp];
[temp release];
if(i%[titleArray count] == 0 && i!=0)
{
[innerDataArray release];
innerDataArray = [[NSMutableArray alloc]init];
[outerDataArray addObject:innerDataArray];
}
}
[innerDataArray release];
NSMutableArray *tempArray;
// display
for(int i=0; i<[outerDataArray count]; i++)
{
tempArray = [outerDataArray objectAtIndex:i];
for(int j=0; j<[innerDataArray count]; j++)
{
NSLog(@"%@", [tempArray objectAtIndex:j]);
}
}
2 个解决方案
#1
1
It sems you add to this 'innerDataArray' but then when you reach a count you release it directly before you add it to the outerDataArray:
它是sems你添加到这个'innerDataArray'但是当你到达计数时,你直接释放它,然后再将它添加到outerDataArray:
if(i%[titleArray count] == 0 && i!=0)
{
[innerDataArray release];
innerDataArray = [[NSMutableArray alloc]init];
[outerDataArray addObject:innerDataArray];
}
shouldn't it be?
不应该吗?
if(i%[titleArray count] == 0 && i!=0)
{
[outerDataArray addObject:innerDataArray];
[innerDataArray release];
innerDataArray = [[NSMutableArray alloc]init];
}
#2
1
[temp release];
In the code shown, you do not retain temp
and should not release it.
在显示的代码中,您不保留temp,也不应该释放它。
[outerDataArray addObject:innerDataArray];
You add an empty innerDataArray, so you are probably dropping the first set of entries. Move this line to before innerDataArray is released or add the first innerDataArray before the loop starts.
您添加一个空的innerDataArray,因此您可能正在删除第一组条目。在释放innerDataArray之前将此行移动到或在循环开始之前添加第一个innerDataArray。
#1
1
It sems you add to this 'innerDataArray' but then when you reach a count you release it directly before you add it to the outerDataArray:
它是sems你添加到这个'innerDataArray'但是当你到达计数时,你直接释放它,然后再将它添加到outerDataArray:
if(i%[titleArray count] == 0 && i!=0)
{
[innerDataArray release];
innerDataArray = [[NSMutableArray alloc]init];
[outerDataArray addObject:innerDataArray];
}
shouldn't it be?
不应该吗?
if(i%[titleArray count] == 0 && i!=0)
{
[outerDataArray addObject:innerDataArray];
[innerDataArray release];
innerDataArray = [[NSMutableArray alloc]init];
}
#2
1
[temp release];
In the code shown, you do not retain temp
and should not release it.
在显示的代码中,您不保留temp,也不应该释放它。
[outerDataArray addObject:innerDataArray];
You add an empty innerDataArray, so you are probably dropping the first set of entries. Move this line to before innerDataArray is released or add the first innerDataArray before the loop starts.
您添加一个空的innerDataArray,因此您可能正在删除第一组条目。在释放innerDataArray之前将此行移动到或在循环开始之前添加第一个innerDataArray。