views:

171

answers:

2

Is there a way for creating an array from other severals arrays, in Objective C?

+4  A: 
NSArray *arrayC = [arrayA arrayByAddingObjectsFromArray:arrayB];
Ole Begemann
+2  A: 

If you have a whole bunch of arrays, you can do this:

NSMutableArray * allObjects = [NSMutableArray array];
[allObjects addObjectsFromArray: array1];
[allObjects addObjectsFromArray: array2];
[allObjects addObjectsFromArray: array3];

This method prevents creating a new copy of the array each time (which is a side effect of arrayByAddingObjectsFromArray:).

Alternatively, if you have an array of arrays, and you want to combine them into a single array, you can do:

for (NSArray * objects in arrayOfArrays) {
  [allObjects addObjectsFromArray:objects];
}

Or:

allObjects = [arrayOfArrays valueForKeyPath:@"@unionOfArrays.self"];
Dave DeLong