In swift algorithms, how to get the second items of a chunked collection?
let a = ["a", "b", "c", "d", "e"]
let chunked = a.chunks(ofCount: 2) //["a", "b"] ["c", "d"] ["e"]
Now I want to get the ["c", "d"]
I can get the range using
chunked.index(chunked.startIndex, offsetBy: 1) //Index(baseRange: Range(2..<4))
But after that, I don't know how to get the exact result.
Please help!
CodePudding user response:
You've already got the index. Just use the subscript syntax to access the collection:
chunked[chunked.index(chunked.startIndex, offsetBy: 1)]
This will give you an ArraySlice<String>
. Printing this will give you ["c", "d"]
.