SubArray selection in swift

59 views Asked by At

I want to do a sub-array selection Python-like (ArraySlice) in Swift.

This is working for me, but I know it's not nice. To make it work I used .suffix embedded to .prefix method. I'm wondering if there's an easier or cleaner method to accomplish this:

let startIndex = 3
let endIndex = 7

let newPoints = Array(points.suffix(points.count - startIndex).prefix(endIndex - startIndex + 1))
1

There are 1 answers

1
burnsi On

You can try Array Slice

Example implementation:

startIndex = 3
endIndex = 7

let slice = points[startindex...endindex]

But you should read the documentation on this. ArraySlice is a look into the array and keeping it alive for a long time is discouraged.