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))

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.