元组可用于在两个(或多个)变量之间交换值而无需使用临时变量。
给定2个变量
var a = "Marty McFly" var b = "Emmett Brown"
我们可以轻松地交换值
(a, b) = (b, a)
结果:
print(a) // "Emmett Brown" print(b) // "Marty McFly"
var a = 0 var b = 1 var c = 2 var d = 3 (a, b, c, d) = (d, c, b, a) print(a, b, c, d) // 3,2,1,0