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 see the i \(i)")
}
I have a program that gets an result array which can also be empty. This is no problem with the for loop above. Now apple wants me to change the code into the following. But this will crash if the array is empty.
var arr = []
for i in 1...arr.count
{
print("i want to see the i \(i)")
}
Do I really have to check the range first before i do a loop like?
var arr = []
if (arr.count >= 1){
for i in 1...arr.count
{
print("I want to see the i \(i)")
}
}
Is there a smarter solution?
The obvious solution in this case would be:
but carefully read through originaluser2's answer
This should produce the same results as your first example, without error...
...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.
If you just want to iterate over a collection, then use the
for <element> in <collection>
syntax.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 range0..<arr.count
.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 range1..<arr.count+1
to iterate over the positions.0..<0
cannot crash for an empty array as0..<0
is just an empty range, and1..<arr.count+1
cannot crash for an empty array as1..<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):Swift 3 syntax:
This is where
startIndex
is the number to start the range at,arr.count
is the number that the range will stay below, and1
is the stride length. If your array has less elements than the given starting index, then the loop will never be entered.