原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID120

1、For循环

(1)for in

使用for-in来遍历一个集合里的元素。

  1. for index in ... {
  2. print("\(index) times 5 is \(index * 5)")
  3. }
  4. // 1 times 5 is 5
  5. // 2 times 5 is 10
  6. // 3 times 5 is 15
  7. // 4 times 5 is 20
  8. // 5 times 5 is 25

上面的例子中,index是一个每次循环遍历开始时被自动赋值的常量。这种情况下,index在使用前不需要声明,只需要将它包含在循环的声明中,就可以对其进行隐式声明,而无需使用let关键字声明。

如果不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问:

  1. let base =
  2. let power =
  3. var answer =
  4. for _ in ...power {
  5. answer *= base
  6. }
  7. print("\(base) to the power of \(power) is \(answer)")
  8. // prints "3 to the power of 10 is 59049"

使用for-in遍历一个数组所有元素:

  1. let names = ["Anna", "Alex", "Brian", "Jack"]
  2. for name in names {
  3. print("Hello, \(name)!")
  4. }
  5. // Hello, Anna!
  6. // Hello, Alex!
  7. // Hello, Brian!
  8. // Hello, Jack!

遍历字典:

  1. let numberOfLegs = ["spider": , "ant": , "cat": ]
  2. for (animalName, legCount) in numberOfLegs {
  3. print("\(animalName)s have \(legCount) legs")
  4. }
  5. // ants have 6 legs
  6. // cats have 4 legs
  7. // spiders have 8 legs

字典元素的遍历顺序和插入顺序可能不同,字典的内容在内部是无序的,所以遍历元素时不能保证顺序。

(2)for(V2.1)

除了for-in循环,Swift 提供使用条件判断和递增方法的标准 C 样式for循环:

  1. 1 for var index = 0; index < 3; ++index {
  2. 2 print("index is \(index)")
  3. 3 }
  4. 4 // index is 0
  5. 5 // index is 1
  6. 6 // index is 2

在初始化表达式中声明的常量和变量(比如var index = 0)只在for循环的生命周期里有效。如果想在循环结束后访问index的值,你必须要在循环生命周期开始前声明index

  1. 1 var index: Int
  2. 2 for index = 0; index < 3; ++index {
  3. 3 print("index is \(index)")
  4. 4 }
  5. 5 // index is 0
  6. 6 // index is 1
  7. 7 // index is 2
  8. 8 print("The loop statements were executed \(index) times")
  9. 9 // prints "The loop statements were executed 3 times"

2、While循环

(1)while

  1. var square =
  2. var diceRoll =
  3. while square < finalSquare {
  4. // 掷骰子
  5. if ++diceRoll == { diceRoll = }
  6. // 根据点数移动
  7. square += diceRoll
  8. if square < board.count {
  9. // 如果玩家还在棋盘上,顺着梯子爬上去或者顺着蛇滑下去
  10. square += board[square]
  11. }
  12. }
  13. print("Game over!")

(2)repeat-while

相当于其他语言中的do-while

  1. repeat {
  2. // move up or down for a snake or ladder
  3. square += board[square]
  4. // roll the dice
  5. if ++diceRoll == { diceRoll = }
  6. // move by the rolled amount
  7. square += diceRoll
  8. } while square < finalSquare
  9. print("Game over!")

3、if

  1. temperatureInFahrenheit =
  2. if temperatureInFahrenheit <= {
  3. print("It's very cold. Consider wearing a scarf.")
  4. } else if temperatureInFahrenheit >= {
  5. print("It's really warm. Don't forget to wear sunscreen.")
  6. } else {
  7. print("It's not that cold. Wear a t-shirt.")
  8. }
  9. // prints "It's really warm. Don't forget to wear sunscreen."

4、Switch

  1. let someCharacter: Character = "e"
  2. switch someCharacter {
  3. case "a", "e", "i", "o", "u":
  4. print("\(someCharacter) is a vowel")
  5. case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
  6. "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
  7. print("\(someCharacter) is a consonant")
  8. default:
  9. print("\(someCharacter) is not a vowel or a consonant")
  10. }
  11. // prints "e is a vowel"

与 C 语言和 Objective-C 中的switch语句不同,在 Swift 中,当匹配的 case 分支中的代码执行完毕后,程序会终止switch语句,而不会继续执行下一个 case 分支。这也就是说,不需要在 case 分支中显式地使用break语句。这使得switch语句更安全、更易用,也避免了因忘记写break语句而产生的错误。

