- Mastering macOS Programming
- Stuart Grimshaw
- 110字
- 2021-07-02 22:54:16
Augmented assignment
As in many other C-derived languages, we can replace:
x = x + 1
With:
x += 1
There are versions of this for the other mathematical operators:
-= *= /= %=
Readers should already be familiar with all these operators.
There is no ++ or -- operator in Swift 3. Older versions of the language did, in fact, retain these from the C family of languages, so don't be too surprised if you come across it in older posts on the Web. If you do need to do an i++ or i-- (although for loops make these operators largely unnecessary), use i += 1 or i -= 1.