Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an NSMutableArray to an NSMutableArray Objective-c

Tags:

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?

like image 467
novicePrgrmr Avatar asked Oct 23 '11 17:10

novicePrgrmr


People also ask

How do you declare NSArray in Objective C?

In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method. id objects[] = { someObject, @"Hello, World!", @42 }; NSUInteger count = sizeof(objects) / sizeof(id); NSArray *array = [NSArray arrayWithObjects:objects count:count];

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

What is NSMutableArray Objective C?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .

Is NSMutableArray thread safe?

In general, the collection classes (for example, NSMutableArray , NSMutableDictionary ) are not thread-safe when mutations are concerned. That is, if one or more threads are changing the same array, problems can occur.


2 Answers

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.

like image 118
August Lilleaas Avatar answered Sep 20 '22 19:09

August Lilleaas


Since an array is just an object like any other:

[myContainerMutableArray addObject:someOtherArray]; 

Or if you want to concatenate them:

[myFirstMutableArray addObjectsFromArray:otherArray]; 
like image 30
DarkDust Avatar answered Sep 20 '22 19:09

DarkDust