From d41d1c550bb1b7954e3154878a4b87203e475ef1 Mon Sep 17 00:00:00 2001 From: littledogboy Date: Fri, 3 Jul 2015 23:08:18 +0800 Subject: [PATCH] =?UTF-8?q?statement=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit statement翻译完毕,一代目翻译 --- source/chapter3/10_Statements.md | 217 ++++++++++++++++++++++++++----- 1 file changed, 183 insertions(+), 34 deletions(-) diff --git a/source/chapter3/10_Statements.md b/source/chapter3/10_Statements.md index c9bf88f9..7a98a9b1 100755 --- a/source/chapter3/10_Statements.md +++ b/source/chapter3/10_Statements.md @@ -1,5 +1,5 @@ -> 翻译:[coverxit](https://github.com/coverxit) -> 校对:[numbbbbb](https://github.com/numbbbbb), [coverxit](https://github.com/coverxit), [stanzhai](https://github.com/stanzhai) +> 翻译:[coverxit](https://github.com/coverxit),[littledogboy](https://github.com/littledogboy) +> 校对:[numbbbbb](https://github.com/numbbbbb), [coverxit](https://github.com/coverxit), [stanzhai](https://github.com/stanzhai),[littledogboy](https://github.com/littledogboy) # 语句 ----------------- @@ -11,7 +11,7 @@ - [带标签的语句](#labeled_statement) - [控制传递语句](#control_transfer_statements) -在 Swift 中,有两种类型的语句:简单语句和控制流语句。简单语句是最常见的,用于构造表达式和声明。控制流语句则用于控制程序执行的流程,Swift 中有三种类型的控制流语句:循环语句、分支语句和控制传递语句。 +在 Swift 中,有两种类型的语句:简单语句和控制流语句。简单语句是最常见的,用于构造表达式或者声明。控制流语句则用于控制程序执行的流程,Swift 中有三种类型的控制流语句:循环语句、分支语句和控制传递语句。 循环语句用于重复执行代码块;分支语句用于执行满足特定条件的代码块;控制传递语句则用于修改代码的执行顺序。在稍后的叙述中,将会详细地介绍每一种类型的控制流语句。 @@ -23,7 +23,8 @@ > *语句* → [*循环语句*](..\chapter3\10_Statements.html#loop_statement) **;** _可选_ > *语句* → [*分支语句*](..\chapter3\10_Statements.html#branch_statement) **;** _可选_ > *语句* → [*标记语句(Labeled Statement)*](..\chapter3\10_Statements.html#labeled_statement) -> *语句* → [*控制转移语句*](..\chapter3\10_Statements.html#control_transfer_statement) **;** _可选_ +> *语句* → [*控制转移语句*](..\chapter3\10_Statements.html#control_transfer_statement) **;** _可选_ +> *语句* → [*XXX语句*](..\chapter3\10_Statements.html#control_transfer_statement) **;** _可选_ > *多条语句(Statements)* → [*语句*](..\chapter3\10_Statements.html#statement) [*多条语句(Statements)*](..\chapter3\10_Statements.html#statements) _可选_ @@ -41,7 +42,7 @@ ### For 语句 -`for`语句允许在重复执行代码块的同时,递增一个计数器。 +`for`语句只有在循环条件为真时重复执行代码块,此时计数器递增。 `for`语句的形式如下: @@ -53,12 +54,12 @@ `for`语句的执行流程如下: -1. *initialzation* 只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。 -2. 计算 *condition* 表达式: - 如果为`true`,*statements* 将会被执行,然后转到第3步。如果为`false`,*statements* 和 *increment* 都不会被执行,`for`至此执行完毕。 +1. *initialzation* *循环变量* 只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。 +2. 判断 *condition* 循环条件: + 如果为`true`,*statements* *循环体* 将会被执行,然后转到第3步。如果为`false`,*statements* 和 *increment* *循环增量* 都不会被执行,`for`至此执行完毕。 3. 计算 *increment* 表达式,然后转到第2步。 -定义在 *initialzation* 中的变量仅在`for`语句的作用域以内有效。*condition* 表达式的值的类型必须遵循`LogicValue`协议。 +在 *initialzation* 中定义的变量仅在`for`循环的作用域内有效。*condition* 表达式的值的类型必须遵循`BooleanType `协议。 > For 循环语法 > *for语句* → **for** [*for初始条件*](..\chapter3\10_Statements.html#for_init) _可选_ **;** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_ **;** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_ [*代码块*](..\chapter3\05_Declarations.html#code_block) @@ -82,7 +83,7 @@ ### While 语句 -`while`语句允许重复执行代码块。 +`while`语句当循环条件为真时,允许重复执行代码块。 `while`语句的形式如下: @@ -98,34 +99,37 @@ 由于 *condition* 的值在 *statements* 执行前就已计算出,因此`while`语句中的 *statements* 可能会被执行若干次,也可能不会被执行。 -*condition* 表达式的值的类型必须遵循`LogicValue`协议。同时,*condition* 表达式也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。 +*condition* 表达式的值的类型必须遵循`BooleanType `协议。同时,*condition* 表达式也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。 > While 循环语法 > *while语句* → **while** [*while条件*](..\chapter3\10_Statements.html#while_condition) [*代码块*](..\chapter3\05_Declarations.html#code_block) -> *while条件* → [*表达式*](..\chapter3\04_Expressions.html#expression) | [*声明*](..\chapter3\05_Declarations.html#declaration) +> *while条件* → [*表达式*](..\chapter3\04_Expressions.html#expression) | [*声明*](..\chapter3\05_Declarations.html#declaration) +> *表达式* → [*表达式*](..\chapter3\04_Expressions.html#expression) +> *表达式* → [*表达式*](..\chapter3\04_Expressions.html#expression) | [条件列表](TODO) +> *表达式* → -### Do-While 语句 +### Repeat-While 语句 -`do-while`语句允许代码块被执行一次或多次。 +`repeat-while`语句允许代码块被执行一次或多次。 -`do-while`语句的形式如下: +`repeat-while`语句的形式如下: -> do { +> repeat { > `statements` > } while `condition` -`do-while`语句的执行流程如下: +`repeat-while`语句的执行流程如下: 1. 执行 *statements*,然后转到第2步。 2. 计算 *condition* 表达式: - 如果为`true`,转到第1步。如果为`false`,`do-while`至此执行完毕。 + 如果为`true`,转到第1步。如果为`false`,`repeat-while`至此执行完毕。 -由于 *condition* 表达式的值是在 *statements* 执行后才计算出,因此`do-while`语句中的 *statements* 至少会被执行一次。 +由于 *condition* 表达式的值是在 *statements* 执行后才计算出,因此`repeat-while`语句中的 *statements* 至少会被执行一次。 -*condition* 表达式的值的类型必须遵循`LogicValue`协议。同时,*condition* 表达式也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。 +*condition* 表达式的值的类型必须遵循`BooleanType `协议。同时,*condition* 表达式也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。 -> Do-While 循环语法 -> *do-while语句* → **do** [*代码块*](..\chapter3\05_Declarations.html#code_block) **while** [*while条件*](..\chapter3\10_Statements.html#while_condition) +> Repeat-While 循环语法 +> * repeat-while语句* → **repeat** [*代码块*](..\chapter3\05_Declarations.html#code_block) **while** [*while条件*](..\chapter3\10_Statements.html#while_condition) ## 分支语句 @@ -152,10 +156,10 @@ 第二种形式是在第一种形式的基础上添加 *else 语句*,当只有一个 else 语句时,像下面这样: -> if `condition` { -> `statements to execute if condition is true` -> } else { -> `statements to execute if condition is false` +> if `condition` { +> `statements to execute if condition is true` +> } else { +> `statements to execute if condition is false` > } 同时,else 语句也可包含`if`语句,从而形成一条链来测试更多的条件,像下面这样: @@ -174,7 +178,28 @@ > If语句语法 > *if语句* → **if** [*if条件*](..\chapter3\10_Statements.html#if_condition) [*代码块*](..\chapter3\05_Declarations.html#code_block) [*else子句(Clause)*](..\chapter3\10_Statements.html#else_clause) _可选_ > *if条件* → [*表达式*](..\chapter3\04_Expressions.html#expression) | [*声明*](..\chapter3\05_Declarations.html#declaration) -> *else子句(Clause)* → **else** [*代码块*](..\chapter3\05_Declarations.html#code_block) | **else** [*if语句*](..\chapter3\10_Statements.html#if_statement) +> *else子句(Clause)* → **else** [*代码块*](..\chapter3\05_Declarations.html#code_block) | **else** [*if语句*](..\chapter3\10_Statements.html#if_statement) + +### Guard 语句 + +`guard` 语句用来转移程序控制出该作用域,假如一个或者多个多个条件不成立。 + `guard` 语句的格式如下: + > guard `condition` else { + `statements` + >} + + `guard`语句中条件的值的类型必须遵循`LogicValue`协议。同时,条件也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。 + + 在`guard`语句中声明的常量或者变量,可使用范围为从声明开始到作用域结束,常量和变量的值从可选绑定声明中分配。 + + `guard`语句需要有`else`子句,并且必须调用被`noreturn`属性标记的函数,或者使用下面的语句把程序执行转移到guard语句的作用域外。 + + * `return` + * `break` + * `continue` + * `throw` + +执行转移语句详情参见[执行转移语句](TODO) ### Switch 语句 @@ -226,11 +251,13 @@ case let (x, y) where x == y: > *case标签* → **case** [*case项列表*](..\chapter3\10_Statements.html#case_item_list) **:** > *case项列表* → [*模式*](..\chapter3\07_Patterns.html#pattern) [*guard-clause*](..\chapter3\10_Statements.html#guard_clause) _可选_ | [*模式*](..\chapter3\07_Patterns.html#pattern) [*guard-clause*](..\chapter3\10_Statements.html#guard_clause) _可选_ **,** [*case项列表*](..\chapter3\10_Statements.html#case_item_list) > *default标签* → **default** **:** -> *guard-clause* → **where** [*guard-expression*](..\chapter3\10_Statements.html#guard_expression) -> *guard-expression* → [*表达式*](..\chapter3\04_Expressions.html#expression) +> *where-clause* → **where** [*guard-expression*](..\chapter3\10_Statements.html#guard_expression) +> *where-expression* → [*表达式*](..\chapter3\04_Expressions.html#expression) - 带标签的语句 + + +## 带标签的语句 你可以在循环语句或`switch`语句前面加上*标签*,它由标签名和紧随其后的冒号(:)组成。在`break`和`continue`后面跟上标签名可以显式地在循环语句或`switch`语句中更改控制流,把控制权传递给指定标签标记的语句。关于这两条语句用法,详情参见 [Break 语句](#break_statement)和 [Continue 语句](#continue_statement)。 @@ -242,7 +269,8 @@ case let (x, y) where x == y: > *标记语句(Labeled Statement)* → [*语句标签*](..\chapter3\10_Statements.html#statement_label) [*循环语句*](..\chapter3\10_Statements.html#loop_statement) | [*语句标签*](..\chapter3\10_Statements.html#statement_label) [*switch语句*](..\chapter3\10_Statements.html#switch_statement) > *语句标签* → [*标签名称*](..\chapter3\10_Statements.html#label_name) **:** > *标签名称* → [*标识符*](..\chapter3\02_Lexical_Structure.html#identifier) - + + ## 控制传递语句 通过无条件地把控制权从一片代码传递到另一片代码,控制传递语句能够改变代码执行的顺序。Swift 提供四种类型的控制传递语句:`break`语句、`continue`语句、`fallthrough`语句和`return`语句。 @@ -251,7 +279,8 @@ case let (x, y) where x == y: > *控制传递语句* → [*break语句*](..\chapter3\10_Statements.html#break_statement) > *控制传递语句* → [*continue语句*](..\chapter3\10_Statements.html#continue_statement) > *控制传递语句* → [*fallthrough语句*](..\chapter3\10_Statements.html#fallthrough_statement) -> *控制传递语句* → [*return语句*](..\chapter3\10_Statements.html#return_statement) +> *控制传递语句* → [*return语句*](..\chapter3\10_Statements.html#return_statement) +> *控制传递语句* → [*throw语句*](..\chapter3\10_Statements.html#throw_statement) ### Break 语句 @@ -303,8 +332,9 @@ case let (x, y) where x == y: 关于在`switch`语句中使用`fallthrough`语句的例子,详情参见[控制流](../chapter2/05_Control_Flow.html)一章的[控制传递语句](../chapter2/05_Control_Flow.html#control_transfer_statements)。 > Fallthrough 语句语法 -> *fallthrough语句* → **fallthrough** +> *fallthrough语句* → **fallthrough** + ### Return 语句 `return`语句用于在函数或方法的实现中将控制权传递给调用者,接着程序将会从调用者的位置继续向下执行。 @@ -319,4 +349,123 @@ case let (x, y) where x == y: 而当只写`return`时,仅仅是将控制权从该函数或方法传递给调用者,而不返回一个值。(这就是说,该函数或方法的返回类型为`Void`或`()`) > Return 语句语法 -> *return语句* → **return** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_ +> *return语句* → **return** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_ + + +### Availability 语句 + +可用性条件,被当做`if` ,`while` 语句的条件,并且 `guard` 语句在运行时会基于特定的语法格式查询接口的可用性。 + +avaliability 语句的形式如下: +> if #available(`platform name version`,` ...`, *) { +> `statements to execute if the APIs are available` +> } else { +> `fallback statements to execute if the APIs are unavailable` +> } + +可用性条件执行一个代码块时,取决于在运行时你想要使用的接口是否可用。 +当编译器检查到代码块中的接口是可用的,则从可用性条件中获取相应信息。 + +可用性条件使用逗号分隔平台名称和版本列表。使用`iOS`,`OSX`,以及`watchOS`为平台名称,包括相应的版本号。*参数是必需的。在任何平台上代码块主体都被可用性条件保护起来,由目标规定的最小部署目标执行。 + +与布尔类型条件不同,不能用逻辑运算符 **&&** 和 **||** 合并可用性条件。 + +> 可用性条件语法 +> *可用性条件* → **#available** ( [availability-arguments­](TODO) ) +> *可用性条件* → [availability-argument­](TODO) | [availability-argument](TODO)­ ,­ [availability-arguments­](TODO) +> *可用性条件* → [平台名称](TODO) [版本号](TODO) +> *可用性条件* → **\*** +> *平台名称* → **iOS** | **iOSApplicationExtension** +> *平台名称* → **OSX** | **OSXApplicationExtension­** +> *平台名称* → **watchOS** +> *版本号* → [十进制数字](TODO) +> *版本号* → [十进制数字](TODO) **.** [十进制数字](TODO) +> *版本号* → [十进制数字](TODO) **.** [十进制数字](TODO) **.** [十进制数字](TODO) + + + +### Throw 语句 +`throw`语句出现在抛出函数或者抛出方法体内,或者类型被`throws`关键字标记的表达式体内。 + +`throw`语句使程序结束执行当前的作用域,并在封闭作用域中传播错误。抛出的错误会一直传播,直到被`do`语句的`catch`子句处理掉。 + +`throw`语句由`throw`关键字 跟一个表达式组成 ,如下所示。 + +> throw `expression` + +表达式值的类型必须遵循 `LogicValue`协议 + +关于如何使用`throw`语句的例子,详情参见[错误处理](TODO)一章的[抛出错误](TODO)。 + +> throw 语句语法 +> *抛出语句* → **throw** *[表达式­](TODO)* + + +### Defer 语句 + + `defer` 语句用于转移程序控制出延迟语句作用域之前执行代码。 + +在 `defer` 语句中的语句无论程序控制如何转移都会执行。这意味着 `defer` 语句可以被使用在以下这些情况,像手动得执行资源管理,关闭文件描述,或者即使抛出了错误也需要去实现执行一些动作。 + +如果多个 `defer` 语句出现在同一范围内,那么它们执行的顺序与它们出现的顺序相反。最后执行的 `defer` 语句为给定范围内的第一个,这意味着最后的延迟语句中的语句涉及的资源可以被其他 `defer`语句清理掉。 + +> 1 func f( ) { +> 2 defer { print("First") } +> 3 defer { print("Second") } +> 4 defer { print("Third") } +> 5 } +> 6 f() +> 7 // prints "Third" +> 8 // prints "Second" +> 9 // prints "First" + + +`defer` 语句中的语句无法转移程序控制权出延迟语句。 + +> defer 语句语法 +> *延迟语句* → **defer** *[代码块](TODO)* + + + +### Do 语句 + +`do` 语句用于引入一个新的作用域,该作用域中可以含有一个或多个`catch`子句,catch子句中定义了一些匹配错误情况的模式。`do` 语句作用域内定义的常量和变量,只能在do语句作用域内访问。 + +> do { +> try `expression` +> `statements` +> } catch `pattern 1` { + `statements` +> } catch `pattern 2` where condition { + `statements` +> } + + +如同`switch`语句,编译器会判断`catch`子句是否被遗漏。如果这样的决定被执行,则视为错误被处理。否则,错误会自动传播出包含作用域,被一个封闭的`catch`语句或抛出函数处理,包含函数必须以`throws`关键字声明。 + +为了确保错误已经被处理,使用一个匹配所有错误的`catch`子句,如通配符模式(_)。如果一个`catch`子句不指定一种模式,catch子句会匹配和结合任何错误本地命名常量错误。有关可在catch子句中使用了图纹的更多信息,请参阅模式。 + +为了确保错误已经被处理,使用一个匹配所有错误的`catch`子句,如通配符模式(_)。如果一个`catch`子句不指定一种模式,`catch`子句会匹配和约束任何局部变量命名的`error`。有关在`catch`子句中使用模式的更多信息,详见[模式](TODO)。 + +关于在一些`catch`子句中如何使用` do`语句的例子,详情参见[错误处理](TODO)一章的[抛出错误](TODO)。 + +> do 语句语法 → **do** *[代码块](TODO) [catch子句](TODO)* +> catch子句 → *[catch子句](TODO) [catch子句](TODO)* +> catch子句 → **catch** **[模式](TODO)** *可选的* [where子句]() *可选的* [代码块](TODO) + + + + + + + + + + + + + + + + +