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?
我有一个存储NSMutableArray的NSMutableArray。如何向数组添加数组?
5 个解决方案
#1
50
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.
这两个文档都在文档中记录。
#2
3
Since an array is just an object like any other:
因为数组和其他数组一样只是一个对象:
[myContainerMutableArray addObject:someOtherArray];
Or if you want to concatenate them:
或者如果你想把它们连接起来:
[myFirstMutableArray addObjectsFromArray:otherArray];
#3
1
You add it like any other object.
就像添加其他对象一样。
NSMutableArray *innerArray = [NSMutableArray array];
NSMutableArray *outerArray = [NSMutableArray array];
[outerArray addObject:innerArray];
#4
0
[YourArray addObjectsFromArray:OtherArray];
[YourArray addObjectsFromArray OtherArray):;
#5
0
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);
#1
50
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.
这两个文档都在文档中记录。
#2
3
Since an array is just an object like any other:
因为数组和其他数组一样只是一个对象:
[myContainerMutableArray addObject:someOtherArray];
Or if you want to concatenate them:
或者如果你想把它们连接起来:
[myFirstMutableArray addObjectsFromArray:otherArray];
#3
1
You add it like any other object.
就像添加其他对象一样。
NSMutableArray *innerArray = [NSMutableArray array];
NSMutableArray *outerArray = [NSMutableArray array];
[outerArray addObject:innerArray];
#4
0
[YourArray addObjectsFromArray:OtherArray];
[YourArray addObjectsFromArray OtherArray):;
#5
0
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);