由於某些原因,儘管進行了所有檢查,但此循環給了我一個空數組消息上的索引越界錯誤。我知道索引不是越界,因爲我設置了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不會使它崩潰。 –