I keep trying to search for the proper way to get a sub array in Swift but I am missing something here. This code doesn't work because rowArray.append(row)
throws an error that states.
Cannot convert value of type 'ArraySlice<Int>' to specified type '[Int]'
I can't figure out how to get an [Int] out of the main array or to convert ArraySlice<Int> to [Int]. I am guessing I am missing something simple but can't seem to find the answer from the docs.
var rowArray = [[Int]]() var rangeStart = 0 let rangeLength = mapWidth for var index = 0; index < mapHeight; ++index{ rangeStart = tileIDs.count - ((index + 1) * mapWidth ) let row : [Int] = tileIDs[rangeStart...rangeStart+rangeLength] rowArray.append(row) } rowArray.append(row)
slice() The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified. Basically, slice lets you select a subarray from an array.
Answer is yes. In C, array is basically a pointer alongside with its length.
subarray() is an inbuilt function in JavaScript which is used to return a part of the typedArray object. Syntax: typedarray.subarray(begin, end) Parameters: It accepts two parameters which are described below: begin: It specifies the index of the starting element from which the part of the given array to be started.
Convert ArraySlice<Int>
to [Int]
with Array()
:
let row : [Int] = Array(tileIDs[rangeStart...rangeStart+rangeLength])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With