每一个 case 分支都必须包含至少一条语句。像下面这样书写代码是无效的,因为第一个 case 分支是空的:

  1. let anotherCharacter: Character = "a"
  2. switch anotherCharacter {
  3. case "a":
  4. case "A":
  5. print("The letter A")
  6. default:
  7. print("Not the letter A")
  8. }
  9. // this will report a compile-time error

一个 case 也可以包含多个模式,用逗号把它们分开(如果太长了也可以分行写):

  1. switch some value to consider {
  2. case value ,
  3. value :
  4. statements
  5. }

(1)区间匹配

  1. let approximateCount =
  2. let countedThings = "moons orbiting Saturn"
  3. var naturalCount: String
  4. switch approximateCount {
  5. case :
  6. naturalCount = "no"
  7. case ..<:
  8. naturalCount = "a few"
  9. case ..<:
  10. naturalCount = "several"
  11. case ..<:
  12. naturalCount = "dozens of"
  13. case ..<:
  14. naturalCount = "hundreds of"
  15. default:
  16. naturalCount = "many"
  17. }
  18. print("There are \(naturalCount) \(countedThings).")
  19. // prints "There are dozens of moons orbiting Saturn."

(2)元组

  1. let somePoint = (, )
  2. switch somePoint {
  3. case (, ):
  4. print("(0, 0) is at the origin")
  5. case (_, ):
  6. print("(\(somePoint.0), 0) is on the x-axis")
  7. case (, _):
  8. print("(0, \(somePoint.1)) is on the y-axis")
  9. case (-..., -...):
  10. print("(\(somePoint.0), \(somePoint.1)) is inside the box")
  11. default:
  12. print("(\(somePoint.0), \(somePoint.1)) is outside of the box")
  13. }
  14. // prints "(1, 1) is inside the box"

(3)值绑定

允许将匹配的值绑定到一个临时的常量或变量,这些常量或变量在该 case 分支里就可以被引用了。

  1. let anotherPoint = (, )
  2. switch anotherPoint {
  3. case (let x, ):
  4. print("on the x-axis with an x value of \(x)")
  5. case (, let y):
  6. print("on the y-axis with a y value of \(y)")
  7. case let (x, y):
  8. print("somewhere else at (\(x), \(y))")
  9. }
  10. // prints "on the x-axis with an x value of 2"

(4)Where

Switch的case分支允许用where子句来作额外的判断:

  1. let yetAnotherPoint = (, -)
  2. switch yetAnotherPoint {
  3. case let (x, y) where x == y:
  4. print("(\(x), \(y)) is on the line x == y")
  5. case let (x, y) where x == -y:
  6. print("(\(x), \(y)) is on the line x == -y")
  7. case let (x, y):
  8. print("(\(x), \(y)) is just some arbitrary point")
  9. }
  10. // prints "(1, -1) is on the line x == -y"

(5)复合case语句

  1. let someCharacter: Character = "e"
  2. switch someCharacter {
  3. case "a", "e", "i", "o", "u":
  4. print("\(someCharacter) is a vowel")
  5. case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
  6. "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
  7. print("\(someCharacter) is a consonant")
  8. default:
  9. print("\(someCharacter) is not a vowel or a consonant")
  10. }
  11. // Prints "e is a vowel"

在复合case语句中也可以使用值绑定:

  1. let stillAnotherPoint = (, )
  2. switch stillAnotherPoint {
  3. case (let distance, ), (, let distance):
  4. print("On an axis, \(distance) from the origin")
  5. default:
  6. print("Not on an axis")
  7. }
  8. // Prints "On an axis, 9 from the origin"

5、控制转移语句

(1)continue

continue语句告诉一个循环体立刻停止本次循环迭代,重新开始下次循环迭代。

  1. let puzzleInput = "great minds think alike"
  2. var puzzleOutput = ""
  3. for character in puzzleInput.characters {
  4. switch character {
  5. case "a", "e", "i", "o", "u", " ":
  6. continue
  7. default:
  8. puzzleOutput.append(character)
  9. }
  10. }
  11. print(puzzleOutput)
  12. // 输出 "grtmndsthnklk"

(2)break

当在一个循环体中使用break时,会立刻中断该循环体的执行,然后跳转到表示循环体结束的大括号(})后的第一行代码。

