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.
(var1, var2, ...) = (val1, val2, ...)
var1, var2, ...
: These are the variables we want to assign values to simultaneously.val1, val2, ...
: These are the values we want to assign to the variables.Let's look at the code below:
import Swift// create some variablesvar name = ""var age = 0var role = ""var isMarried = false// assign values at once(name, age, role, isMarried) = ("Theodore", 200, "Software Deeveloper", true)// print valuesprint(name)print(age)print(role)print(isMarried)
In the code above: