¿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?>
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 insertartheHours
entempArray
. La declaraciónif
puede verificar sitheHours
no es una cadena vacía antes de insertarla en el array.Por ejemplo, puedes reemplazar esta línea:
con esto:
Este código verifica si
theHours
no es una cadena vacía antes de insertarla entempArray
. SitheHours
está vacío, no lo inserta entempArray
, 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ónif
agregada:“` = []
func setTimeArray() {
let iStart = Int(Double(selectedStart)! * 0.01)
var index = iStart
var tempArray: Array
}
{="" 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
}