由于某些原因,尽管进行了所有检查,但此循环给了我一个空数组消息上的索引越界错误。我知道索引不是越界,因为我设置了if语句来检查它,但它仍然崩溃。另外,当我编辑代码以获得显式索引时,例如数组[0] ....应用程序不会崩溃,如果数组为空,则无意义。任何想法可能导致这种情况?ios:非空数组上的索引超出范围错误
至于为什么我用了一段时间,而不是for循环访问数组,所以导致了相同的问题,所以我想明确自己做索引,它仍然会崩溃应用程序。
while(index < self.representatives.count - 1){
if(self.representatives.count > 0){
self.api.getPoliticianContactInfo(self.representatives[8].candidateId!, completion: { (result, id) in
print(result[0].value
print(id)
print("The index is: \(index)")
self.representatives[index].contactInfo = result
self.api.getPoliticianPhoto(id, completion: { (image) in
self.representatives[index].photo = image
print(self.representatives[index].photo)
})
}
index+=1
}
请修改您的问题以在实际问题中包含代码* – Paulw11
该数组是否具有超过8个元素用于此self.representives [8]'不会崩溃? –
它有9个元素,所以放8不会使它崩溃。 –