tags:

views:

41

answers:

2

Hi Guys,

I need to append two NSMUtableArray's can any one suggest me how it possible?

My code is:

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

Anyone's help will be much appreciated.

Thanks all, Lakshmi.

+1  A: 

What's probably happening, is that your [appDelegate getTestList:1] is not actually returning a NSMutableArray, but a NSArray. Just typecasting the array as mutable by holding a pointer to it like that will not work in that case, instead use:

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

Or you could store the 'textList' variable that you have in your appDelegate as an NSMutableArray in the first place. I am assuming that you have an NSArray of NSArrays (or their mutable versions). Eg.

\\ 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];

Note: that you will probably have a different workflow, but I hope that you get the idea of storing the actual lists as NSMutableArrays.

Another Note: the second method WILL modify in place the NSMutableArray that [appDelegate getTextList:1]; returns

cool_me5000
Actually I am getting the list from Xmlparser and I am stoting it in the NSMutableArray like: NSMutableArray *array1 = [appDelegate getTextList:1];
Laxmi G
Actually My problem is At first time I will get 10 items from the parser and I store it in NSMutableArray *textArrayList; I will use this array through out the controller.when I hit load more resuts buttons I am sending the parameter of pagenumber and again getting next ten more items.I want this next ten items to append to the textArrayList.And display all the 20 items.
Laxmi G
+1  A: 

Try this:

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

You're getting the exception because you're trying to send mutating messages to an immutable array.

NSResponder