开发者

How to create an Array from other Array?

开发者 https://www.devze.com 2022-12-19 23:28 出处:网络
Is there a way for creating an array f开发者_Python百科rom other severals arrays, in Objective C?NSArray *arrayC = [arrayA arrayByAddingObjectsFromArray:arrayB];

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号