在我的应用程序中,我使用for循环在NSMutableArray中添加数据。所有都正确添加,但问题是如果迭代次数增加,则数据在特定索引值处变为零。我的代码如下:
NSArray * sortedArray =[FilterProjectsArray sortedArrayUsingDescriptors:descriptors];
NSMutableArray *Array1=[[NSMutableArray alloc]initWithArray:sortedArray];
[appdelegate.filtersArray addObject:[Array1 mutableCopy]] ;
for (int i=0; i<[appdelegate.filtersArray count]; i++)
{
NSLog(@"object in filtersarray count is %d",[[appdelegate.filtersArray objectAtIndex:i]count]);
}
为此我在控制台中获取输出如下:
object in filtersarray count is 9
object in filtersarray count is 9
object in filtersarray count is 2
object in filtersarray count is 0
object in filtersarray count is 2
object in filtersarray count is 9
object in filtersarray count is 0
object in filtersarray count is 0
object in filtersarray count is 9
object in filtersarray count is 2
object in filtersarray count is 0
object in filtersarray count is 0
object in filtersarray count is 0
object in filtersarray count is 2
object in filtersarray count is 3
object in filtersarray count is 0
object in filtersarray count is 0
object in filtersarray count is 0
object in filtersarray count is 0
object in filtersarray count is 3
这是我得到的输出,我不知道为什么它会像这样。所以如果有人对此有任何想法,请告诉我。
答案 0 :(得分:0)
只需执行addObject:Array1然后
[Array 1 release];
如果您不使用ARC。
答案 1 :(得分:0)
当您将数组添加为对象时,您不必执行可变副本
[appdelegate.filtersArray addObject:Array1] ;