Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

426
Visualizações
How to solve Fatal Error: Index out of range error in swift?

In this piece of code, when the iteration position reach 13 out of an array with 20 elements, it crashes with the index out of range error.

let counterHeaders = headers.count
for value in 0..<(counterHeaders){
    if headers[value] == "" {
        headers.remove(at: value)
    }
}

Note: counterHeaders variable was created to check how many values was in headers array

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You just need to iterate your indices in reverse order when removing items in your collection.

for value in headers.indices.reversed() {
    if headers[value] == "" {
        headers.remove(at: value)
    }
}

Note also there is a mutating method that accepts a predicate called removeAll(where:):

headers.removeAll(where: \.isEmpty)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda