Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append two NSMutableArray's in Iphone sdk or append an NSArray With NSMutableArray?

Tags:

objective-c

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.

like image 822
Laxmi G Avatar asked Dec 10 '22 13:12

Laxmi G


1 Answers

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

like image 57
Stone Mason Avatar answered Apr 19 '23 23:04

Stone Mason