I have below code.
var myArray : [String] = ["1", "2", "3", "4", "5"]
for var i in 0..<myArray.count {
print("i==\(i)==\(myArray[i])")
myArray.remove(at: i)
i = i-1
}
Log is as below.
i==0==1
i==1==3
i==2==5
fatal error: Index out of range
Log I was expecting is
i==0==1
i==0==2
i==0==3
i==0==4
i==0==5
I am saying this because when I set i=i-1
in the for loop, i
will be always 0.
Which means variable i
is not having effect of code i=i-1
Any idea why this is happening?
Same code I have in java, and it works fine.
ArrayList<String> mm = new ArrayList<>();
mm.add("1");
mm.add("2");
mm.add("3");
mm.add("4");
mm.add("5");
for(int i=0; i<mm.size(); i++){
Log.d("mm", "i=="+i+"=="+mm.get(i));
mm.remove(i);
i = i-1;
}
Log I have is as below
i==0==1
i==0==2
i==0==3
i==0==4
i==0==5