How to access not the first elements of an array in Swift?

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

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

+7
arrays swift tail cdr
source share
3 answers

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

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

Use 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] 
+11
source share

The correct answer for Swift 2.2, 3+ :

let restOfArray = Array(array.dropFirst())

Update for Swift 3+ :

like @leanne mentioned in the comments below, it looks like this is now possible:

let restOfArray = array.dropFirst()

+6
source share

Not.

 extension Array { func skip(count:Int)->[T] {return [T](self[count..<self.count])} } 

https://github.com/mythz/swift-linq-examples/blob/master/src/LinqExamples/extensions.swift

+1
source share

All Articles