Swift problem

Discussion in 'Mac Programming' started by larswik, Jan 2, 2015.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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???
     

    Attached Files:

  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    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.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Did you try with a space? Maybe the { or whole block is being treated as a second operand?
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    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".
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    I didn't realize that. Thanksfortheinfo.
     
  7. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    Thanks, for that info.
     

Share This Page