Defined
Tuples are composed of several types of data, the data that makes up a tuple is called an element, and the type of each element can be arbitrary.
Usage One
Let Tuples1 = ("Hello", "World", 2017)
Tuples are the same as arrays, and the corners of their elements can be tuple1.0 tuple1.1 tuple1.2 starting from 0.
Print ("Get first value \ (tuples1.0), get the second value \ (tuples1.1), get the third value \ (tuples1.2)")
Usage Two
Let Tuples2 = (first: "Hello", Second: "World", third:2017)
The values of the tuples used in this use can be Tuple2.first tuple2.second Tuple3.third
Print ("Get the first value \ (Tuples2.first), get the second value \ (Tuples2.second), get the third value \ (Tuples2.third)" )
Usage Three
Variable receiving numerical method
Let (x, Y, Z) = ("Hello", "World", 2017);
Print ("Get X is \ (x) get y are \ (y) get Z is \ (z)")
Use _ to remove the value of some elements ignore _ corresponding value
Let (A, _, B) = ("Hello", "World", 2017);
Print ("Get X is \ (a) Get z-is \ (b)")
Tuples Worth modifying
Let Tuples2 = (first: "Hello", Second: "World", third:2017)
The values of the tuples used in this use can be Tuple2.first tuple2.second Tuple3.third
Print ("Get the first value \ (Tuples2.first), get the second value \ (Tuples2.second), get the third value \ (Tuples2.third)" )
This will cause an error
Tuples2.first = "Test"; The let before tuple2 should be changed to Var to make tuples into variable tuples
var tuples2 = (First: "Hello", Second: "World", third:2017)
The values of the tuples used in this use can be Tuple2.first tuple2.second Tuple3.third
var print ("Get the first value \ (Tuples2.first), get the second value \ (Tuples2.second), get the third value \ (Tuples2.thi RD) ")
Tuples2.first = "Test";
Print ("Get the first value \ (Tuples2.first), get the second value \ (Tuples2.second), get the third value \ (Tuples2.third)" )
Advanced Applications for tuples
Defines a function that can exchange two elements in an array
Func swap<t> (_ Nums:inout [T], _ P:int, _ Q:int) {
(Nums[p], nums[q]) = (Nums[q], nums[p])
}
var array = ["Hello", "World") as [any]
For value in array {
print (value);
}
Swap (&array, 0, 2)
For value in array {
print (value);
}
Swift Learning tuple (tuple)