I have an array that contains UIViews (cells) that starts empty. I set it's values when a function is fired by looping through a different array:
cells = [cell0, cell1, cell2, cell3, cell4]
// Code in function ↓
var cellAnimationArray: [NPFCarouselCell] = []
for cell in self.cells {
if cell.frame == farLeftFrame {
cellAnimationArray.insert(cell, at: 0)
} else if cell.frame == leftFrame {
cellAnimationArray.insert(cell, at: 1)
} else if cell.frame == centerFrame {
cellAnimationArray.insert(cell, at: 2)
} else if cell.frame == rightFrame {
cellAnimationArray.insert(cell, at: 3)
} else if cell.frame == farRightFrame {
cellAnimationArray.insert(cell, at: 4)
} else {
print("Frame does not exist!")
print(cell.frame)
return
}
When I get to the line cellAnimationArray.insert(cell, at: 4), the app crashes with :
fatal error: Array index out of range
Why am I getting an out of index error when I am insert an item into the array?