Concatenation of two arrays in Objective-C

孤者浪人 提交于 2019-12-09 05:00:48

问题


How to concatenate two arrays into a single array in Objective-C?


回答1:


NSArray* newArray = [firstArray arrayByAddingObjectsFromArray:secondArray];

Or using mutable array version just add to it all objects from another array:

[myMutableArray addObjectsFromArray:secondArray];



回答2:


With immutable arrays:

NSArray *arr1 = @[@(1), @(2), @(3)];
NSArray *arr2 = @[@(4), @(5), @(6)];
NSArray *arr3 = [arr1 arrayByAddingObjectsFromArray:arr2];

or adding onto a mutable array:

NSArray *arr1 = @[@(1), @(2), @(3)];
NSArray *arr2 = @[@(4), @(5), @(6)];
NSMutableArray *arr3 = [NSMutableArray arrayWithArray:arr1];

[arr3 addObjectsFromArray:arr2];



回答3:


NSSArray *theArrayIReallyWant = [oneArrayIDontReallyWant arrayByAddingObjectsFromArray:otherArrayIDontReallyWant];

If you need to, retain theArrayIReallyWant so it stays in memory, just be sure to release it when you are done. That by far is the easiest method :)



来源:https://stackoverflow.com/questions/4154502/concatenation-of-two-arrays-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!