I\'m encountering a change in the swift code which i do not quite understand.
var arr = []
for var i = 1; i <= arr.count; i += 1
{
print(\"i want to
If you just want to iterate over a collection, then use the for <element> in <collection>
syntax.
for element in arr {
// do something with element
}
If you also need access to the index of the element at each iteration, you can use enumerate(). Because indices are zero based, the index will have the range 0..<arr.count
.
for (index, element) in arr.enumerate() {
// do something with index & element
// if you need the position of the element (1st, 2nd 3rd etc), then do index+1
let position = index+1
}
You can always add one to the index at each iteration in order to access the position (to get a range of 1..<arr.count+1
).
If none of these solve your problem, then you can use the range 0..<arr.count
to iterate over the indices of your array, or as @vacawama says, you could use the range 1..<arr.count+1
to iterate over the positions.
for index in 0..<arr.count {
// do something with index
}
for position in 1..<arr.count+1 {
// do something with position
}
0..<0
cannot crash for an empty array as 0..<0
is just an empty range, and 1..<arr.count+1
cannot crash for an empty array as 1..<1
is also an empty range.
Also see @vacawama's comment below about using stride
for safely doing more custom ranges. For example (Swift 2 syntax):
let startIndex = 4
for i in startIndex.stride(to: arr.count, by: 1) {
// i = 4, 5, 6, 7 .. arr.count-1
}
Swift 3 syntax:
for i in stride(from: 4, to: arr.count, by: 1) {
// i = 4, 5, 6, 7 .. arr.count-1
}
This is where startIndex
is the number to start the range at, arr.count
is the number that the range will stay below, and 1
is the stride length. If your array has less elements than the given starting index, then the loop will never be entered.
This should produce the same results as your first example, without error...
var arr = []
var i=1
for _ in arr
{
print("i want to see the i \(i)")
i += 1
}
...although that just seems like a complicated way to count the elements in an array (arr.count) so I suspect that there is more to this question than meets the eye.
The obvious solution in this case would be:
var arr = []
for i in arr.indices {
print("I want to see the i \(i)") // 0 ... count - 1
print("I want to see the i \(i + 1)") // 1 ... count
}
but carefully read through originaluser2's answer