22

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.

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

Mike Z
  • 4,121
  • 2
  • 31
  • 53
novicePrgrmr
  • 18,647
  • 31
  • 81
  • 103

5 Answers5

57

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.

smonff
  • 3,399
  • 3
  • 36
  • 46
August Lilleaas
  • 54,010
  • 13
  • 102
  • 111
3

Since an array is just an object like any other:

[myContainerMutableArray addObject:someOtherArray];

Or if you want to concatenate them:

[myFirstMutableArray addObjectsFromArray:otherArray];
DarkDust
  • 90,870
  • 19
  • 190
  • 224
1

In case if you add the same NSMutableArray Object, Like

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);
joel prithivi
  • 340
  • 2
  • 13
1

You add it like any other object.

NSMutableArray *innerArray = [NSMutableArray array];
NSMutableArray *outerArray = [NSMutableArray array];
[outerArray addObject:innerArray];
Chuck
  • 234,037
  • 30
  • 302
  • 389
0

[YourArray addObjectsFromArray:OtherArray];

NouNou
  • 185
  • 4
  • 12