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?
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
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 .
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With