和其他具有元组类型的语言一样,Swift中可以利用元组交换两个变量的值(基于析构原理):
var a = 1, b = 2
(a, b) = (b, a)
// a = 2, b = 1
print(a, b)
如果是3个变量呢?答案也是可以的
var a = 1, b = 2, c = 3
(a, b, c) = (b, c, a)
// a = 2, b = 3, c = 1
print(a, b, c)
和其他具有元组类型的语言一样,Swift中可以利用元组交换两个变量的值(基于析构原理):
var a = 1, b = 2
(a, b) = (b, a)
// a = 2, b = 1
print(a, b)
如果是3个变量呢?答案也是可以的
var a = 1, b = 2, c = 3
(a, b, c) = (b, c, a)
// a = 2, b = 3, c = 1
print(a, b, c)