The control flow is basically the same, here are a few interesting places.
switch
Break
The original document is No Implicit Fallthrough. A rough translation is: there is no implicit through. Among them, Implicit is a word that often appears, and the original meaning of Chinese is: "subtle, suggestive, hidden". In Swift, it usually means default processing. For example, the implicit penetration here refers to the traditional situation where multiple cases will be worn from top to bottom without break. For example, implicitly unwrapped optionals, implicitly parsing optional types, the default unpacking operation will be performed without manually unpacking.
Go back to the switch problem, 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" matches, it jumps out directly after the current case ends and does not continue to execute. If you want to continue to run through the case below, you can achieve it by falling through.
Tuple
We can use tuple in switch for matching. Use _ to represent all values. For example, in the following example, determine which region the coordinates belong to:
let somePoint = (1, 1) switch somePoint { case (0, 0): // Located at a far point println("(0, 0) is at the origin") case (_, 0): // x is any value, y is 0, that is, on the X axis println("(\(somePoint.0), 0) is on the x-axis") case (0, _): // y is any value and x is 0, that is, on the Y axis println("(0, \(somePoint.1)) is on the y-axis") case (-2...2, -2...2): // In a square with the origin as the center and the side length is 4. println("(\(somePoint.0), \(somePoint.1)) is inside the box") default: println("(\(somePoint.0), \(somePoint.1)) is outside of the box") } // "(1, 1) is inside the box"
If you want to use this value in case, you can use value bindings to solve it:
let somePoint = (0, 1) switch somePoint { case (0, 0): println("(0, 0) is at the origin") case (let x, 0): println("x is \(x)") case (0, let y): println("y is \(y)") default: println("default") }
Where
The parameters can be matched through where in case. For example, if we want to print a 45-degree look-up situation like y=x or y=-x, it used to be solved by if, but now we can use switch to do it:
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 on the line x == -y") case let (x, y): println("(\(x), \(y)) is just some arbitrary point") } // "(1, -1) is on the line x == -y”
Control Transfer Statements
Swift has four control transfer statuses:
continue - For loop , directly perform the next loop iteration. Tell the loop body: My loop has ended.
break - For control flow (loop + switch), it ends the entire control flow directly. The current loop will pop up in the loop, and the current switch will pop up in the switch. If you really don't want to do anything with a case in the switch, you can directly add break to ignore it.
fallthrough - In switch, lead the code to the next case instead of jumping out of the switch by default.
return - Used in the function
other
I saw an interesting thing: Swift Cheat Sheet, which is a pure code snippet. If you suddenly short circuit and forget the syntax, you can take a look.
For example, the Control Flow part has the following code, which basically covers all 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, value) 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 the current value of the range on each iteration of the loop) for _ in 1...3 { // Do something three times. } // while loop var i = 1 while i < 1000 { i *= 2 } // do-while loop do { println("hello") } while 1 == 2 // Switch let vegetable = "red pepper" switch vegetable { case "celery": let vegetableComment = "Add some raisins and make ants on a log." case "cucumber", "watercress": let vegetableComment = "That would make a good tea sandwich." case let x where ("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 == 2: println("City Name: \(cityName) | Abbr.:\(abbr) Population: \(pop)") default: println("Not a valid city") }
The above is the entire content of this article, I hope you like it.