Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation of two arrays in Objective-C

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

like image 986
Kiran Avatar asked Nov 11 '10 12:11

Kiran


2 Answers

NSArray* newArray = [firstArray arrayByAddingObjectsFromArray:secondArray]; 

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

[myMutableArray addObjectsFromArray:secondArray]; 
like image 135
Vladimir Avatar answered Sep 23 '22 00:09

Vladimir


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]; 
like image 20
bnabilos Avatar answered Sep 24 '22 00:09

bnabilos