Is there a way for creating an array from other severals arrays, in Objective C?
+4
A:
NSArray *arrayC = [arrayA arrayByAddingObjectsFromArray:arrayB];
Ole Begemann
2010-02-14 19:40:16
+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
2010-02-14 20:51:20