Swift Control Flow控制流
来源:互联网 发布:办公室暧昧 知乎 编辑:程序博客网 时间:2024/06/06 02:59
一.For-in
1. ...
在下面的例子是打印从1到6的数, 1...6 1是开头 6是结尾,开头和结尾都包括在其中。
第一个例子:
for index in 1...6{ print("index = \(index)")}打印:
index = 1index = 2index = 3index = 4index = 5index = 6
第二个例子:
这个例子相当于普通的for循环。 for(int i = 1;i < 10;i++)
let base = 3let power = 10var answer = 1for _ in 1...power { answer *= base}print("\(base) to the power of \(power) is \(answer)")打印:
3 to the power of 10 is 59049
2.数组的遍历
let names = ["Anna", "Alex", "Brian", "Jack"]for name in names { print("Hello, \(name)!")}
打印:
Hello, Anna!Hello, Alex!Hello, Brian!Hello, Jack!
3.字典的遍历
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]for (animalName, legCount) in numberOfLegs { print("\(animalName)s have \(legCount) legs")}打印:
ants have 6 legscats have 4 legsspiders have 8 legs
4.字符串的遍历
for character in "Dog!".characters { print(character)}打印:
Dog!
二、Repeat-While
先执行循环,知道条件为假就结束,和do-while一样。
repeat { statements} while condition
三、if
这个没什么好讲的。var temperatureInFahrenheit = 90if temperatureInFahrenheit <= 32 { print("It's very cold. Consider wearing a scarf.")} else if temperatureInFahrenheit >= 86 { print("It's really warm. Don't forget to wear sunscreen.")} else { print("It's not that cold. Wear a t-shirt.")}打印:
It's really warm. Don't forget to wear sunscreen.
四、Switch
可以多个条件,可以不要break。
switch some value to consider {case value 1: respond to value 1case value 2,value 3: respond to value 2 or 3default: otherwise, do something else}
例子1:
let someCharacter: Character = "e"switch someCharacter {case "a", "e", "i", "o", "u": print("\(someCharacter) is a vowel")case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m","n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z": print("\(someCharacter) is a consonant")default: print("\(someCharacter) is not a vowel or a consonant")}// prints "e is a vowel"
例子2:
let anotherCharacter: Character = "a"switch anotherCharacter {case "a": print("The letter a")case "A": print("The letter A")default: print("Not the letter A")}// this will report a compile-time error
例子3:
元组的switch - case
let somePoint = (1, 1)switch somePoint {case (0, 0): print("(0, 0) is at the origin")case (_, 0): print("(\(somePoint.0), 0) is on the x-axis")case (0, _): print("(0, \(somePoint.1)) is on the y-axis")case (-2...2, -2...2): print("(\(somePoint.0), \(somePoint.1)) is inside the box")default: print("(\(somePoint.0), \(somePoint.1)) is outside of the box")}// prints "(1, 1) is inside the box"
例子4:
let anotherPoint = (2, 0)switch anotherPoint {case (let x, 0): print("on the x-axis with an x value of \(x)")case (0, let y): print("on the y-axis with a y value of \(y)")case let (x, y): print("somewhere else at (\(x), \(y))")}// prints "on the x-axis with an x value of 2"
例子5:where
let yetAnotherPoint = (1, -1)switch yetAnotherPoint {case let (x, y) where x == y: print("(\(x), \(y)) is on the line x == y")case let (x, y) where x == -y: print("(\(x), \(y)) is on the line x == -y")case let (x, y): print("(\(x), \(y)) is just some arbitrary point")}// prints "(1, -1) is on the line x == -y"
例子6:Continue
let puzzleInput = "great minds think alike"var puzzleOutput = ""for character in puzzleInput.characters { switch character { case "a", "e", "i", "o", "u", " ": continue default: puzzleOutput.append(character) }}print(puzzleOutput)// prints "grtmndsthnklk"打印的时case之外的字符串
例子7:break
let numberSymbol: Character = "三" // Simplified Chinese for the number 3var possibleIntegerValue: Int?switch numberSymbol {case "1", "١", "一", "๑": possibleIntegerValue = 1case "2", "٢", "二", "๒": possibleIntegerValue = 2case "3", "٣", "三", "๓": possibleIntegerValue = 3case "4", "٤", "四", "๔": possibleIntegerValue = 4default: break}if let integerValue = possibleIntegerValue { print("The integer value of \(numberSymbol) is \(integerValue).")} else { print("An integer value could not be found for \(numberSymbol).")}// prints "The integer value of 三 is 3."
例子8:fallthrough
let integerToDescribe = 5var description = "The number \(integerToDescribe) is"switch integerToDescribe {case 2, 3, 5, 7, 11, 13, 17, 19: description += " a prime number, and also" fallthroughdefault: description += " an integer."}print(description)// prints "The number 5 is a prime number, and also an integer."
例子9:#available
检查API的有效性
if #available(platform name version, ..., *) { statements to execute if the APIs are available} else { fallback statements to execute if the APIs are unavailable}
if #available(iOS 9, OSX 10.10, *) { // Use iOS 9 APIs on iOS, and use OS X v10.10 APIs on OS X} else { // Fall back to earlier iOS and OS X APIs}
详细原文请查看
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID120
0 0
- Swift Control Flow控制流
- Swift 控制流(Control Flow)
- [iOS翻译]《The Swift Programming Language》 Control Flow - 控制流
- Swift笔记(四)之控制流(Control Flow)
- 对照Java学习Swift--控制流(Control Flow)
- knockout控制流Control Flow
- 控制流 Flow of Control
- 串口流控制(flow control)
- 串口流控制(flow control)
- 串口流控制(flow control)
- 串口流控制(flow control)
- Exceptional Flow Control(异常控制流)
- 串口流控制(flow control)
- swift学习笔记 --- Control Flow
- Exceptional C++: [Item 47. Control Flow] [条款47 控制流]
- Auto Flow Control (AFC) 自动流控制 与 FIFO
- Swift from Scratch: Optionals and Control Flow
- Swift Cheat Sheet [2] - Control Flow
- <LeetCode OJ> 22. Generate Parentheses
- 一键加群android代码如何使用!
- 12本最具影响力的程序员书籍
- hadoop学习笔记(十)——hadoop + hbase + zookeeper
- Java基础_12_正则表达式
- Swift Control Flow控制流
- 显著性检测算法学习阶段论文总结(3)
- android 自定义控件以及自定义view学习(随机验证码生成)
- MultipeerConnectivity(简介)
- 02-SSH
- 直接插入排序
- 03-SSH server config
- hdu 3308 LCIS
- APP开发外包注意事项有哪些?