当在一个switch代码块中使用break时,会立即中断该switch代码块的执行,并且跳转到表示switch代码块结束的大括号(})后的第一行代码。

  1. let numberSymbol: Character = "三" // Simplified Chinese for the number 3
  2. var possibleIntegerValue: Int?
  3. switch numberSymbol {
  4. case "", "١", "一", "๑":
  5. possibleIntegerValue =
  6. case "", "٢", "二", "๒":
  7. possibleIntegerValue =
  8. case "", "٣", "三", "๓":
  9. possibleIntegerValue =
  10. case "", "٤", "四", "๔":
  11. possibleIntegerValue =
  12. default:
  13. break
  14. }
  15. if let integerValue = possibleIntegerValue {
  16. print("The integer value of \(numberSymbol) is \(integerValue).")
  17. } else {
  18. print("An integer value could not be found for \(numberSymbol).")
  19. }
  20. // prints "The integer value of 三 is 3."

(3)Fallthrough

Swift 中的switch不会从上一个 case 分支落入到下一个 case 分支中。相反,只要第一个匹配到的 case 分支完成了它需要执行的语句,整个switch代码块完成了它的执行。相比之下,C 语言要求你显示的插入break语句到每个switch分支的末尾来阻止自动落入到下一个 case 分支中。

如果需要 C 风格的贯穿的特性,可以在每个需要该特性的 case 分支中使用fallthrough关键字:

  1. let integerToDescribe =
  2. var description = "The number \(integerToDescribe) is"
  3. switch integerToDescribe {
  4. case , , , , , , , :
  5. description += " a prime number, and also"
  6. fallthrough
  7. default:
  8. description += " an integer."
  9. }
  10. print(description)
  11. // prints "The number 5 is a prime number, and also an integer."

注意: fallthrough关键字不会检查它下一个将会落入执行的 case 中的匹配条件。fallthrough简单地使代码执行继续连接到下一个 case 中的执行代码,这和 C 语言标准中的switch语句特性是一样的。

(4)带Label的语句

在 Swift 中,可以在循环体和switch代码块中嵌套循环体和switch代码块来创造复杂的控制流结构。然而,循环体和switch代码块两者都可以使用break语句来提前结束整个方法体。因此,显示地指明break语句想要终止的是哪个循环体或者switch代码块,会很有用。类似地,如果你有许多嵌套的循环体,显示指明continue语句想要影响哪一个循环体也会非常有用。

为了实现这个目的,你可以使用标签来标记一个循环体或者switch代码块,当使用break或者continue时,带上这个标签,可以控制该标签代表对象的中断或者执行。

  1. gameLoop: while square != finalSquare {
  2. if ++diceRoll == { diceRoll = }
  3. switch square + diceRoll {
  4. case finalSquare:
  5. // 到达最后一个方块,游戏结束
  6. break gameLoop
  7. case let newSquare where newSquare > finalSquare:
  8. // 超出最后一个方块,再掷一次骰子
  9. continue gameLoop
  10. default:
  11. // 本次移动有效
  12. square += diceRoll
  13. square += board[square]
  14. }
  15. }
  16. print("Game over!")

(5)提前退出

if语句一样,guard语句的执行取决于一个表达式的布尔值。可以用guard语句来要求条件必须为真时执行guard语句后的代码。不同于if语句,一个guard语句总是有一个else分句,如果条件不为真则执行else分局中的代码。

  1. func greet(person: [String: String]) {
  2. guard let name = person["name"] else {
  3. return
  4. }
  5.  
  6. print("Hello \(name)!")
  7.  
  8. guard let location = person["location"] else {
  9. print("I hope the weather is nice near you.")
  10. return
  11. }
  12.  
  13. print("I hope the weather is nice in \(location).")
  14. }
  15.  
  16. greet(["name": "John"])
  17. // prints "Hello John!"
  18. // prints "I hope the weather is nice near you."
  19. greet(["name": "Jane", "location": "Cupertino"])
  20. // prints "Hello Jane!"
  21. // prints "I hope the weather is nice in Cupertino."

如果条件不被满足,在else分支上的代码就会被执行。这个分支必须转移控制以退出guard语句出现的代码段。它可以用控制转移语句如return,breakcontinue做这件事,或者调用一个不返回的方法或函数,例如fatalError()

