I'm adding values to an array when a user clicks a cell like so:
var savedArray = [] as Array
self.savedArray.append(obj[indexPath.row].title as Any)
This is successfully saving the values. But, when I want to remove them (when they deselect the cell) it says 'Fatal Index out of range'.
let number = indexPath.row
self.savedArray.remove(at: number)
Oddly enough, this code works for the first (0) indexPath, but not the others. Is there a fix? Thank you.
numbervariable is verly likely going beyond the borders of your array... Have you tried printing it ?Any. Use the type of your element. Make yourElement(probably a String) and conform toEquatable(if it doesn't String does conform to Equatable). Then find the firstIndex of your element in your savedArray and remove it.