Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios add array to array in front

Tags:

arrays

ios

iphone

I have an existing array to which i want to add an other array in the front of the existing array.

Add to the end is no problem with

[existingArray addObjectsFromArray:newArray];

But how to add it to the front?

like image 714
user944351 Avatar asked Apr 20 '12 07:04

user944351


People also ask

How do I append one array to another array?

To append one array to another, call the concat() method on the first array, passing it the second array as a parameter, e.g. const arr3 = arr1. concat(arr2) . The concat method will merge the two arrays and will return a new array. Copied!

How do I append two arrays in Swift?

var Array1 = ["Item 1", "Item 2"] var Array2 = ["Thing 1", "Thing 2"] var Array3 = Array1 + Array2 // Array 3 will just be them combined :) Save this answer.

Can we append array?

If you are using List as an array, you can use its append(), insert(), and extend() functions. You can read more about it at Python add to List. If you are using array module, you can use the concatenation using the + operator, append(), insert(), and extend() functions to add elements to the array.


1 Answers

You can do this without a temporary array, and without assuming that newArray is an NSMutableArray, and without making an NSIndexSet:

[existingArray replaceObjectsInRange:NSMakeRange(0,0)
                withObjectsFromArray:newArray];
like image 159
Kurt Revis Avatar answered Sep 23 '22 06:09

Kurt Revis