0

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

1
  • do you mean an Array of Arrays or Merging Arrays into one Commented Feb 14, 2010 at 19:43

2 Answers 2

3
NSArray *arrayC = [arrayA arrayByAddingObjectsFromArray:arrayB];
Sign up to request clarification or add additional context in comments.

Comments

2

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"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.