arrays - iOS Swift : fatal error: Index out of range -
i'm trying remove object @ index i'm getting error. here code:
var arr2 = [3,5,6] let arr3 = arr2 (index, element) in arr3.enumerated() { print("iii \(index)") arr2.remove(at: index) }
logs :
iii 0 iii 1 iii 2 fatal error: index out of range
please let me know i'm doing wrong.
your index
value based on arr3
. remove items arr2
index
doesn't represent proper location in arr2
.
one solution iterate arr3
in reverse.
for (index, element) in arr3.enumerated().reversed() {
this keeps remaining indexes same iteration continues.
wiki
Comments
Post a Comment