1 self-increment and self-increment operations2 3Like the C language, Swift also provides the convenience of adding 1 or minus 1 to the variable itself (+ +) and self-subtraction (--) operator. Its operands can be shaped and floating-point. 4var i =05++i//Now i = 16A value of ++i,i is added 1 per call. In fact, ++i is a shorthand for i = i + 1, and i = i = i-shorthand for 1. 7 8+ + and--both pre-and post-op. ++i,i++,--i and i--is a valid notation. 9 Ten It is important to note that these operators have a return value after I have modified I. If you only want to modify the value of I, then you can ignore the return value. But if you want to use the return value, you need to be aware that the return value of the pre-and post-operation is different. OneWhen + +in front of the time, the first self-re-return. AWhen + +Back to the time, the first to return to the self-increment. - - For example: thevar a =0 -Let B = ++a//A and B are now 1 . -Let C = a++//A is now 2, but C is the value of a self-increment before 1 -The above example, let B = + +a adds 1 to the first and returns the value of a. So both A and B are the new value 1. + -and let C = a++, the value of a is returned first, then a is added 1. So C gets the old value of a 1, and a plus 1 turns 2. + AUnless you need to use i++ 's features, it is recommended that you use ++i and-I, because it is more consistent with our logic to return this behavior before modifying it.
Swift Self-increment and self-increment operations