Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

429
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!