0

在我的应用程序中,我使用 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

这就是我得到的输出,我不知道它为什么会这样。因此,如果有人对此有任何想法,请告诉我。

4

2 回答 2

0

you dont have to do mutable copy when you are adding the array as an object

[appdelegate.filtersArray addObject:Array1] ;
于 2012-09-21T08:52:54.620 回答
0

只需执行 addObject:Array1 然后

[Array 1 release];

如果您不使用 ARC。

于 2012-09-21T08:52:29.847 回答