Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are ++ and +=1 exactly the same?

I just switch to Swift for couple days and I notice that the postfix and prefix ++ and -- will be removed in Swift 3. I have done some research and according to the announcement on the Swift website, the operator ++ and -- will be replaced by += 1 and -= 1. Link here New Features in Swift 2.2

I have a piece of code that work just fine with the old version of Swift. When I change from return counter1++ which is my original code to return counter1 += 1 and an error pops up saying

No '+=' candidates produce the expected contextual type 'Int'

Here is my example

func countingCounter() -> (() -> Int){     var counter1 = 0     let incrementCounter1: () -> Int = {         return counter1+=1   //original is counter1++     } return incrementCounter1 } 

I have tried to work this out but still stuck.

like image 549
hientrq Avatar asked Apr 26 '16 06:04

hientrq


1 Answers

It clearly states in the Apple docs, copied here for your convenience:

NOTE

The compound assignment operators do not return a value. For example, you cannot write let b = a += 2.

No, the ++ operator is NOT the same as +=.

like image 177
Laffen Avatar answered Sep 22 '22 12:09

Laffen