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.
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 +=
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With