与可以实现同样功能的if语句相比,使用guard语句会提升我们代码的可靠性。 可以使你的代码连贯地被执行而不需要将它包在else块中,它可以使你处理违反要求的代码接近要求。

(6)检查API的可用性

  1. if #available(iOS , OSX 10.10, *) {
  2. // Use iOS 9 APIs on iOS, and use OS X v10.10 APIs on OS X
  3. } else {
  4. // Fall back to earlier iOS and OS X APIs
  5. }

以上可用性条件指定在iOS平台下,if段的代码仅仅在iOS9及更高系统中可运行;在OS X平台下,仅在OS X v10.10及更高系统可运行。最后一个参数,*,是必须的并且指定在任何其他平台上,if段的代码在最小可用部署目标指定项目中执行。

在它普遍的形式中,可用性条件获取了平台名字和版本的清单。平台名字可以是iOSOSXwatchOS。除了特定的主板本号像iOS8,我们可以指定较小的版本号像iOS8.3以及 OS X v10.10.3。

  1. if #available(platform name version, ..., *) {
  2. statements to execute if the APIs are available
  3. } else {
  4. fallback statements to execute if the APIs are unavailable
  5. }

Swift3.0P1 语法指南——控制流的更多相关文章

  1. Swift3.0P1 语法指南——构造器

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  2. Swift3.0P1 语法指南——下标

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  3. Swift3.0P1 语法指南——方法

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  4. Swift3.0P1 语法指南——属性

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  5. Swift3.0P1 语法指南——类和结构体

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  6. Swift3.0P1 语法指南——枚举

    原档: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programm ...

  7. Swift3.0P1 语法指南——闭包

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  8. Swift3.0P1 语法指南——函数

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  9. Swift3.0P1 语法指南——基本操作符

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

随机推荐

  1. intellij idea让资源文件自动更新

    intellij idea默认文件是自动保存的,但是手头有个项目jsp文件改动后,在tomcat中不能立即响应变化.要jsp文件改动后立刻看到变化,有个配置.在idea tomcat 中server的 ...

  2. jquery工具方法access详解

    access : 多功能值操作(内部) access方法可以使set/get方法在一个函数中体现.比如我们常用的css,attr都是调用了access方法. css的使用方法: $(selector) ...

  3. [转有改动]vi

    转自http://www.51testing.com/html/86/427686-247344.html 多按几次[ESC],系统会发出滴滴声以确定进入命令模式.就进入了命令模式,所有在键盘上打的字 ...

  4. java并发编程学习: 阻塞队列 使用 及 实现原理

    队列(Queue)与栈(Stack)是数据结构中的二种常用结构,队列的特点是先进先出(First In First Out),而Stack是先进后出(First In Last Out),说得通俗点: ...

  5. js通过日期计算属于星期几

    var arys1 = new Array(); arys1 = "2016-09-25".split('-'); //日期为输入日期,格式为 2013-3-10 var ssda ...

  6. OpenGL在Ubuntu 14.04 中的设置与编程

    在sudo apt-get install XXX,别的教程讲的很详细了. 编写好程序需要在shell中链接 g++ teapot.c -o teapot -lglut -lGL -lGLU 此处要注 ...

  7. FineUI(专业版)实现百变通知框(无JavaScript代码)!

    博客园已经越来越不公正了,居然说我这篇文章没有实质的内容!! 我其实真的想问哪些通篇几十个字,没任何代码和技术分享,嚷嚷着送书的文章的就能雄霸博客园首页几天,我这篇文章偏偏就为管理员所容不下. 其实我 ...

  8. Mysql索引PRIMARY、NORMAL、UNIQUE、FULLTEXT 区别和使用场合

    索引 数据库的索引就像一本书的目录,能够加快数据库的查询速度. MYSQL索引有四种PRIMARY.INDEX.UNIQUE.FULLTEXT, 其中PRIMARY.INDEX.UNIQUE是一类,F ...

  9. 【poj1694】 An Old Stone Game

    http://poj.org/problem?id=1694 (题目链接) 题意 一棵树,现在往上面放石子.对于一个节点x,只有当它的直接儿子都放满石子时,才能将它直接儿子中的一个石子放置x上,并回收 ...

  10. Select Top在七种数据库中的使用方法(包含mysql)

    1. Oracle数据库 SELECT * FROM TABLE1 WHERE ROWNUM<=N 2. Infomix数据库 SELECT FIRST N * FROM TABLE1 3. D ...