How to copy end of the Array in swift?

后端 未结 6 1731
感动是毒
感动是毒 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 15:54

    This is one possible solution, there are probably a few more

    let array = [1, 2, 3, 4, 5]
    let endOfArray = Array(array[2..

    Or with dynamic index and range check

    let array = [1, 2, 3, 4, 5]
    let index = 2
    let endOfArray : [Int]
    if index < array.count {
      endOfArray = Array(array[index..

    The re-initializition of the array is needed since the range subscription of Array returns ArraySlice

提交回复
热议问题