如何在Iphone sdk中附加两个NSMutableArray或在NSMrray附加NSMrray?

Lax*_*i G 4 objective-c

我需要追加两个NSMUtableArray可以任何一个建议我怎么可能?

我的代码是:

NSMutableArray *array1 = [appDelegate getTextList:1];
NSArray *array2 = [appDelegate getTextList:2];
[array1 addObjectsFromArray:array2];//I am getting exception here.
Run Code Online (Sandbox Code Playgroud)

任何人的帮助将不胜感激.

谢谢大家,拉克希米.

Sto*_*son 13

可能发生的是,你的[appDelegate getTestList:1]实际上并没有返回a NSMutableArray,而是a NSArray.只需通过按住指针将数组类型化为可变,就像在那种情况下那样不起作用,而是使用:

NSMutableArray *array1 = [[appDelegate getTextList:1] mutableCopy];
NSArray *array2 = [appDelegate getTextList:2];
[array1 addObjectsFromArray:array2];
Run Code Online (Sandbox Code Playgroud)

或者您可以将appDelegate中的'textList'变量存储为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];
Run Code Online (Sandbox Code Playgroud)

注意:您可能会有不同的工作流程,但我希望您能够将实际列表存储为NSMutableArrays.

另注:第二种方法WILL到位的修改NSMutableArray[appDelegate getTextList:1];返回