In swift, I want to compare two different indexes in the same array. Right now, My code is something like:
var myArray:[String] = ["1" , "1", "2"]
for i in myArray{
if(myArray[i] == myArray[i + 1]){
// do something
}
}
From this, I get an error:
Cannot convert value of type 'String' to expected argument type 'Int'
How do I go about fixing this?
>Solution :
For-each construction (for i in array) does not provide you with an index, it takes elements from a sequence.
You may want to use ranges like this to aquire indices:
for i in 0 ..< array.count