swift3.0p1语法指南——控制流

小妖 小妖     2022-07-31     233

关键词:

原档: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 1...5 {
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 = 3
2 let power = 10
3 var answer = 1
4 for _ in 1...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": 8, "ant": 6, "cat": 4]
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

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

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

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

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

2、While循环

(1)while

 1 var square = 0
 2 var diceRoll = 0
 3 while square < finalSquare {
 4     // 掷骰子
 5     if ++diceRoll == 7 { diceRoll = 1 }
 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 == 7 { diceRoll = 1 }
6     // move by the rolled amount
7     square += diceRoll
8 } while square < finalSquare
9 print("Game over!")

3、if

1 temperatureInFahrenheit = 90
2 if temperatureInFahrenheit <= 32 {
3     print("It‘s very cold. Consider wearing a scarf.")
4 } else if temperatureInFahrenheit >= 86 {
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 1,
3 value 2:
4     statements
5 }

(1)区间匹配

 1 let approximateCount = 62
 2 let countedThings = "moons orbiting Saturn"
 3 var naturalCount: String
 4 switch approximateCount {
 5 case 0:
 6     naturalCount = "no"
 7 case 1..<5:
 8     naturalCount = "a few"
 9 case 5..<12:
10     naturalCount = "several"
11 case 12..<100:
12     naturalCount = "dozens of"
13 case 100..<1000:
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 = (1, 1)
 2 switch somePoint {
 3 case (0, 0):
 4     print("(0, 0) is at the origin")
 5 case (_, 0):
 6     print("((somePoint.0), 0) is on the x-axis")
 7 case (0, _):
 8     print("(0, (somePoint.1)) is on the y-axis")
 9 case (-2...2, -2...2):
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, 0)
 2 switch anotherPoint {
 3 case (let x, 0):
 4     print("on the x-axis with an x value of (x)")
 5 case (0, 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 = (1, -1)
 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"

 

1             let stillAnotherPoint = (9, 0)
2             switch stillAnotherPoint {
3                 case (let distance, 0), (0, 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 "1", "?", "", "?":
 5     possibleIntegerValue = 1
 6 case "2", "?", "", "?":
 7     possibleIntegerValue = 2
 8 case "3", "?", "", "?":
 9     possibleIntegerValue = 3
10 case "4", "?", "", "?":
11     possibleIntegerValue = 4
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 = 5
 2 var description = "The number (integerToDescribe) is"
 3 switch integerToDescribe {
 4 case 2, 3, 5, 7, 11, 13, 17, 19:
 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 == 7 { diceRoll = 1 }
 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 9, 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语法指南——字符串与字符

原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-ID2851、字符串字面值1letsomeString 查看详情

swift3.0p1语法指南——基本操作符

原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID60关于基本操作符Swift支持大多数的标准C操作符,也改进了 查看详情

android:kotlin详细入门学习指南-包-控制流-返回与跳转-基础语法(代码片段)

...:Kotlin详细入门学习指南-包-控制流-返回与跳转-基础语法(三)建议先看看前面的文章Android:Kotlin详细入门学习指南-基础语法(一)Android:Kotlin详细入门学习指南-基本类型-基础语法(二)这篇文章主要分享Kotl 查看详情

kotlin语法基础,控制流

控制流通常,计算机程序编写出来,就是为了让计算机理解而解决某问题。按照预先设定的顺序执行的指令序列,也就显得尤为重要。程序员除了学会告诉计算机需要处理的那些数据以外,还需要学会指定这些指... 查看详情

rtmp规范1.0全面指南

参考技术ARTMP(realtimemessagingprotocol)协议本文为Adobertmp规范1.0的中文介绍,其中内容大部分都是翻译自rtmp官方文档rtmp_specification_1.0.pdfAdobe的实时消息传输协议(RTMP)通过可靠的流传输(如TCP[RFC0793])提供双向消息多路传输服务... 查看详情

无容器控制流语法 - 模板无法使用 IE8 正确呈现

】无容器控制流语法-模板无法使用IE8正确呈现【英文标题】:Containerlesscontrolflowsyntax-thetemplatesdon\'trenderproperlywithIE8【发布时间】:2012-08-2616:23:14【问题描述】:使用无容器语法并调用Knockoutjs模板时,IE8无法在foreach控制流中正... 查看详情

控制流分析

...单的字符序列。词法分析器将这些字符序列转换为单词,语法分析器从中进一步发现语法结构。由编译器前端产生的结果可以是语法树或者某种低级形式的中间代码。但是即使这样,它对程序做什么和怎么做仍然没有多少提示。... 查看详情

android:kotlin详细入门学习指南-函数-基础语法(代码片段)

...f0c;持续更新Android:Kotlin详细入门学习指南-函数-基础语法(六)建议先看看前面的文章Android:Kotlin详细入门学习指南-基础语法(一)Android:Kotlin详细入门学习指南-基本类型-基础语法(二)Android:Kotlin详细入门学习指南... 查看详情

流控制语句

...件为真或假时执行不同的语句,可以用If……else语句实现语法格式如下: IFLogical_expression   expression1  [ELSE   expression2]用变量示例:--显示笔试平均成绩、再根 查看详情

markdown入门及语法详细指南★(代码片段)

...arkdown编辑工具1.平台集成2.独立软件3.插件拓展Markdown基本语法快捷键1.标题2.文本3.列表4.引用5.代码6.链接与图像7.表格8.分隔线9.1数学公式(扩展语法)9.2图表(扩展语法)转义字符语法技巧Markdown功能拓展与语法... 查看详情

swift3.0previewe1语法指南——基础

原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309--------------------------------- 查看详情

ffprobe使用指南

1.ffprobe是什么?简单的说,ffprobe是一个多媒体流分析工具。它从多媒体流中收集信息,并且以人类和机器可读的形式打印出来。它可以用来检测多媒体流的容器类型,以及每一个多媒体流的格式和类型。它可以作为一个独立的... 查看详情

读书笔记(控制系统设计指南第1,2章)

一、知识要点记录第一章控制理论简介1.2控制系统1.2.1控制器:由控制率和功率变换器协同工作。1.2.2被控机器:由被控对象与反馈装置组成。 1.3控制工程师:主要工作是负责系统集成与试运行,同时也应该参与控制器的选... 查看详情

mysql初学者必备:控制流函数,日期和时间函数(代码片段)

控制流函数:     if函数:          语法:if (条件,'符合条件的情况','不符合条件的情况')     ifnull函数:           语法:ifnull (正常显示,'为null时显示的文字'&... 查看详情

训练指南网络流题集(代码片段)

layout:posttitle:训练指南网络流题集author:"luowentaoaa"catalog:truemathjax:truetags:-网络流-图论-训练指南A.UVA-11248(最大流,最小割)UVA-11248FrequencyHopping题意给定一个有向网络,每条边均有一个容量。问是否存在一个从点1到点N,流量... 查看详情

Java:异常作为控制流?

】Java:异常作为控制流?【英文标题】:Java:Exceptionsascontrolflow?【发布时间】:2010-12-0512:20:29【问题描述】:我听说对控制流使用异常是不好的做法。您对此有何看法?publicstaticfindStringMatch(g0,g1)intg0Left=-1;intg0Right=-1;intg1Left=-1;intg... 查看详情

Java:异常作为控制流?

】Java:异常作为控制流?【英文标题】:Java:Exceptionsascontrolflow?【发布时间】:2010-12-0512:20:29【问题描述】:我听说对控制流使用异常是不好的做法。您对此有何看法?publicstaticfindStringMatch(g0,g1)intg0Left=-1;intg0Right=-1;intg1Left=-1;intg... 查看详情

jdk1.8新特性——stream(流)的中间操作基本语法

...treamAPI的操作步骤图解五、Stream(流)的中间操作语法1、筛选与切片2、映射3、排 查看详情