Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Hey, started looking at Swift today and I got a problem I can't explain. It sees so simple. look at the attached photo below.

When I add the '++' Incrementation post I get this error. But when they are added pre var it is fine?

I did a quick web search for the error messages but nothing that popped out at me. The error explanation that it is not a binary operator, I agree with. It's just supposed to increment the value by 1?

It must be something simple???
 

Attachments

  • for_loop.png
    for_loop.png
    32.5 KB · Views: 180

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
Maybe use ++i instead.

1. Google search terms: swift language operators

2. In top results: The Swift Programming Language: Basic Operators

3. Under the heading "Increment and Decrement Operators":
Unless you need the specific behavior of i++, it is recommended that you use ++i and --i in all cases, because they have the typical expected behavior of modifying i and returning the result.
Given that the language is actively evolving, maybe something that was just a recommendation has now become required. You can look at the Revision History of the linked reference and then search for more current info.

Also see:
https://developer.apple.com/swift/blog/


You didn't say what reference, tutorial, or other source you're learning from, so it's impossible to say whether that material is current or not. Maybe you just need a more current source.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Thanks Chown33...

From the error message it said that it was not a binary operator, which is is not. From my understanding a binary operator needs an operand on both sides of the operator to be binary, 2.
the ++i works fine and the results are as expected. The tutorial is just a youtube video.

Thanks for clearing that up. I will read about it because I am curios why you can't post increment.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Hey, started looking at Swift today and I got a problem I can't explain. It sees so simple. look at the attached photo below.

When I add the '++' Incrementation post I get this error. But when they are added pre var it is fine?

I did a quick web search for the error messages but nothing that popped out at me. The error explanation that it is not a binary operator, I agree with. It's just supposed to increment the value by 1?

It must be something simple???

In Swift, the spaces around operators are significant. A binary operator has either no spaces around it or spaces on both sides. A unary operator has no spaces to the operand, but spaces to the other side. So in your code i++{ the ++ has no spaces on either side, so it taken to be a binary operator. Change it to

Code:
 for i = 0; i < n; i++ {

with a space after the ++. Quite similar, "i< n" or "i <n" won't work, only "i<n" or "i < n".
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
In Swift, the spaces around operators are significant. A binary operator has either no spaces around it or spaces on both sides. A unary operator has no spaces to the operand, but spaces to the other side. So in your code i++{ the ++ has no spaces on either side, so it taken to be a binary operator. Change it to

Code:
 for i = 0; i < n; i++ {

with a space after the ++. Quite similar, "i< n" or "i <n" won't work, only "i<n" or "i < n".

Thanks, for that info.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.