Knowledge Points:
- the variable in the for In loop can use the underscore _, meaning that the variable is not allocated and the system does not allocate storage space for the variable
- There must be an executable statement behind the case in switch, and Swift supports multi-conditional matching if there are many results that need to execute the same statement
- Switch support range Matching
- Switch can match tuple data
- Switch can bind values
- Case in switch can use where to increase the judging condition
- Fallthrough keyword, when the previous case is executed, then executes the case or the default statement after the Fallthrough, fallthrough the condition in the subsequent case statement cannot define the variable
- Label
Example code:
: Playground-noun:a place where people can playimport uikit//1, for In loop///Here I is a let type, that is, constant, if the value of the cyclic weight change I will be error for I in 1. .. 4 {println ("***********\ (i)")}//if I value is not used, it can be written as an underscore _, so that the system does not open a storage space for _ in 1...4 {println ("***********")}//2, S Witch statement//switch requires all cases to be handled or error. So default is generally not omitted//Swift in the switch statement argument can be string///Swift in the case of switch in the case without breaklet flag = "ABC" Switch flag {case "ABC": println ("abc") Case "BCD": println ("BCD") Case "def": println ("Def") Default:println ("Defau LT ")}//switch must have an executable statement after the case, and if there are many results that need to execute the same statement, Swift supports multi-condition matching var gread = 90/10switch Gread {case ten, 9:prin TLN ("excellent") Case 8, 7:println ("good") Case 6:println ("Pass") Default:println ("failed")}//switch in the Range matching var score = 95switch Score {case 90...100:PRINTLN ("excellent") Case 89...60:println ("good") Default: println ("fail")}//switch matches tuple data//Determines if point is on an x-axis of-2 ... 2,y axis is-2 ... 2 of the rectangular box let point = (1, 1) switch point {Case (0, 0): println ("This point is the origin of the coordinates")//The underscore here can be understood as ignoring the value of the x-axis, or to understand that any value is a row of cases (_, 0): println ("This point is on the x-axis") c ASE (0, _): println ("This point on the y-axis") case ( -2...2, -2...2): println ("This point on an x-axis is-2 ... 2,y axis is-2 ... 2 in the rectangle ") default:println (" This point is outside the rectangle ")}//Case's value binding let Point1 = (0) switch Point1 {case (Let x, 0): println ("Point is on X axis, x value is \ (x)") case (0, let y): println ("Point is on y-axis, y value is \ (y)")//The wording here is equivalent to case (let X, let Y) case l ET (x, y): println ("x value is \ (x), Y value is \ (y)") case in}//switch can use where to increase the judging condition let Point2 = ( -10) switch Point2 {case Let (x, y) where x = = Y:println ("monotonically incrementing") Case let (x, y) where x = =-y:println ("monotonically decreasing") Default: println ("Not on these two lines")}//fallthrough//when the previous case is executed, the following case or default statement// The condition in the case statement after Fallthrough cannot define a variable let num = 20var str = "\ (num) is a" switch num {case 0...50:STR + = "0~50" Fallthrough default:str + = "integer"}//3, Label Out:for _ in 1...2 { For _ in 1...3 {println ("start")//The break here is to jump out of the current loop//break//Here The break is out of the parent Ring, out is the label break out} println ("==========")}
Swift Learning-Process Control