-, tuple type
1. What is a tuple type
A tuple type consists of n arbitrary types of data (n >= 0), and the data that makes up the tuple type can be called an "element"
Example:
Let position = (x:10.5, y:20)//position has 2 elements, X, Y is the name of the element
Let person = (name: ' Jack ')//person only name one element
Let data = ()//Empty tuple
2. Access to elements
var position = (x:10.5, y:20)
(1) with element name
Let value = position.x//value
POSITION.Y = 50//Set Value
(2) position with element
var value = position.0//equivalent to var value = postion.x
POSITION.1 = 50//equivalent to POSTION.Y = 50
code example:
Note: If you use let to define a tuple, then it is a constant and you cannot modify its elements
Let point = (X:10, y:20)
Point.x = 30
The 2nd line of code will error
code example:
3. Meta-Set output
You can output an entire tuple to see the values of all the elements
var point = (x:10.5, y:20)
Point.x = 30
Point.1 = 50
println (Point)
The output is:(30.0, +)
Second, the use of details
(1) Element name can be omitted
Let position = (10, 20)
Let person = ("Jack")
(2) The type of the element can be specified explicitly
var person: (Int, String) = (all, "Rose")
The No. 0 element of person can only be of type int, 1th element can only be string type
Note: You cannot add an element name when you explicitly specify an element type
Therefore, the following statement is incorrect
var person: (Int, String) = (age:23, name: "Rose")
(3) can receive tuple data with multiple variables
var (x, y) = (ten, +)//X is 10,y is 20
var point = (x, y)/A point consists of 2 elements, 10 and 20, respectively
(4) You can assign elements to multiple variables individually
var point = (10, 20)
var (x, y) = point
X is 10,y is 20
(5) You can use the underscore _ to ignore the value of an element and to remove the value of another element
var person = ("Jack")
var (_, name) = person
The content of name is "Jack", and element 20 in person is ignored
Iii. Process Control in Swift
The process structure supported by Swift is as follows:
Loop structure: For, for-in, while, do-while
Select structure: If, switch
Note: These statements must be followed by curly braces {}, which are not required in the C language
Description: In contrast to the C language, the usage is basically the same as: for, while, Do-while, if
So, just focus on for-in and switch to
Iv. Structure of For-in
Simple to use:
For-in and Range operators
For I in 1...3 {
println (i)
}
Assign values in order from the range to I, and execute 1 cycles per 1 values
The length of the range is the number of times the loop body executes
code example:
Tip: If you don't need to use a value in a range, you can use the underscore _ to ignore
For _ in 1...3 {
println ("*********")
}
code example:
Note: I is a constant and its value cannot be changed.
V. Use of Switch
1. Examples of use:
Let grade ="B"SwitchGrade { Case "A": println ("Excellent grade") Case "B": println ("Good grade") Case "C": println ("Medium Grade")default: println ("Unknown level")}View Code
The differences between the 2.switch statements in Swift and C:
In C, if there is no break at the end of the case, then the next or default statement is executed
In Swift, there is no need to add a break after each case, and the switch statement is automatically exited by default after executing the code corresponding to the case
3.switch Point of attention
In swift, there must be a statement that can be executed after each case
Let grade = b" switch Grade { case " a " case " : println ( " good rating " default : println ( " unknown level " )}
View Code
Description: The second line of code will error
Multi-condition matching for 4.case
1 case can be followed by a number of matching conditions, the conditions are separated by commas,
Let grade = b" switch Grade { case " a " case " : println ( " good rating " default : println ( " unknown level " )}
View Code
Range Matching for 5.case
You can fill in a range as a match condition after a case
Let score = theSwitchScore/Ten { Case Ten,9: println ("Excellent") Case 8,7,6: println ("Pass")default: println ("inferior lattice")}//The printing result is: excellentView Code
Attention:
Switch to ensure that all possible cases are handled, or the compiler will directly error
Therefore, the default must be added here, or there will be some less processing situation
6.case Matching tuple
Case can also be used to match tuples. For example, determine if a point is in the blue rectangle in the right image.
Let point = (1,1)SwitchPoint { Case(0,0): println ("this point is on the Origin .") Case(_,0): println ("this point is on the x-axis.") Case(0, _): println ("this point is on the Y axis.") Case(-2...2, -2...2): println ("this point is inside the rectangle")default: println ("this point in other locations")}View Code
The role of _ in line 5th (2 ways of understanding)
(1) can match any value
(2) ignore the corresponding position tuple element
Value binding of 7.case
At the same time as case matching, you can bind the value in switch to a specific constant or variable to use in the statement following the case
Let point = (Ten,0)SwitchPoint { Case(Let X,0): println ("this point is on the x axis and the x value is \ (x)") Case(0, let Y): println ("this point is on the Y axis and the Y value is \ (y)") CaseLet (x, y): println ("The x value of this point is \ (x), and the Y value is \ (y)")}//Print: This point is on the x-axis and the X-value is tenView Code
8.where
The switch statement can use where to increase the condition of the judgment. Like judging whether a point is on the Green Line or the Purple Line of the right image.
var point = (Ten, -Ten)SwitchPoint { CaseLet (x, y)wherex = =Y:println ("this point is on the Green Line.") CaseLet (x, y)wherex = =-Y:println ("this point is on the Purple Line.")default: println ("this point is not on these 2 lines.")}//Print: This point is on the Purple Line.View Code
The role of 9.fallthrough
After executing the current case, the case or default statement after Fallthrough is executed
Note: The case condition after fallthrough cannot define variables and constants
Let num = -var str="\ (num) is a"Switchnum { Case 0... -: Str+="between the 0~50"Fallthroughdefault: Str+="integer"}println (str)//Printed: 20 is an integer between the 0~50View Code
10. Tags
Use one of the 1 functions of the label: can be used to explicitly specify which loop to exit
//do 2 sets of push-ups, each group of 3, finish a group on a breakGroup: for_inch 1...2 { forIteminch 1...3{println ("do 1 push-ups") ifitem = =2 { BreakGroup}} println ("take a break .")}View Code
The output is
Do 1 push-ups
Do 1 push-ups
code example:
iOS development swift-tuple type and Process Control