Knowledge Points:
- the element in the tuple data can have a name or it can have no name
- elements in the tuple data can be empty
- element Access can be by element name or subscript
- elements in tuple data declared as Var can be changed
- You can specify the type of the element (the name of the element cannot be added in cases where the element type is explicitly indicated)
- you can receive tuple data with multiple variables
- elements can be assigned to multiple variables individually
- you can omit the value of an element with an underscore to remove other elements
Example code:
: Playground-noun:a place where people can playimport uikit//tuple type consists of n any type of data (n >= 0)//The data that makes up the tuple type can be called "element"//1, with element The name and the declaration without the element name let position = (x:10.5, y:20)//position has 2 elements, X, Y is the name of the element let Position2 = (20, 40)//declaration of no element name let person = (Name: "Jack")//person has only one element and the type of the element does not require let data = ()//NULL tuple//2, element access//with element name access position.xposition.y//with element subscript access posit Ion.0position.1//positon is a constant so cannot change the value of the element//2, change the value of the element var Mulpos = (a:10, b:20) MULPOS.A = 20mulpos.0//3, the output of the tuple println ( Mulpos)//4, the type of the specified element var Person2: (Int, String) = ("Jack")//person's No. 0 element can only be of type Int, the first element can only be a String type// Note: The element name cannot be added in cases where the element type is explicitly specified//Therefore, the following statement is wrong//var person: (Int, String) = (age:20, name: "Jack")//5, you can receive the tuple data var (x, y) with multiple variables = (ten, +)//X is 10,y is 20var point = (x, y)//point consists of two elements, 10 and 20//6 respectively, can be assigned to multiple variables var point1 = (Ten, three) var (x1, y1) = point1//x is 10,y is 20//7, can use underscore _ ignore the value of an element, take out the value of other elements var person22 = ("Jack") Var (_, name) = person22//here only to receive Jack, ignoring 20
Swift Learning-tuple type