Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the not-the-first elements of an array in Swift?

Swift's Array has a first function which returns the first element of the array (or nil if the array is empty.)

Is there a built-in function that will return the remainder of the array without the first element?

like image 837
Robert Atkins Avatar asked Nov 18 '14 14:11

Robert Atkins


People also ask

How do you find an array without the first element?

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

How do you get the first element of an array in Swift?

To get the first element of an array in Swift, access first property of this Array. Array. first returns the first element of this array. If the array is empty, then this property returns nil .

How do you access the elements of an array?

Access Array Elements You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

How do you find the first thing in an array?

Alternativly, you can also use the reset() function to get the first element. The reset() function set the internal pointer of an array to its first element and returns the value of the first array element, or FALSE if the array is empty.


1 Answers

There is one that might help you get what you are looking for:

func dropFirst<Seq : Sliceable>(s: Seq) -> Seq.SubSlice

Use like this:

let a = [1, 2, 3, 4, 5, 6, 7, 18, 9, 10]

let b = dropFirst(a) // [2, 3, 4, 5, 6, 7, 18, 9, 10]
like image 162
MirekE Avatar answered Sep 22 '22 08:09

MirekE