For Loop Bugs
June 03, 2014
One tiny dot will be the source of many, many off-by-one errors:
“Use .. to make a range that omits its upper value, and use … to make a range that includes both values.”
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.
Updated Feb 25, 2015: Apple has changed the for loop syntax to this much clearer option that will result in far fewer bugs:
for i in 1...5 {
//This will execute for 1, 2, 3, 4, 5
}
for i in 0..<5 {
//This will execute for 0, 1, 2, 3, 4
}
Thank FSM.