Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging NSArrays in Objective-C

I have an NSDictionary where each key points to an array. I later want to merge all of the values into one array. Is there a way to use the API to do something more efficient than say:

NSArray *anArray = [someDictionary allValues]; NSMutableArray *newArray = [NSMutableArray array]; start outter loop on anArray    start inner loop on objects in anArray      add objectAtIndex to newArray 
like image 627
Coocoo4Cocoa Avatar asked Aug 14 '09 19:08

Coocoo4Cocoa


People also ask

How do I override primitive instance methods of an NSArray?

Any subclass of NSArray must override the primitive instance methods count and object (at:). These methods must operate on the backing store that you provide for the elements of the collection. For this backing store you can use a static array, a standard NSArray object, or some other data type or mechanism.

How do I sort an array in NSArray?

NSArray *array1 = [NSArray arrayWithObjects:@"one", @"two", @"three", nil]; NSArray *array2 = @ [@"one", @"two", @"three"]; The most flexible ways to sort an array is with the sortedArrayUsingComparator: method.

What is the difference between NSArray and nsmutablearray?

NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArray. See Toll-Free Bridging for more information on toll-free bridging.

How do I make a subclass of an NSArray?

Remember that NSArray is the public interface for a class cluster and what this entails for your subclass. You must provide the storage for your subclass and implement the primitive methods that directly act on that storage. Before making a custom subclass of NSArray, investigate NSPointerArray and the corresponding Core Foundation type, CFArray.


1 Answers

Just use [newArray addObjectsFromArray:anArray];

like image 147
Ben Gottlieb Avatar answered Sep 20 '22 07:09

Ben Gottlieb