Matthew Fowler Matthew Fowler - 7 months ago 12
Swift Question

Replace c style for-loop in Swift 2.2.1

This is the loop in my app:

for var i = column - 1; i >= 0 && burgers[i, row]?.burgerType == burgerType;
i -= 1, horzLength += 1 {

}


What would be the best way to implement this loop in Swift 2.2.1?

Answer

Try this:

var i = column - 1
while i >= 0 && burgers[i, row]?.burgerType == burgerType {
    i -= 1
    horzLength += 1
}

This sort of abuse of the for loop syntax was the exact reason it was deprecated in Swift 2.2. Even if a for syntax was available, this would still be more clear than that abomination