Como remover o item na matriz? [duplicado]

Esta pergunta já tem uma resposta aqui:

Queda de RemoveAtIndex da matriz rápida 5 respostas

Estou codificando comRápidoe confunda com um problema. Eu encontreiErro de índice fora da faixa quando estou tentando remover um item da matriz durante a enumeração da matriz.

Aqui estão meus códigos de erro:

        var array :[Int] = [0,1,2,3,4,5]
        for (index, number) in array.enumerate() {
            if array[index] == 2 {
               array.removeAtIndex(index) // Fatal error: Index out of range
            }
        }

Isso significa que array.enumerate () não é chamado durante cada loop for?

Eu tenho que mudar meus códigos assim:

    for number in array {
       if number == 2 || number == 5 {
          array.removeAtIndex(array.indexOf(number)!)
       }
    }

Ou

var index = 0
repeat {
    if array[index] == 2 || array[index] == 4 {
        array.removeAtIndex(index)
    }
    index += 1

} while(index < array.count)

questionAnswers(1)

yourAnswerToTheQuestion