I want to be able to check when my array index goes out of range. The array elements are all strings, so I tried to do something like this but it doesn't work:
if questions[3] != nil {
}
Can someone just show me how to check?
I want to be able to check when my array index goes out of range. The array elements are all strings, so I tried to do something like this but it doesn't work:
if questions[3] != nil {
}
Can someone just show me how to check?
Before indexing into the array, you need to check:
count
is above the intended indexlet intendedIndex: Int = 3
if (intendedIndex >= 0 && questions.count > intendedIndex) {
// This line will not throw index out of range:
let question3 = questions[intendedIndex]
}