RemoveAtIndex crash from swift array

后端 未结 5 659
半阙折子戏
半阙折子戏 2021-01-25 20:43

I have an array of letters, and want to match the characters against the letters and then do something to that letter (in this case turn it yellow) and then remove that matched

5条回答
  •  粉色の甜心
    2021-01-25 21:36

    For this purpose (removing elements of an array), you may want to use an indexing generator. Let me give an example:

    var array = [1, 2, 3, 4]
    var ig = array.generate()
    var index = 0
    var element = ig.next()
    while element != nil {
        if element == 2 {
            array.removeAtIndex(index)
        } else {
            index = index + 1
        }
        element = ig.next()
    }
    

提交回复
热议问题