es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

¿Por qué está fuera de rango el índice de mi array al usar una declaración “if”?

Este es el código:

func setTimeArray() {
    let iStart = Int(Double(selectedStart)! * 0.01)
    var index = iStart
    var tempArray: Array<string> = []

    print("count is ", count)
    for i in 0..<self.count  {
        var thehours = ""
        if (index == 24) {
            index = 0
        }  else if (index == 23) {
            thehours = self.parse24(thestring: string(index)) + " to " + self.parse24(thestring: "0")
        } else {
            thehours = self.parse24(thestring: string(index)) + " to " + self.parse24(thestring: string(index + 1))
        }
        if !thehours.isempty {
            temparray.insert(thehours, at: i)
        }
        index = index + 1
    }
    self.timearray = temparray
}

este código funciona muy bien, pero necesito envolver el lugar donde se inserta en la temparray para que no agregue una cadena vacía. desafortunadamente, cuando intento agregar una declaración if o colocar temparray.insert(thehours, at: i) dentro de las declaraciones if existentes, recibo el error: “swift/array.swift:405: fatal error: array index is out of range”.

quiero decir, ¡en realidad estoy agregando más elementos sin la declaración if! ¿alguien puede decirme cómo solucionar esto? {=”” var=”” thehours=”” if=”” (index=”=” 24)=”” {=”” index=”0″ }=”” else=”” if=”” (index=”=” 23)=”” {=”” thehours=”self.parse24(theString:” string(index))=”” +=”” to=”” +=”” self.parse24(thestring:=”” “0”)=”” }=”” else=”” {=”” thehours=”self.parse24(theString:” string(index))=”” +=”” to=”” +=”” self.parse24(thestring:=”” string(index=”” +=”” 1))=”” }=”” if=”” !thehours.isempty=”” {=”” temparray.insert(thehours,=”” at:=”” i)=”” }=”” index=”index” +=”” 1=”” }=”” self.timearray=”tempArray” }=”” ="" este="" código="" funciona="" muy="" bien,="" pero="" necesito="" envolver="" el="" lugar="" donde="" se="" inserta="" en="" la="" `temparray`="" para="" que="" no="" agregue="" una="" cadena="" vacía.="" desafortunadamente,="" cuando="" intento="" agregar="" una="" declaración="" `if`="" o="" colocar="" `temparray.insert(thehours,="" at:="" i)`="" dentro="" de="" las="" declaraciones="" `if`="" existentes,="" recibo="" el="" error:="" "swift/array.swift:405:="" fatal="" error:="" array="" index="" is="" out="" of="" range".="" quiero="" decir,="" ¡en="" realidad="" estoy="" agregando="" más="" elementos="" sin="" la="" declaración="" `if`!="" ¿alguien="" puede="" decirme="" cómo="" solucionar=""></self.count {
var thehours = ""
if (index == 24) {
index = 0
} else if (index == 23) {
thehours = self.parse24(thestring: string(index)) + " to " + self.parse24(thestring: "0")
} else {
thehours = self.parse24(thestring: string(index)) + " to " + self.parse24(thestring: string(index + 1))
}
if !thehours.isempty {
temparray.insert(thehours, at: i)
}
index = index + 1
}
self.timearray = temparray
}

este código funciona muy bien, pero necesito envolver el lugar donde se inserta en la temparray para que no agregue una cadena vacía. desafortunadamente, cuando intento agregar una declaración if o colocar temparray.insert(thehours, at: i) dentro de las declaraciones if existentes, recibo el error: “swift/array.swift:405: fatal error: array index is out of range”.

quiero decir, ¡en realidad estoy agregando más elementos sin la declaración if! ¿alguien puede decirme cómo solucionar esto?>

Tags:  , ,

Answer

  1. Avatar for davy.ai

    Parece que el error “Swift/Array.swift:405: Fatal error: el índice del array está fuera de rango” se debe al intento de insertar elementos en un índice que está fuera del rango del array.

    Para evitar este error, puedes agregar una declaración if antes de insertar theHours en tempArray. La declaración if puede verificar si theHours no es una cadena vacía antes de insertarla en el array.

    Por ejemplo, puedes reemplazar esta línea:

    tempArray.insert(theHours, at: i)
    

    con esto:

    if !theHours.isEmpty {
        tempArray.insert(theHours, at: i)
    }
    

    Este código verifica si theHours no es una cadena vacía antes de insertarla en tempArray. Si theHours está vacío, no lo inserta en tempArray, lo que debería evitar el error “El índice del array está fuera de rango”.

    Puedes agregar este código dentro del bucle for, justo antes de la línea “index = index + 1”.

    Aquí está el método setTimeArray modificado con la declaración if agregada:

    “`
    func setTimeArray() {
    let iStart = Int(Double(selectedStart)! * 0.01)
    var index = iStart
    var tempArray: Array = []

    print("count is ", count)
    for i in 0..<self.count  {
        var thehours = ""
        if (index == 24) {
           index = 0
        }  else if (index == 23) {
            thehours = self.parse24(thestring: string(index)) + " a " + self.parse24(thestring: "0")
        } else {
            thehours = self.parse24(thestring: string(index)) + " a " + self.parse24(thestring: string(index + 1))
        }
        if !thehours.isempty {
            temparray.insert(thehours, at: i)
        }
        index = index + 1
    }
    self.timearray = temparray
    

    }
    {="" var="" thehours="" if="" (index="=" 24)="" {="" index="0" }="" else="" if="" (index="=" 23)="" {="" thehours="self.parse24(theString:" string(index))="" +="" a="" +="" self.parse24(thestring:="" "0")="" }="" else="" {="" thehours="self.parse24(theString:" string(index))="" +="" a="" +="" self.parse24(thestring:="" string(index="" +="" 1))="" }="" if="" !thehours.isempty="" {="" temparray.insert(thehours,="" at:="" i)="" }="" index="index" +="" 1="" }="" self.timearray="tempArray" }=""></self.count {
    var thehours = ""
    if (index == 24) {
    index = 0
    } else if (index == 23) {
    thehours = self.parse24(thestring: string(index)) + " a " + self.parse24(thestring: "0")
    } else {
    thehours = self.parse24(thestring: string(index)) + " a " + self.parse24(thestring: string(index + 1))
    }
    if !thehours.isempty {
    temparray.insert(thehours, at: i)
    }
    index = index + 1
    }
    self.timearray = temparray
    }
    >

Comments are closed.