I have and array like this:
let array:Array<String> = ["name0","name1","name2","name3","name4","name5","name6"]
And now I wanna get
name0 , name1 , name2
. How to do this?
You can get a slice using array[0 ... 2] or array[0 ..< 3] or array[..<3].
To convert it into a new array, you have to:
Array(array[0 ... 2])
but many operations can be done on the subsequence.
ArraySlice, lose the :Array<String>. If you want to convert to an array, you have to wrap the result in Array(...) as I said in my answer. Also, please use the short type specifiers, e.g. [String] instead of Array<String>.let dataSourceArr: [String] = Array(array[0 ..< 3]) or just let dataSourceArr = Array(array[0 ..< 3])You can use this to get slice of an array :
let values = array[0...2]
Here you will know what else you can do : https://developer.apple.com/documentation/swift/arrayslice
array[0 ... 2].