I'm trying to initiate the variables at zero, so it currently looks like this
x1,y1,x2,y2=(0,0,0,0)
It works, but just seems a little redundant. Is there a cleaner way?
You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.
When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.
We can assign values to multiple variables at once in a single statement in Swift. We need to wrap the variables inside a bracket and assign the values using the equal sign = . The values are also wrapped inside a bracket.
How do you put multiple variables in a for loop? And you, too, can now declare multiple variables, in a for-loop, as follows: Just separate the multiple variables in the initialization statement with commas.
That is effectively unpacking a tuple. You can do:
x1 = y1 = x2 = y2 = 0
Just don't do this with mutable objects!
I'd usually do
x1 = y1 = x2 = y2 = 0
However, this hardly matters. Both versions are easy to grasp at a single glance.
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