开发者

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

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

My code is:

NSMutableArray *array1 = [appDelegate getTextList:1];
NSA开发者_开发问答rray *array2 = [appDelegate getTextList:2];
[array1 addObjectsFromArray:array2];//I am getting exception here.

Anyone's help will be much appreciated.

Thanks all, Lakshmi.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