2010-07-01 58 views
0

澄清我有以下代码(这两个项目和itemsCopy是NSMutableArray的公司):需要对addObjectsFromArray功能

//DO: populate items w/ 30 elements 
[self.itemsCopy addObjectsFromArray:self.items]; 
//DO: remove all elements in items 

结果

Begin Pass 1: 
itemsCopy = 0 
items = 30 

End Pass 1: 
itemsCopy = 30 
items = 0 

Begin Pass 2: 
itemsCopy = 0 
items = 30 

End Pass 2: 
itemsCopy = 30 
items = 0 

我怎样才能不断追加项目itemsCopy的结束?我想场景是这样的:

Begin Pass 1: 
itemsCopy = 0 
items = 30 

End Pass 1: 
itemsCopy = 30; 
items = 0; 

Begin Pass 2: 
itemsCopy = 30 
items = 30 

End Pass 2: 
itemsCopy = 60 
items = 0 
+3

某处您必须重新初始化itemsCopy数组,或者每次执行不同对象上的操作时都需要重新初始化.... – ennuikiller 2010-07-01 00:33:07

+1

so addObjectsFromArray should always append? – 2010-07-01 01:43:26

+0

希汉阿拉姆:是的。请参阅文档:http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/addObjectsFromArray:无处它说它会重置阵列,也没有任何理由。 – 2010-07-01 12:05:24

回答

3

通过保持你只是填补了itemsCopy属性相同的数组。显然,无论您使用什么方法,都将重置为空数组。