ios 如何将 NSMutableArray 添加到 NSMutableArray Objective-c

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7868015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:01:18  来源:igfitidea点击:

How to add an NSMutableArray to an NSMutableArray Objective-c

iosobjective-cxcodensmutablearraynsarray

提问by novicePrgrmr

I am making the switch from Java to Objective-c, and I'm having some difficulty. I have searched this problem this without much success.

我正在从 Java 切换到 Objective-c,但遇到了一些困难。我已经搜索了这个问题,但没有取得多大成功。

I have an NSMutableArray that stores NSMutableArrays. How do I add an array to the array?

我有一个存储 NSMutableArrays 的 NSMutableArray。如何将数组添加到数组中?

回答by August Lilleaas

You can either store a reference to another array (or any type of object) in your array:

您可以在数组中存储对另一个数组(或任何类型的对象)的引用:

[myArray addObject:otherArray];

Or concatenate the arrays.

或者连接数组。

[myArray addObjectsFromArray:otherArray];

Both of which are documented in the documentation.

这两者都记录在文档中

回答by DarkDust

Since an array is just an object like any other:

由于数组只是一个像任何其他对象一样的对象:

[myContainerMutableArray addObject:someOtherArray];

Or if you want to concatenatethem:

或者,如果您想连接它们:

[myFirstMutableArray addObjectsFromArray:otherArray];

回答by Chuck

You add it like any other object.

您可以像添加任何其他对象一样添加它。

NSMutableArray *innerArray = [NSMutableArray array];
NSMutableArray *outerArray = [NSMutableArray array];
[outerArray addObject:innerArray];

回答by NouNou

[YourArray addObjectsFromArray:OtherArray];

[YourArray addObjectsFromArray:OtherArray];

回答by joel prithivi

In case if you add the same NSMutableArray Object, Like

如果您添加相同的 NSMutableArray 对象,例如

NSMutableArray *mutableArray1 = [[NSMutableArray alloc]initWithObjects:@"test1",@"test2",@"test3",nil];

NSMutableArray *mutableArray2 = [[NSMutableArray alloc]initWithObjects:@"test4",@"test5",@"test6", nil];

mutableArray1 = [NSMutableArray arrayWithArray:mutableArray1];

[mutableArray1 addObjectsFromArray:mutableArray2]; 

Nslog(@"mutableArray1 : %@",mutableArray1);