How to get a subArray from Swift 2.0

…衆ロ難τιáo~ 提交于 2019-11-30 00:57:43

问题


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)

回答1:


Convert ArraySlice<Int> to [Int] with Array():

let row : [Int] = Array(tileIDs[rangeStart...rangeStart+rangeLength])


来源:https://stackoverflow.com/questions/33060238/how-to-get-a-subarray-from-swift-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!