iOS Swift 4 Coding

How to get a subarray of an array in Swift 4?

2 views August 29, 2018 August 30, 2018 bicobro 0

If you want to get a sub array of an array in Swift 4, you simply use a one-side or two-side range operator.

let myArray = ["A", "B", "C", "D", "E"]
var subArray = myArray[1...3]

// Result: subArray = ["B", "C", "D"]

var subArray = myArray[3...] // one-side range operator
// Result: subArray = ["D", "E"]

var subArray = myArray[...2] // one-side range operator
// Result: subArray = ["A", "B", "C"]
Tags:

Was this helpful?