Control flow is basically the same, here are a few more interesting places to list.
Switch
Break
The original document is no implicit fallthrough, rough translation is: there is no implicit penetration. Implicit is a frequent occurrence of the word, the Chinese original intention is: "Implicit, implied, hidden accumulation." In Swift, the default processing is usually indicated. For example, the implicit penetration here refers to the traditional case of multiple cases where there is no break to wear to the end. Again for example implicitly unwrapped optionals, implicitly resolves the optional type, is the default will unpack the operation without manual pass! To unpack.
To return to the switch question, look at the following code:
Let Anothercharacter:character = ' a '
switch anothercharacter {case
' a ':
println ("The Letter A ') case
" A ":
println (" The Letter A ")
default:
println (" Not the letter A ")
}
You can see that although the case "a" was matched, it jumped out immediately after the current cases ended and did not continue. If you want to continue through to the following case can be implemented through Fallthrough.
Tuple
We can use Ganso (tuple) to match in the switch. All values are represented by _. For example, the following example determines what area the coordinates belong to:
Let Somepoint = (1, 1)
switch Somepoint {case
(0, 0): //located in Far point
println ("(0, 0) is at the origin")
case (_, 0): //X is any value, Y is 0, i.e. println on the x-axis
("(somepoint.0), 0) is on the x-axis")
case (0, _): //Y for any value, X is 0, that is, in Println on the Y-axis
("(0, \ (somepoint.1)) is on the y-axis")
case ( -2...2, -2...2)://In a square that is centered at the origin and has a side length of 4.
println ("(somepoint.0), \ (Somepoint.1)) is inside the box")
Default:
println ("(somepoint.0), \ ( Somepoint.1)) is outside to the box ")
}
//" (1, 1) is inside the box "
If you want to use this value in the case, you can use the bindings binding (value) to resolve:
Let Somepoint = (0, 1)
switch Somepoint {case
(0, 0):
println ("(0, 0) are at the origin") case
(Let X, 0):
println ("x is \ (x)") Case
(0, let y):
println ("Y is \ (y)")
default:
println ("Default") c28/>}
Where
In case, you can match the parameters by where. For example, we want to print y=x or y=-x this 45-degree look at the situation, was previously through the if solution, can now use switch to start:
Let Yetanotherpoint = (1,-1)
switch Yetanotherpoint {case let
(x, y) where x = = y:
println ("(x), \ (y)) is On the line x = = y ") case let
(x, y) where x = =-Y:
println ((\ (x), \ (y)) is in the line x = y") case let
(x , y):
println ("(x), \ (y)) is just some arbitrary point")
}
//"(1,-1) are on the line x = y"
Control Transfer Statements
Swift has four control transfer states:
Continue-Direct Next loop iteration for loop. Tell the loop body: my cycle is over.
Break-ends the entire flow of control directly against controls flows (loop + switch). It jumps out of the current loop in the loop and jumps out of the current switch in the switch. If you really don't want to do anything with a case in the switch, you can simply add a break in it to ignore it.
Fallthrough-In the switch, lead the code to the next case instead of the default jump switch.
Return-use in functions
Other
See an interesting thing: Swift Cheat Sheet, which is a pure code fragment, if a sudden short circuit forget grammar can come to see.
The
, such as the Control Flow section, has the following code that basically covers all the points:
For loop (array) Let myarray = [1, 1, 2, 3, 5] for value in MyArray {if value = = 1 {println ("one!")
else {println ("not one!") }//For Loop (dictionary) var dict = [' name ': ' Steve Jobs ', ' title ': ' CEO ', ' Company ': ' Apple '] for (key, Valu e) in Dict {println ("\ (key): \ (Value)"}//For loop (range) for I in-1...1 {//[-1, 0, 1] println (i)}//use. To exclude the "last number"//For loop (ignoring "current value of the" range on each iteration of the "loop" for _)
1...3 {//do something three times.}
While loop var i = 1 while I < 1000 {i *= 2}//Do-while loops do {println ("Hello")} while 1 = 2//Switch Let vegetable = ' red pepper ' switch vegetable {case ' celery ': let vegetablecomment = ' Add some raisins and make ants O n a log. "Case" "Cucumber", "watercress": let Vegetablecomment = "This would make a good tea sandwich." Case Let X where X.hassuffix ("Pepper"): let Vegetablecomment = "is it a spicy \ (x)?" Default://required (in order to cover all possible input) let vegetablecomment = "Everything tastes good in soup."} Switch to validate plist content let city:dictionary<string, anyobject> = ["Name": "Qingdao", "Population" : 2_721_000, "abbr": "QD"] switch (city["name", city["population"], city["abbr"]) {case (. Some (let CityName as NSString),. Some (let pop as NSNumber),. Some (let abbr as NSString)) where abbr.length = = 2:println ("City Name: \ (cityname) | Abbr.:\ (abbr)
Population: \ (POP) ") default:println (" Not a valid city ")}
The above is the entire contents of this article, I hope you can enjoy.