需要澄清addObjectsFromArray函数

时间:2010-07-01 00:25:17

标签: objective-c cocoa cocoa-touch nsmutablearray nsarray

我有以下代码(items和itemsCopy都是NSMutableArray):

//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

1 个答案:

答案 0 :(得分:3)

通过保留相同的数组,您只需填写itemsCopy属性。无论你使用什么方法,它显然都被重置为一个空数组。