How to create an Array from other Array?
Is there a way for creating an array f开发者_Python百科rom other severals arrays, in Objective C?
NSArray *arrayC = [arrayA arrayByAddingObjectsFromArray:arrayB];
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"];
精彩评论