I have a JSON get request where I retrieve information about four users and then load four views with their pictures, names, etc.
I also have a function where the user can tap on those views and view more information about them.
However, I need to make sure that the array (randomFour) containing the four users contains actual information for the view they are selecting and is not nil.
I attempted to do this by counting the indexes of the array, but for some reason it isn't preventing it from going through and then crashes because the values are nil.
@objc func loadWriteView(sender: UITapGestureRecognizer) {
if sender.view == a_view || sender.view == a_pic && randomFour.count >= 1 {
setupCardWrite(user: randomFour[0])
} else if sender.view == b_view || sender.view == b_pic && randomFour.count >= 2 {
setupCardWrite(user: randomFour[1])
} else if sender.view == c_view || sender.view == c_pic && randomFour.count >= 3 {
setupCardWrite(user: randomFour[2])
} else if sender.view == d_view || sender.view == d_pic && randomFour.count >= 4 {
setupCardWrite(user: randomFour[3])
}
}
As you can see, I check that the count is greater than a certain value, but then I still sometimes receive this error:
Thread 1: Fatal error: Index out of range
And when inspecting I see:
randomFour = ([Compliments.User]) 0 values
How can I make sure that the users have been loaded properly?