How to copy end of the Array in swift?

后端 未结 6 1739
感动是毒
感动是毒 2020-11-30 15:20

There must be some really elegant way of copying end of the Array using Swift starting from some index, but I just could not find it, so I ended with this:

f         


        
6条回答
  •  长情又很酷
    2020-11-30 16:16

    And another one ...

    let array = [1, 2, 3, 4, 5]
    let fromIndex = 2
    let endOfArray = array.dropFirst(fromIndex)
    print(endOfArray) // [3, 4, 5]
    

    This gives an ArraySlice which should be good enough for most purposes. If you need a real Array, use

    let endOfArray = Array(array.dropFirst(fromIndex))
    

    An empty array/slice is created if the start index is larger than (or equal to) the element count.

提交回复
热议问题