Merge pull request #181 from stanzhai/develop

【校对】完成首轮人工校对
This commit is contained in:
SiweiShen
2014-06-15 06:30:04 +08:00
5 changed files with 201 additions and 256 deletions

View File

@ -1,6 +1,5 @@
> 翻译Hawstein
> 校对numbbbbb
> 翻译Hawstein
> 校对numbbbbb, stanzhai
# 特性
-----------------
@ -14,10 +13,8 @@
通过以下方式指定一个特性:符号`@`后面跟特性名,如果包含参数,则把参数带上:
```
@attribute name
@attribute name(attribute arguments)
```
> @`attribute name`
> @`attribute name`(`attribute arguments`)
有些声明特性通过接收参数来指定特性的更多信息以及它是如何修饰一个特定的声明的。这些特性的参数写在小括号内,它们的格式由它们所属的特性来定义。
@ -72,7 +69,7 @@
`objc`特性有一个可选的参数,由标记符组成。当你想把`objc`所修饰的实体以一个不同的名字暴露给Objective-C你就可以使用这个特性参数。你可以使用这个参数来命名类协议方法getterssetters以及初始化器。下面的例子把`ExampleClass``enabled`属性的getter暴露给Objective-C名字是`isEnabled`,而不是它原来的属性名。
```
```swift
@objc
class ExampleClass {
var enabled: Bool {
@ -115,7 +112,7 @@ Interface Builder特性是Interface Builder用来与Xcode同步的声明特性
该特性用于修饰函数或方法的类型,表明该函数或方法不会返回到它的调用者中去。你也可以用它标记函数或方法的声明,表示函数或方法的相应类型,`T`,是`@noreturn T`
> 特性语法
> *特* → **@** [*特性名*](..\chapter3\06_Attributes.html#attribute_name) [*特性参数子句*](..\chapter3\06_Attributes.html#attribute_argument_clause) _可选_
> *特* → **@** [*特性名*](..\chapter3\06_Attributes.html#attribute_name) [*特性参数子句*](..\chapter3\06_Attributes.html#attribute_argument_clause) _可选_
> *特性名* → [*标识符*](LexicalStructure.html#identifier)
> *特性参数子句* → **(** [*平衡令牌列表*](..\chapter3\06_Attributes.html#balanced_tokens) _可选_ **)**
> *特性(Attributes)列表* → [*特色*](..\chapter3\06_Attributes.html#attribute) [*特性(Attributes)列表*](..\chapter3\06_Attributes.html#attributes) _可选_

View File

@ -1,6 +1,5 @@
> 翻译honghaoz
> 校对numbbbbb, stanzhai
> 翻译honghaoz
> 校对numbbbbb, stanzhai
# 模式Patterns
-----------------
@ -35,9 +34,11 @@
通配符模式匹配并忽略任何值包含一个下划线_。当你不关心被匹配的值时可以使用此模式。例如下面这段代码进行了`1...3`的循环,并忽略了每次循环的值:
for _ in 1...3 {
// Do something three times.
}
```swift
for _ in 1...3 {
// Do something three times.
}
```
> 通配符模式语法
> *通配符模式* → **_**
@ -47,7 +48,9 @@
标识符模式匹配任何值,并将匹配的值和一个变量或常量绑定起来。例如,在下面的常量申明中,`someValue`是一个标识符模式,匹配了类型是`Int``42`
let someValue = 42
```swift
let someValue = 42
```
当匹配成功时,`42`被绑定(赋值)给常量`someValue`
@ -63,13 +66,15 @@
标识符模式包含在值绑定模式中,绑定新的变量或常量到匹配的值。例如,你可以分解一个元组的元素,并把每个元素绑定到相应的标识符模式中。
let point = (3, 2)
switch point {
// Bind x and y to the elements of point.
case let (x, y):
println("The point is at (\(x), \(y)).")
}
// prints "The point is at (3, 2).”
```swift
let point = (3, 2)
switch point {
// Bind x and y to the elements of point.
case let (x, y):
println("The point is at (\(x), \(y)).")
}
// prints "The point is at (3, 2).”
```
在上面这个例子中,`let`将元组模式`(x, y)`分配到各个标识符模式。因为这种行为,`switch`语句中`case let (x, y):``case (let x, let y):`匹配的值是一样的。
@ -85,17 +90,21 @@
当元组模式被用在`for-in`语句或者变量或常量申明时,它可以包含通配符模式,标识符模式或者其他包含这两种模式的模式。例如,下面这段代码是不正确的,因为`(x, 0)`中的元素`0`是一个表达式模式:
let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)]
// This code isn't valid.
for (x, 0) in points {
/* ... */
}
```swift
let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)]
// This code isn't valid.
for (x, 0) in points {
/* ... */
}
```
对于只包含一个元素的元组,括号是不起作用的。模式匹配那个单个元素的类型。例如,下面是等效的:
let a = 2 // a: Int = 2
let (a) = 2 // a: Int = 2
let (a): Int = 2 // a: Int = 2
```swift
let a = 2 // a: Int = 2
let (a) = 2 // a: Int = 2
let (a): Int = 2 // a: Int = 2
```
> 元组模式语法
> *元组模式* → **(** [*元组模式元素列表*](..\chapter3\07_Patterns.html#tuple_pattern_element_list) _可选_ **)**
@ -117,8 +126,8 @@
有两种类型转换模式,`is`模式和`as`模式。这两种模式均只出现在`switch`语句中的`case`标签中。`is`模式和`as`模式有以下形式:
is type
pattern as type
> is `type`
> `pattern` as `type`
`is`模式匹配一个值如果这个值的类型在运行时runtime`is`模式右边的指定类型(或者那个类型的子类)是一致的。`is`模式和`is`操作符一样,它们都进行类型转换,但是抛弃了返回的类型。
@ -138,33 +147,36 @@
由表达式模式所代表的表达式用Swift标准库中的`~=`操作符与输入表达式的值进行比较。如果`~=`操作符返回`true`,则匹配成功。默认情况下,`~=`操作符使用`==`操作符来比较两个相同类型的值。它也可以匹配一个整数值与一个`Range`对象中的整数范围,正如下面这个例子所示:
let point = (1, 2)
switch point {
case (0, 0):
println("(0, 0) is at the origin.")
case (-2...2, -2...2):
println("(\(point.0), \(point.1)) is near the origin.")
default:
println("The point is at (\(point.0), \(point.1)).")
}
// prints "(1, 2) is near the origin.”
```swift
let point = (1, 2)
switch point {
case (0, 0):
println("(0, 0) is at the origin.")
case (-2...2, -2...2):
println("(\(point.0), \(point.1)) is near the origin.")
default:
println("The point is at (\(point.0), \(point.1)).")
}
// prints "(1, 2) is near the origin.”
```
你可以重载`~=`操作符来提供自定义的表达式行为。例如,你可以重写上面的例子,以实现用字符串表达的点来比较`point`表达式。
// Overload the ~= operator to match a string with an integer
func ~=(pattern: String, value: Int) -> Bool {
return pattern == "\(value)"
}
switch point {
case ("0", "0"):
println("(0, 0) is at the origin.")
case ("-2...2", "-2...2"):
println("(\(point.0), \(point.1)) is near the origin.")
default:
println("The point is at (\(point.0), \(point.1)).")
}
// prints "(1, 2) is near the origin.”
```swift
// Overload the ~= operator to match a string with an integer
func ~=(pattern: String, value: Int) -> Bool {
return pattern == "\(value)"
}
switch point {
case ("0", "0"):
println("(0, 0) is at the origin.")
case ("-2...2", "-2...2"):
println("(\(point.0), \(point.1)) is near the origin.")
default:
println("The point is at (\(point.0), \(point.1)).")
}
// prints "(1, 2) is near the origin.”
```
> 表达式模式语法
> *表达式模式* → [*表达式*](..\chapter3\04_Expressions.html#expression)
> *表达式模式* → [*表达式*](..\chapter3\04_Expressions.html#expression)

View File

@ -1,6 +1,5 @@
> 翻译fd5788
> 校对yankuangshi
> 翻译fd5788
> 校对yankuangshi, stanzhai
# 泛型参数
---------
@ -19,28 +18,32 @@
泛型形参子句指定泛型类型或函数的类型形参,以及这些参数的关联约束和要求。泛型形参子句用尖括号(<>)包住,并且有以下两种形式:
<generic parameter list>
<generic parameter list where requirements >
> <`generic parameter list`>
> <`generic parameter list` where `requirements`>
泛型形参列表中泛型形参用逗号分开,每一个采用以下形式:
type parameter : constrain
> `type parameter` : `constrain`
泛型形参由两部分组成类型形参及其后的可选约束。类型形参只是占位符类型如TUVKeyTypeValueType等的名字而已。你可以在泛型类型、函数的其余部分或者构造器声明以及函数或构造器的签名中使用它。
约束用于指明该类型形参继承自某个类或者遵守某个协议或协议的一部分。例如,在下面的泛型中,泛型形参`T: Comparable`表示任何用于替代类型形参`T`的类型实参必须满足`Comparable`协议。
func simpleMin<T: COmparable>(x: T, y: T) -> T {
if x < y {
return y
}
return x
```swift
func simpleMin<T: COmparable>(x: T, y: T) -> T {
if x < y {
return y
}
return x
}
```
如,`Int``Double`均满足`Comparable`协议,该函数接受任何一种类型。与泛型类型相反,调用泛型函数或构造器时不需要指定泛型实参子句。类型实参由传递给函数或构造器的实参推断而出。
simpleMin(17, 42) // T is inferred to be Int
simpleMin(3.14159, 2.71828) // T is inferred to be Double
```swift
simpleMin(17, 42) // T is inferred to be Int
simpleMin(3.14159, 2.71828) // T is inferred to be Double
```
## Where 子句
@ -77,19 +80,23 @@
泛型实参子句指定_泛型类型_的类型实参。泛型实参子句用尖括号<>)包住,形式如下:
< generic argument list >
> <`generic argument list`>
泛型实参列表中类型实参有逗号分开。类型实参是实际具体类型的名字用来替代泛型类型的泛型形参子句中的相应的类型形参。从而得到泛型类型的一个特化版本。如Swift标准库的泛型字典类型定义如下
struct Dictionary<KeyTypel: Hashable, ValueType>: Collection, DictionaryLiteralConvertible {
/* .. */
}
```swift
struct Dictionary<KeyTypel: Hashable, ValueType>: Collection, DictionaryLiteralConvertible {
/* .. */
}
```
泛型`Dictionary`类型的特化版本,`Dictionary<String, Int>`就是用具体的`String``Int`类型替代泛型类型`KeyType: Hashable``ValueType`产生的。每一个类型实参必须满足它所替代的泛型形参的所有约束,包括任何`where`子句所指定的额外的要求。上面的例子中,类型形参`KeyType`要求满足`Hashable`协议,因此`String`也必须满足`Hashable`协议。
可以用本身就是泛型类型的特化版本的类型实参替代类型形参(假设已满足合适的约束和要求)。例如,为了生成一个元素类型是整型数组的数组,可以用数组的特化版本`Array<Int>`替代泛型类型`Array<T>`的类型形参`T`来实现。
let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
```swift
let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
```
如[泛型形参子句](#generic_parameter)所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。

View File

@ -95,10 +95,10 @@ _________________
<p></p>
> 控制传递语句(Control Transfer Statement) 语法
> *控制转移语句* → [*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)
> *控制传递语句* → [*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)
<p></p>

View File

@ -1,6 +1,5 @@
> 翻译coverxit
> 校对numbbbbb, coverxit
> 翻译coverxit
> 校对numbbbbb, coverxit, stanzhai
# 语句
-----------------
@ -18,21 +17,14 @@
是否将分号(`;`)添加到语句的结尾处是可选的。但若要在同一行内写多条独立语句,请务必使用分号。
> GRAMMAR OF A STATEMENT
> *statement* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)**;** *opt*
> *statement* → [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)**;** *opt*
> *statement* → [*loop-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement)**;** *opt*
> *statement* → [*branch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement)**;** *opt*
> *statement* → [*labeled-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement)
> *statement* → [*control-transfer-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement)**;** *opt*
> *statement* → [*statment*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement) [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements)**;** *opt*
> 语句语法
> *语句* → [*表达式*](..\chapter3\04_Expressions.html#expression) **;** _可选_
> *语句* → [*声明*](..\chapter3\05_Declarations.html#declaration) **;** _可选_
> *语句* → [*循环语句*](..\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) **;** _可选_
> *多条语句(Statements)* → [*语句*](..\chapter3\10_Statements.html#statement) [*多条语句(Statements)*](..\chapter3\10_Statements.html#statements) _可选_
<a name="loop_statements"></a>
## 循环语句
@ -41,15 +33,11 @@
通过`break`语句和`continue`语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement)和 [Continue 语句](#continue_statement)。
> GRAMMAR OF A LOOP STATEMENT
> *loop-statement* → [*for-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement)
> *loop-statement* → [*for-in-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement)
> *loop-statement* → [*while-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-statement)
> *loop-statement* → [*do-while-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement)
> 循环语句语法
> *循环语句* → [*for语句*](..\chapter3\10_Statements.html#for_statement)
> *循环语句* → [*for-in语句*](..\chapter3\10_Statements.html#for_in_statement)
> *循环语句* → [*while语句*](..\chapter3\10_Statements.html#wheetatype类型ile_statement)
> *循环语句* → [*do-while语句*](..\chapter3\10_Statements.html#do_while_statement)
### For 语句
@ -57,32 +45,25 @@
`for`语句的形式如下:
```swift
for `initialzation`; `condition`; `increment` {
`statements`
}
```
> for `initialzation`; `condition`; `increment` {
> `statements`
> }
*initialzation*、*condition* 和 *increment* 之间的分号,以及包围循环体 *statements* 的大括号都是不可省略的。
`for`语句的执行流程如下:
1. *initialzation* 只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。
2. 计算 *condition* 表达式:
如果为`true`*statements* 将会被执行然后转到第3步。如果为`false`*statements* 和 *increment* 都不会被执行,`for`至此执行完毕。
3. 计算 *increment* 表达式然后转到第2步。
定义在 *initialzation* 中的变量仅在`for`语句的作用域以内有效。*condition* 表达式的值的类型必须遵循`LogicValue`协议。
> GRAMMAR OF A FOR STATEMENT
> *for-statement* → **for** [*for-init*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
> *for-statement* → **for (** [*for-init*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* **;** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt* **)** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
> *for-statement* → [*variable-declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration) | [*expression-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list)
> 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)
> *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)
> *for初始条件* → [*变量声明*](..\chapter3\05_Declarations.html#variable_declaration) | [*表达式列表*](..\chapter3\04_Expressions.html#expression_list)
### For-In 语句
@ -90,17 +71,14 @@ for `initialzation`; `condition`; `increment` {
`for-in`语句的形式如下:
```swift
for `item` in `collection` {
`statements`
}
```
> for `item` in `collection` {
> `statements`
> }
`for-in`语句在循环开始前会调用 *collection* 表达式的`generate`方法来获取一个生成器类型(这是一个遵循`Generator`协议的类型)的值。接下来循环开始,调用 *collection* 表达式的`next`方法。如果其返回值不是`None`,它将会被赋给 *item*,然后执行 *statements*,执行完毕后回到循环开始处;否则,将不会赋值给 *item* 也不会执行 *statements*`for-in`至此执行完毕。
> GRAMMAR OF A FOR-IN STATEMENT
> *for-in-statement* → **for** [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) **in** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
> For-In 循环语法
> *for-in语句* → **for** [*模式*](..\chapter3\07_Patterns.html#pattern) **in** [*表达式*](..\chapter3\04_Expressions.html#expression) [*代码块*](..\chapter3\05_Declarations.html#code_block)
### While 语句
@ -108,28 +86,23 @@ for `item` in `collection` {
`while`语句的形式如下:
```swift
while `condition` {
`statements`
}
```
> while `condition` {
> `statements`
> }
`while`语句的执行流程如下:
1. 计算 *condition* 表达式:
如果为真`true`转到第2步。如果为`false``while`至此执行完毕。
2. 执行 *statements* 然后转到第1步。
由于 *condition* 的值在 *statements* 执行前就已计算出,因此`while`语句中的 *statements* 可能会被执行若干次,也可能不会被执行。
*condition* 表达式的值的类型必须遵循`LogicValue`协议。同时,*condition* 表达式也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。
> GRAMMAR OF A WHILE STATEMENT
> *while-statement* → **while** [*while-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block)
> *while-condition* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)
> 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)
### Do-While 语句
@ -137,16 +110,13 @@ while `condition` {
`do-while`语句的形式如下:
```swift
do {
`statements`
} while `condition`
```
> do {
> `statements`
> } while `condition`
`do-while`语句的执行流程如下:
1. 执行 *statements*然后转到第2步。
2. 计算 *condition* 表达式:
如果为`true`转到第1步。如果为`false``do-while`至此执行完毕。
@ -154,9 +124,8 @@ do {
*condition* 表达式的值的类型必须遵循`LogicValue`协议。同时,*condition* 表达式也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。
> GRAMMAR OF A DO-WHILE STATEMENT
> *do-while-statement* → **do** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) **while** [*while-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition)
> Do-While 循环语法
> *do-while语句* → **do** [*代码块*](..\chapter3\05_Declarations.html#code_block) **while** [*while条件*](..\chapter3\10_Statements.html#while_condition)
<a name="branch_statements"></a>
## 分支语句
@ -165,11 +134,9 @@ do {
`switch`语句中的控制流可以用`break`语句修改,详情请见[Break 语句](#break_statement)。
> GRAMMAR OF A BRANCH STATEMENT
> *branch-statement* → [*if-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement)
> *branch-statement* → [*switch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement)
> 分支语句语法
> *分支语句* → [*if语句*](..\chapter3\10_Statements.html#if_statement)
> *分支语句* → [*switch语句*](..\chapter3\10_Statements.html#switch_statement)
### If 语句
@ -179,44 +146,35 @@ do {
第一种形式是当且仅当条件为真时执行代码,像下面这样:
```swift
if `condition` {
`statements`
}
```
> if `condition` {
> `statements`
> }
第二种形式是在第一种形式的基础上添加 *else 语句*,当只有一个 else 语句时,像下面这样:
```swift
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`语句,从而形成一条链来测试更多的条件,像下面这样:
```swift
if `condition 1` {
`statements to execute if condition 1 is true`
} else if `condition 2` {
`statements to execute if condition 2 is true`
}
else {
`statements to execute if both conditions are false`
}
```
> if `condition 1` {
> `statements to execute if condition 1 is true`
> } else if `condition 2` {
> `statements to execute if condition 2 is true`
> }
> else {
> `statements to execute if both conditions are false`
> }
`if`语句中条件的值的类型必须遵循`LogicValue`协议。同时,条件也可以使用可选绑定,详情参见[可选绑定](../chapter2/01_The_Basics.html#optional_binding)。
> GRAMMAR OF AN IF STATEMENT
> *if-statement* → **if** [*if-condition*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition) [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) [*else-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause) *opt*
> *if-condition* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) | [*declaration*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration)
> *else-clause* → **else** [*code-block*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block) | **else** [*if-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement) *opt*
> 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)
### Switch 语句
@ -224,19 +182,17 @@ else {
`switch`语句的形式如下:
```swift
switch `control expression` {
case `pattern 1`:
`statements`
case `pattern 2` where `condition`:
`statements`
case `pattern 3` where `condition`,
`pattern 4` where `condition`:
`statements`
default:
`statements`
}
```
> switch `control expression` {
> case `pattern 1`:
> `statements`
> case `pattern 2` where `condition`:
> `statements`
> case `pattern 3` where `condition`,
> `pattern 4` where `condition`:
> `statements`
> default:
> `statements`
> }
`switch`语句的*控制表达式control expression*会首先被计算,然后与每一个 case 的模式pattern进行匹配。如果匹配成功程序将会执行对应的 case 分支里的 *statements*。另外,每一个 case 分支都不能为空,也就是说在每一个 case 分支中至少有一条语句。如果你不想在匹配到的 case 分支中执行代码,只需在该分支里写一条`break`语句即可。
@ -262,25 +218,16 @@ case let (x, y) where x == y:
当匹配的 case 分支中的代码执行完毕后,程序会终止`switch`语句,而不会继续执行下一个 case 分支。这就意味着,如果你想执行下一个 case 分支,需要显式地在你需要的 case 分支里使用`fallthrough`语句。关于`fallthrough`语句的更多信息,详情参见 [Fallthrough 语句](#fallthrough_statement)。
> GRAMMAR OF A SWITCH STATEMENT
> *switch-statement* → **switch** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) **{** [*switch-cases*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases) *opt* **}**
> *switch-cases* → [*switch-case*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case) [*switch-cases*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases) *opt*
> *switch-case* → [*case-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label) [*statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements) | [*default-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label) [*statements*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements)
> *switch-case* → [*case-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label) **;** | [*default-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label) **;**
> *case-label* → **case** [*case-item-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list) **:**
> *case-item-list* → [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) [*guard-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause) *opt* | [*pattern*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern) [*guard-clause*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause) *opt*, [*case-item-list*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list)
> *default-label* → **default :**
> *guard-clause* → **where** [*guard-expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression)
> *guard-expression* → [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression)
> Switch语句语法
> *switch语句* → **switch** [*表达式*](..\chapter3\04_Expressions.html#expression) **{** [*SwitchCase列表*](..\chapter3\10_Statements.html#switch_cases) _可选_ **}**
> *SwitchCase列表* → [*SwitchCase*](..\chapter3\10_Statements.html#switch_case) [*SwitchCase列表*](..\chapter3\10_Statements.html#switch_cases) _可选_
> *SwitchCase* → [*case标签*](..\chapter3\10_Statements.html#case_label) [*多条语句(Statements)*](..\chapter3\10_Statements.html#statements) | [*default标签*](..\chapter3\10_Statements.html#default_label) [*多条语句(Statements)*](..\chapter3\10_Statements.html#statements)
> *SwitchCase* → [*case标签*](..\chapter3\10_Statements.html#case_label) **;** | [*default标签*](..\chapter3\10_Statements.html#default_label) **;**
> *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)
<a name="labeled_statement"></a>
<a name="control_transfer_statements"></a> 带标签的语句
@ -291,38 +238,28 @@ case let (x, y) where x == y:
关于使用带标签的语句的例子,详情参见[控制流](../chapter2/05_Control_Flow.html)一章的[带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
> GRAMMAR OF A LABELED STATEMENT
> *labeled-statement* → [*statement-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label) [*loop-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement) | [*statement-label*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label) [*switch-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement)
> *statement-label* → [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) **:**
> *label-name* → [*identifier*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier)
> 标记语句语法
> *标记语句(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`语句。
> GRAMMAR OF A CONTROL TRANSER STATEMENT
> *control-transfer-statement* → [*break-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement)
> *control-transfer-statement* → [*continue-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement)
> *control-transfer-statement* → [*fallthrough-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement)
> *control-transfer-statement* → [*return-statement*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement)
> 控制传递语句(Control Transfer Statement) 语法
> *控制传递语句* → [*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)
<a name="break_statement"></a>
### Break 语句
`break`语句用于终止循环或`switch`语句的执行。使用`break`语句时,可以只写`break`这个关键词,也可以在`break`后面跟上标签名label name像下面这样
```swift
break
break `label name`
```
> break
> break `label name`
`break`语句后面带标签名时,可用于终止由这个标签标记的循环或`switch`语句的执行。
@ -332,19 +269,16 @@ break `label name`
关于使用`break`语句的例子,详情参见[控制流](../chapter2/05_Control_Flow.html)一章的 [Break](../chapter2/05_Control_Flow.html#break) 和[带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
> GRAMMAR OF A BREAK STATEMENT
> *break-statement* → **break** [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) *opt*
> Break 语句语法
> *break语句* → **break** [*标签名称*](..\chapter3\10_Statements.html#label_name) _可选_
<a name="continue_statement"></a>
### Continue 语句
`continue`语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用`continue`语句时,可以只写`continue`这个关键词,也可以在`continue`后面跟上标签名label name像下面这样
```swift
continue
continue `label name`
```
> continue
> continue `label name`
`continue`语句后面带标签名时,可用于终止由这个标签标记的循环中当前迭代的执行。
@ -356,9 +290,8 @@ continue `label name`
关于使用`continue`语句的例子,详情参见[控制流](../chapter2/05_Control_Flow.html)一章的 [Continue](../chapter2/05_Control_Flow.html#continue) 和[带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
> GRAMMAR OF A CONTINUE STATEMENT
> *continue-statement* → **continue** [*label-name*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name) *opt*
> Continue 语句语法
> *continue语句* → **continue** [*标签名称*](..\chapter3\10_Statements.html#label_name) _可选_
<a name="fallthrough_statement"></a>
### Fallthrough 语句
@ -369,9 +302,8 @@ continue `label name`
关于在`switch`语句中使用`fallthrough`语句的例子,详情参见[控制流](../chapter2/05_Control_Flow.html)一章的[控制传递语句](../chapter2/05_Control_Flow.html#control_transfer_statements)。
> GRAMMAR OF A FALLTHROUGH STATEMENT
> *continue-statement* → **fallthrough**
> Fallthrough 语句语法
> *fallthrough语句* → **fallthrough**
### Return 语句
@ -379,15 +311,12 @@ continue `label name`
使用`return`语句时,可以只写`return`这个关键词,也可以在`return`后面跟上表达式,像下面这样:
```swift
return
return `expression`
```
> return
> return `expression`
`return`语句后面带表达式时表达式的值将会返回给调用者。如果表达式值的类型与调用者期望的类型不匹配Swift 则会在返回表达式的值之前将表达式值的类型转换为调用者期望的类型。
而当只写`return`时,仅仅是将控制权从该函数或方法传递给调用者,而不返回一个值。(这就是说,该函数或方法的返回类型为`Void``()`
> GRAMMAR OF A RETURN STATEMENT
> *return-statement* → **return** [*expression*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression) *opt*
> Return 语句语法
> *return语句* → **return** [*表达式*](..\chapter3\04_Expressions.html#expression) _可选_