2010-09-28 85 views
4

我需要追加兩個NSMUtableArray的任何一個可以告訴我怎麼可能?如何在Iphone SDK中附加兩個NSMutableArray或者附加NSArray和NSMutableArray?

我的代碼是:

NSMutableArray *array1 = [appDelegate getTextList:1]; 
NSArray *array2 = [appDelegate getTextList:2]; 
[array1 addObjectsFromArray:array2];//I am getting exception here. 

任何人的幫助將非常感激。

謝謝大家, Lakshmi。

+0

你有什麼異常? – willcodejavaforfood 2010-09-28 08:02:27

+0

這是我得到的異常: – 2010-09-28 08:49:15

+0

2010-09-28 14:18:10.685 VoiceMessenger [3147:307] ***由於未捕獲異常'NSGenericException'而終止應用程序,原因:'*** Collection <__ NSArrayM:0x166090 >突變而被枚舉( 「」, 「」, 「」, 「」, 「 「 」「, 」「, 」「, 」「, 」「, 」 「 )' – 2010-09-28 08:49:44

回答

13

什麼是可能發生的,是你的[的appDelegate getTestList:1]實際上沒有返回NSMutableArray,但NSArray。只是類型轉換數組作爲可變通過保持它的指針一樣,不會在這種情況下工作,而是使用:

NSMutableArray *array1 = [[appDelegate getTextList:1] mutableCopy]; 
NSArray *array2 = [appDelegate getTextList:2]; 
[array1 addObjectsFromArray:array2]; 

或者你可以存儲在您的appDelegate具有作爲一個NSMutableArray在「文本清單」變量第一個地方。我假設你有NSArrayNSArrays(或他們的可變版本)。例如。

// In the class interface 
NSMutableArray *textLists; 

// In the function in which you add lists to the array 
NSMutableArray *newTextList; 
[self populateArray:newTextList]; // Or something like that 

[textLists addObject:newTextList]; 

注:,你可能有不同的工作流程,但我希望你存儲的實際列爲NSMutableArrays的想法。

另注:第二種方法WILL修改的地方NSMutableArray[appDelegate getTextList:1];回報

+0

其實我從Xmlparser獲取列表,我在NSMutableArray中描述它:NSMutableArray * array1 = [appDelegate getTextList:1]; – 2010-09-28 08:51:06

+0

其實我的問題是第一次我會從解析器中獲得10個項目,我將它存儲在NSMutableArray * textArrayList;我將通過控制器使用這個數組。當我點擊加載更多分鐘按鈕時,我發送pagenumber的參數,並再次獲取下十個項目。我希望這下十個項目追加到textArrayList.And顯示所有20個項目。 – 2010-09-28 08:56:10

1

試試這個:

NSMutableArray *result = 
    [[appDelegate getTextList:1] mutableCopy] 
     addObjectsFromArray:[appDelegate getTextList:2]]; 

你得到的異常,因爲你想將變異消息發送到不可變數組。