0

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))
McKinley
  • 1,123
  • 1
  • 8
  • 18
ccmsd18
  • 58
  • 7

1 Answers1

0

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.

burnsi
  • 6,194
  • 13
  • 17
  • 27
  • Oh I was only looking into the Array's properties/methods.. didn't know it had that type as well. That'll work great. Thanks! – ccmsd18 May 11 '23 at 03:48