Why can't I use i ++ in for loop in Swift

I know the difference between i++

and ++i

in Swift. As the whitepaper said, it is better to use ++i

for magnification i


But I am wondering why I am getting a syntax error using i++

in a for loop. The code looks like this:

for var i = 0; i < 10; i++{


However, in other cases, you can use either i++

or ++i

. Are there any limits for the loop?


source to share

1 answer

The error says that:

Operator is not a known binary operator

The reason is very simple: you need to add a space between the statement and the opening curly brace:

i++ { 


without this, the compiler takes ++{

as a binary operator with i

and print("hello")

as its arguments

The problem is not related to the prefixed version of the increment operator, because the variable i

makes a clear separation between the operator ++

and the curly brace (letters and numbers cannot be used to define operators).



All Articles