fix quota format

This commit is contained in:
Jie Liang
2019-03-26 22:44:42 -05:00
parent 810eba1150
commit 1d01a9900b
10 changed files with 1306 additions and 2 deletions

View File

@ -21,10 +21,13 @@ Swift 语言相对较小,这是由于 Swift 代码中常用的类型、函数
> getter-setter 方法块语法 > getter-setter 方法块语法
> >
> *getter-setter 方法块* → { [*getter 子句*](05_Declarations.html#getter-clause) [*setter 子句*](05_Declarations.html#setter-clause)<sub>可选</sub> } | { [*setter 子句*](05_Declarations.html#setter-clause) [*getter 子句*](05_Declarations.html#getter-clause) } > *getter-setter 方法块* → { [*getter 子句*](05_Declarations.html#getter-clause) [*setter 子句*](05_Declarations.html#setter-clause)<sub>可选</sub> } | { [*setter 子句*](05_Declarations.html#setter-clause) [*getter 子句*](05_Declarations.html#getter-clause) }
>
这个定义表明,一个 getter-setter 方法块可以由一个 getter 分句后跟一个可选的 setter 分句构成,然后用大括号括起来,或者由一个 setter 分句后跟一个 getter 分句构成,然后用大括号括起来。上述的语法产式等价于下面的两个语法产式, 这个定义表明,一个 getter-setter 方法块可以由一个 getter 分句后跟一个可选的 setter 分句构成,然后用大括号括起来,或者由一个 setter 分句后跟一个 getter 分句构成,然后用大括号括起来。上述的语法产式等价于下面的两个语法产式,
> getter-setter 方法块语法 > getter-setter 方法块语法
> >
> getter-setter 方法块 → { [*getter 子句*](05_Declarations.html#getter-clause) [*setter 子句*](05_Declarations.html#setter-clause)<sub>可选</sub> } > getter-setter 方法块 → { [*getter 子句*](05_Declarations.html#getter-clause) [*setter 子句*](05_Declarations.html#setter-clause)<sub>可选</sub> }
>
> getter-setter 方法块 → { [*setter 子句*](05_Declarations.html#setter-clause) [*getter 子句*](05_Declarations.html#getter-clause) } > getter-setter 方法块 → { [*setter 子句*](05_Declarations.html#setter-clause) [*getter 子句*](05_Declarations.html#getter-clause) }
>

View File

@ -12,28 +12,45 @@ Swift 的*“词法结构lexical structure”* 描述了能构成该语言
注释被编译器当作空白处理。单行注释由 `//` 开始直至遇到换行符U+000A或者回车符U+000D。多行注释由 `/*` 开始,以 `*/` 结束。注释允许嵌套,但注释标记必须匹配。 注释被编译器当作空白处理。单行注释由 `//` 开始直至遇到换行符U+000A或者回车符U+000D。多行注释由 `/*` 开始,以 `*/` 结束。注释允许嵌套,但注释标记必须匹配。
> 空白语法 > 空白语法
>
<a id="whitespace"></a> <a id="whitespace"></a>
> *空白* → [*空白项*](#whitespace-item) [*空白*](#whitespace)<sub>可选</sub> > *空白* → [*空白项*](#whitespace-item) [*空白*](#whitespace)<sub>可选</sub>
>
> *空白项* → [*断行符*](#line-break) > *空白项* → [*断行符*](#line-break)
>
> *空白项* → [*注释*](#comment) > *空白项* → [*注释*](#comment)
>
> *空白项* → [*多行注释*](#multiline-comment) > *空白项* → [*多行注释*](#multiline-comment)
>
> *空白项* → U+0000U+0009U+000BU+000C 或者 U+0020 > *空白项* → U+0000U+0009U+000BU+000C 或者 U+0020
>
<a id="line-break"></a> <a id="line-break"></a>
> *断行符* → U+000A > *断行符* → U+000A
>
> *断行符* → U+000D > *断行符* → U+000D
>
> *断行符* → U+000D 接着是 U+000A > *断行符* → U+000D 接着是 U+000A
>
<a id="comment"></a> <a id="comment"></a>
> *注释* → // [*注释内容 断行*](#comment-text line-break) > *注释* → // [*注释内容 断行*](#comment-text line-break)
>
> *多行注释* → `/*` [*多行注释内容*](#multiline-commnet-text) `*/` > *多行注释* → `/*` [*多行注释内容*](#multiline-commnet-text) `*/`
>
> *注释内容* → [*注释内容项*](#comment-text-item) [*注释内容*](#comment-text)<sub>可选</sub> > *注释内容* → [*注释内容项*](#comment-text-item) [*注释内容*](#comment-text)<sub>可选</sub>
>
> *注释内容项* → 任何 Unicode 标量值, 除了 U+000A 或者 U+000D > *注释内容项* → 任何 Unicode 标量值, 除了 U+000A 或者 U+000D
>
> *多行注释内容* → [*多行注释内容项*](#multiline-comment-text-item) [*多行注释内容*](#multiline-comment-text)<sub>可选</sub> > *多行注释内容* → [*多行注释内容项*](#multiline-comment-text-item) [*多行注释内容*](#multiline-comment-text)<sub>可选</sub>
>
> *多行注释内容项* → [*多行注释*](#multiline-comment). > *多行注释内容项* → [*多行注释*](#multiline-comment).
>
> *多行注释内容项* → [*注释内容项*](#comment-text-item) > *多行注释内容项* → [*注释内容项*](#comment-text-item)
>
> *多行注释内容项* → 任何 Unicode 标量值, 除了 `/*` 或者 `*/` > *多行注释内容项* → 任何 Unicode 标量值, 除了 `/*` 或者 `*/`
>
注释可以包含额外的格式和标记,正如 [*Markup Formatting Reference*](https://developer.apple.com/library/prerelease/ios/documentation/Xcode/Reference/xcode_markup_formatting_ref/index.html#//apple_ref/doc/uid/TP40016497) 所述。 注释可以包含额外的格式和标记,正如 [*Markup Formatting Reference*](https://developer.apple.com/library/prerelease/ios/documentation/Xcode/Reference/xcode_markup_formatting_ref/index.html#//apple_ref/doc/uid/TP40016497) 所述。
@ -47,42 +64,69 @@ Swift 的*“词法结构lexical structure”* 描述了能构成该语言
闭包中如果没有明确指定参数名称,参数将被隐式命名为 `$0`、`$1`、`$2` 等等。这些命名在闭包作用域范围内是合法的标识符。 闭包中如果没有明确指定参数名称,参数将被隐式命名为 `$0`、`$1`、`$2` 等等。这些命名在闭包作用域范围内是合法的标识符。
> 标识符语法 > 标识符语法
>
<a id="identifier"></a> <a id="identifier"></a>
> *标识符* → [*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub> > *标识符* → [*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub>
>
> *标识符* → \`[*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub>\` > *标识符* → \`[*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub>\`
>
> *标识符* → [*隐式参数名*](#implicit-parameter-name) > *标识符* → [*隐式参数名*](#implicit-parameter-name)
>
<a id="identifier-list"></a> <a id="identifier-list"></a>
> *标识符列表* → [*标识符*](#identifier) | [*标识符*](#identifier) **,** [*标识符列表*](#identifier-list) > *标识符列表* → [*标识符*](#identifier) | [*标识符*](#identifier) **,** [*标识符列表*](#identifier-list)
>
<a id="identifier-head"></a> <a id="identifier-head"></a>
> *头部标识符* → 大写或小写字母 A - Z > *头部标识符* → 大写或小写字母 A - Z
>
> *头部标识符* → _ > *头部标识符* → _
>
> *头部标识符* → U+00A8U+00AAU+00ADU+00AFU+00B2U+00B5或者 U+00B7U+00BA > *头部标识符* → U+00A8U+00AAU+00ADU+00AFU+00B2U+00B5或者 U+00B7U+00BA
>
> *头部标识符* → U+00BCU+00BEU+00C0U+00D6U+00D8U+00F6或者 U+00F8U+00FF > *头部标识符* → U+00BCU+00BEU+00C0U+00D6U+00D8U+00F6或者 U+00F8U+00FF
>
> *头部标识符* → U+0100U+02FFU+0370U+167FU+1681U+180D或者 U+180FU+1DBF > *头部标识符* → U+0100U+02FFU+0370U+167FU+1681U+180D或者 U+180FU+1DBF
>
> *头部标识符* → U+1E00U+1FFF > *头部标识符* → U+1E00U+1FFF
>
> *头部标识符* → U+200BU+200DU+202AU+202EU+203FU+2040U+2054或者 U+2060U+206F > *头部标识符* → U+200BU+200DU+202AU+202EU+203FU+2040U+2054或者 U+2060U+206F
>
> *头部标识符* → U+2070U+20CFU+2100U+218FU+2460U+24FF或者 U+2776U+2793 > *头部标识符* → U+2070U+20CFU+2100U+218FU+2460U+24FF或者 U+2776U+2793
>
> *头部标识符* → U+2C00U+2DFF 或者 U+2E80U+2FFF > *头部标识符* → U+2C00U+2DFF 或者 U+2E80U+2FFF
>
> *头部标识符* → U+3004U+3007U+3021U+302FU+3031U+303F或者 U+3040U+D7FF > *头部标识符* → U+3004U+3007U+3021U+302FU+3031U+303F或者 U+3040U+D7FF
>
> *头部标识符* → U+F900U+FD3DU+FD40U+FDCFU+FDF0U+FE1F或者 U+FE30U+FE44 > *头部标识符* → U+F900U+FD3DU+FD40U+FDCFU+FDF0U+FE1F或者 U+FE30U+FE44
>
> *头部标识符* → U+FE47U+FFFD > *头部标识符* → U+FE47U+FFFD
>
> *头部标识符* → U+10000U+1FFFDU+20000U+2FFFDU+30000U+3FFFD或者 U+40000U+4FFFD > *头部标识符* → U+10000U+1FFFDU+20000U+2FFFDU+30000U+3FFFD或者 U+40000U+4FFFD
>
> *头部标识符* → U+50000U+5FFFDU+60000U+6FFFDU+70000U+7FFFD或者 U+80000U+8FFFD > *头部标识符* → U+50000U+5FFFDU+60000U+6FFFDU+70000U+7FFFD或者 U+80000U+8FFFD
>
> *头部标识符* → U+90000U+9FFFDU+A0000U+AFFFDU+B0000U+BFFFD或者 U+C0000U+CFFFD > *头部标识符* → U+90000U+9FFFDU+A0000U+AFFFDU+B0000U+BFFFD或者 U+C0000U+CFFFD
>
> *头部标识符* → U+D0000U+DFFFD 或者 U+E0000U+EFFFD > *头部标识符* → U+D0000U+DFFFD 或者 U+E0000U+EFFFD
>
<a id="identifier-character"></a> <a id="identifier-character"></a>
> *标识符字符* → 数值 0 - 9 > *标识符字符* → 数值 0 - 9
>
> *标识符字符* → U+0300U+036FU+1DC0U+1DFFU+20D0U+20FF或者 U+FE20U+FE2F > *标识符字符* → U+0300U+036FU+1DC0U+1DFFU+20D0U+20FF或者 U+FE20U+FE2F
>
> *标识符字符* → [*头部标识符*](#identifier-head) > *标识符字符* → [*头部标识符*](#identifier-head)
>
> <a id="identifier-characters"></a> > <a id="identifier-characters"></a>
>
> *标识符字符组* → [*标识符字符*](#identifier-character) [*标识符字符组*](#identifier-characters)<sub>可选</sub> > *标识符字符组* → [*标识符字符*](#identifier-character) [*标识符字符组*](#identifier-characters)<sub>可选</sub>
>
<a id="implicit-parameter-name"></a> <a id="implicit-parameter-name"></a>
> *隐式参数名* → **$** [*十进制数字列表*](#decimal-digits) > *隐式参数名* → **$** [*十进制数字列表*](#decimal-digits)
>
<a id="keywords"></a> <a id="keywords"></a>
## 关键字和标点符号 ## 关键字和标点符号
@ -119,13 +163,19 @@ true // 布尔值字面量
> 字面量语法 > 字面量语法
> >
> *字面量* → [*数值字面量*](#numeric-literal) | [*字符串字面量*](#string-literal) | [*布尔值字面量*](#boolean-literal) | [*nil 字面量*](#nil-literal) > *字面量* → [*数值字面量*](#numeric-literal) | [*字符串字面量*](#string-literal) | [*布尔值字面量*](#boolean-literal) | [*nil 字面量*](#nil-literal)
>
<a id="numeric-literal"></a> <a id="numeric-literal"></a>
> *数值字面量* → **-**<sub>可选</sub> [*整数字面量*](#integer-literal) | **-**<sub>可选</sub> [*浮点数字面量*](#floating-point-literal) > *数值字面量* → **-**<sub>可选</sub> [*整数字面量*](#integer-literal) | **-**<sub>可选</sub> [*浮点数字面量*](#floating-point-literal)
>
> <a id="boolean-literal"></a> > <a id="boolean-literal"></a>
>
> *布尔值字面量* → **true** | **false** > *布尔值字面量* → **true** | **false**
>
> <a id="nil-literal"></a> > <a id="nil-literal"></a>
>
> *nil 字面量* → **nil** > *nil 字面量* → **nil**
>
<a id="integer_literals"></a> <a id="integer_literals"></a>
### 整数字面量 ### 整数字面量
@ -144,47 +194,81 @@ true // 布尔值字面量
> >
<a id="integer-literal"></a> <a id="integer-literal"></a>
> *整数字面量* → [*二进制字面量*](#binary-literal) > *整数字面量* → [*二进制字面量*](#binary-literal)
>
> *整数字面量* → [*八进制字面量*](#octal-literal) > *整数字面量* → [*八进制字面量*](#octal-literal)
>
> *整数字面量* → [*十进制字面量*](#decimal-literal) > *整数字面量* → [*十进制字面量*](#decimal-literal)
>
> *整数字面量* → [*十六进制字面量*](#hexadecimal-literal) > *整数字面量* → [*十六进制字面量*](#hexadecimal-literal)
>
<a id="binary-literal"></a> <a id="binary-literal"></a>
> *二进制字面量* → **0b** [*二进制数字*](#binary-digit) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub> > *二进制字面量* → **0b** [*二进制数字*](#binary-digit) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
>
> <a id="binary-digit"></a> > <a id="binary-digit"></a>
>
> *二进制数字* → 数值 0 到 1 > *二进制数字* → 数值 0 到 1
>
> <a id="binary-literal-character"></a> > <a id="binary-literal-character"></a>
>
> *二进制字面量字符* → [*二进制数字*](#binary-digit) | _ > *二进制字面量字符* → [*二进制数字*](#binary-digit) | _
>
> <a id="binary-literal-characters"></a> > <a id="binary-literal-characters"></a>
>
> *二进制字面量字符组* → [*二进制字面量字符*](#binary-literal-character) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub> > *二进制字面量字符组* → [*二进制字面量字符*](#binary-literal-character) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
>
<a id="octal-literal"></a> <a id="octal-literal"></a>
> *八进制字面量* → **0o** [*八进字数字*](#octal-digit) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub> > *八进制字面量* → **0o** [*八进字数字*](#octal-digit) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
>
> <a id="octal-digit"></a> > <a id="octal-digit"></a>
>
> *八进字数字* → 数值 0 到 7 > *八进字数字* → 数值 0 到 7
>
> <a id="octal-literal-character"></a> > <a id="octal-literal-character"></a>
>
> *八进制字符* → [*八进字数字*](#octal-digit) | _ > *八进制字符* → [*八进字数字*](#octal-digit) | _
>
> <a id="octal-literal-characters"></a> > <a id="octal-literal-characters"></a>
>
> *八进制字符组* → [*八进制字符*](#octal-literal-character) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub> > *八进制字符组* → [*八进制字符*](#octal-literal-character) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
>
<a id="decimal-literal"></a> <a id="decimal-literal"></a>
> *十进制字面量* → [*十进制数字*](#decimal-digit) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub> > *十进制字面量* → [*十进制数字*](#decimal-digit) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
>
> <a id="decimal-digit"></a> > <a id="decimal-digit"></a>
>
> *十进制数字* → 数值 0 到 9 > *十进制数字* → 数值 0 到 9
>
> <a id="decimal-digits"></a> > <a id="decimal-digits"></a>
>
> *十进制数字组* → [*十进制数字*](#decimal-digit) [*十进制数字组*](#decimal-digits)<sub>可选</sub> > *十进制数字组* → [*十进制数字*](#decimal-digit) [*十进制数字组*](#decimal-digits)<sub>可选</sub>
>
> <a id="decimal-literal-character"></a> > <a id="decimal-literal-character"></a>
>
> *十进制字符* → [*十进制数字*](#decimal-digit) | _ > *十进制字符* → [*十进制数字*](#decimal-digit) | _
>
> <a id="decimal-literal-characters"></a> > <a id="decimal-literal-characters"></a>
>
> *十进制字符组* → [*十进制字符*](#decimal-literal-character) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub> > *十进制字符组* → [*十进制字符*](#decimal-literal-character) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
>
<a id="hexadecimal-literal"></a> <a id="hexadecimal-literal"></a>
> *十六进制字面量* → **0x** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub> > *十六进制字面量* → **0x** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
>
> <a id="hexadecimal-digit"></a> > <a id="hexadecimal-digit"></a>
>
> *十六进制数字* → 数值 0 到 9, 字母 a 到 f, 或 A 到 F > *十六进制数字* → 数值 0 到 9, 字母 a 到 f, 或 A 到 F
>
> <a id="hexadecimal-literal-character"></a> > <a id="hexadecimal-literal-character"></a>
>
> *十六进制字符* → [*十六进制数字*](#hexadecimal-digit) | _ > *十六进制字符* → [*十六进制数字*](#hexadecimal-digit) | _
>
> <a id="hexadecimal-literal-characters"></a> > <a id="hexadecimal-literal-characters"></a>
>
> *十六进制字面量字符组* → [*十六进制字符*](#hexadecimal-literal-character) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub> > *十六进制字面量字符组* → [*十六进制字符*](#hexadecimal-literal-character) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
>
<a id="floating_point_literals"></a> <a id="floating_point_literals"></a>
### 浮点数字面量 ### 浮点数字面量
@ -207,24 +291,37 @@ true // 布尔值字面量
> >
<a id="floating-point-literal"></a> <a id="floating-point-literal"></a>
> *浮点数字面量* → [*十进制字面量*](#decimal-literal) [*十进制分数*](#decimal-fraction)<sub>可选</sub> [*十进制指数*](#decimal-exponent)<sub>可选</sub> > *浮点数字面量* → [*十进制字面量*](#decimal-literal) [*十进制分数*](#decimal-fraction)<sub>可选</sub> [*十进制指数*](#decimal-exponent)<sub>可选</sub>
>
> *浮点数字面量* → [*十六进制字面量*](#hexadecimal-literal) [*十六进制分数*](#hexadecimal-fraction)<sub>可选</sub> [*十六进制指数*](#hexadecimal-exponent) > *浮点数字面量* → [*十六进制字面量*](#hexadecimal-literal) [*十六进制分数*](#hexadecimal-fraction)<sub>可选</sub> [*十六进制指数*](#hexadecimal-exponent)
>
<a id="decimal-fraction"></a> <a id="decimal-fraction"></a>
> *十进制分数* → **.** [*十进制字面量*](#decimal-literal) > *十进制分数* → **.** [*十进制字面量*](#decimal-literal)
>
> <a id="decimal-exponent"></a> > <a id="decimal-exponent"></a>
>
> *十进制指数* → [*十进制指数 e*](#floating-point-e) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal) > *十进制指数* → [*十进制指数 e*](#floating-point-e) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal)
>
<a id="hexadecimal-fraction"></a> <a id="hexadecimal-fraction"></a>
> *十六进制分数* → **.** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub> > *十六进制分数* → **.** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
>
> <a id="hexadecimal-exponent"></a> > <a id="hexadecimal-exponent"></a>
>
> *十六进制指数* → [*十六进制指数 p*](#floating-point-p) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal) > *十六进制指数* → [*十六进制指数 p*](#floating-point-p) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal)
>
<a id="floating-point-e"></a> <a id="floating-point-e"></a>
> *十进制指数 e* → **e** | **E** > *十进制指数 e* → **e** | **E**
>
> <a id="floating-point-p"></a> > <a id="floating-point-p"></a>
>
> *十六进制指数 p* → **p** | **P** > *十六进制指数 p* → **p** | **P**
>
> <a id="sign"></a> > <a id="sign"></a>
>
> *正负号* → **+** | **-** > *正负号* → **+** | **-**
>
<a id="string_literals"></a> <a id="string_literals"></a>
### 字符串字面量 ### 字符串字面量
@ -232,13 +329,17 @@ true // 布尔值字面量
字符串字面量是被引号包括的一串字符组成。 单行字符串字面量被包在双引号中的一串字符组成,形式如下: 字符串字面量是被引号包括的一串字符组成。 单行字符串字面量被包在双引号中的一串字符组成,形式如下:
> "`字符`" > "`字符`"
>
字符串字面量中不能包含未转义的双引号(`"`)、未转义的反斜线(`\`)、回车符、换行符。 字符串字面量中不能包含未转义的双引号(`"`)、未转义的反斜线(`\`)、回车符、换行符。
多行字符串字面量被包在三个双引号中的一串字符组成,形式如下: 多行字符串字面量被包在三个双引号中的一串字符组成,形式如下:
> """ > """
>
> `字符` > `字符`
>
> """ > """
>
与单行字符串字面量不同的是,多行字符串字面量可以包含不转义的双引号("),回车以及换行。它不能包含三个非转义的连续双引号。 与单行字符串字面量不同的是,多行字符串字面量可以包含不转义的双引号("),回车以及换行。它不能包含三个非转义的连续双引号。
@ -286,27 +387,43 @@ let textB = "Hello world"
> >
<a id="string-literal"></a> <a id="string-literal"></a>
> *字符串字面量* → [*静态字符串字面量*](#static-string-literal) | [*插值字符串字面量*](#interpolated-string-literal) > *字符串字面量* → [*静态字符串字面量*](#static-string-literal) | [*插值字符串字面量*](#interpolated-string-literal)
>
<a id="static-string-literal"></a> <a id="static-string-literal"></a>
> *静态字符串字面量* → **"**[*引用文本*](#quoted-text)<sub>可选</sub>**"** > *静态字符串字面量* → **"**[*引用文本*](#quoted-text)<sub>可选</sub>**"**
>
> <a id="quoted-text"></a> > <a id="quoted-text"></a>
>
> *引用文本* → [*引用文本项*](#quoted-text-item) [*引用文本*](#quoted-text)<sub>可选</sub> > *引用文本* → [*引用文本项*](#quoted-text-item) [*引用文本*](#quoted-text)<sub>可选</sub>
>
> <a id="quoted-text-item"></a> > <a id="quoted-text-item"></a>
>
> *引用文本项* → [*转义字符*](#escaped-character) > *引用文本项* → [*转义字符*](#escaped-character)
>
> *引用文本项* → 除了 **"**、**\\**、U+000A、U+000D 以外的所有 Unicode 字符 > *引用文本项* → 除了 **"**、**\\**、U+000A、U+000D 以外的所有 Unicode 字符
>
<a id="interpolated-string-literal"></a> <a id="interpolated-string-literal"></a>
> *插值字符串字面量* → **"**[*插值文本*](#interpolated-text)<sub>可选</sub>**"** > *插值字符串字面量* → **"**[*插值文本*](#interpolated-text)<sub>可选</sub>**"**
>
> <a id="interpolated-text"></a> > <a id="interpolated-text"></a>
>
> *插值文本* → [*插值文本项*](#interpolated-text-item) [*插值文本*](#interpolated-text)<sub>可选</sub> > *插值文本* → [*插值文本项*](#interpolated-text-item) [*插值文本*](#interpolated-text)<sub>可选</sub>
>
> <a id="interpolated-text-item"></a> > <a id="interpolated-text-item"></a>
>
> *插值文本项* → **\\****(**[*表达式*](./04_Expressions.md)**)** | [*引用文本项*](#quoted-text-item) > *插值文本项* → **\\****(**[*表达式*](./04_Expressions.md)**)** | [*引用文本项*](#quoted-text-item)
>
<a id="escaped-character"></a> <a id="escaped-character"></a>
> *转义字符* → **\\****0** | **\\****\\** | **\t** | **\n** | **\r** | **\\"** | **\\'** > *转义字符* → **\\****0** | **\\****\\** | **\t** | **\n** | **\r** | **\\"** | **\\'**
>
> *转义字符* → **\u {** [*unicode 标量数字*](#unicode-scalar-digits) **}** > *转义字符* → **\u {** [*unicode 标量数字*](#unicode-scalar-digits) **}**
>
> <a id="unicode-scalar-digits"></a> > <a id="unicode-scalar-digits"></a>
>
> *unicode 标量数字* → 一到八位的十六进制数字 > *unicode 标量数字* → 一到八位的十六进制数字
>
<a id="operators"></a> <a id="operators"></a>
## 运算符 ## 运算符
@ -322,6 +439,7 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
> 注意 > 注意
> >
> 以下这些标记 `=`、`->`、`//`、`/*`、`*/`、`.`、`<`(前缀运算符)、`&`、`?`、`?`(中缀运算符)、`>`(后缀运算符)、`!` 、`?` 是被系统保留的。这些符号不能被重载,也不能用于自定义运算符。 > 以下这些标记 `=`、`->`、`//`、`/*`、`*/`、`.`、`<`(前缀运算符)、`&`、`?`、`?`(中缀运算符)、`>`(后缀运算符)、`!` 、`?` 是被系统保留的。这些符号不能被重载,也不能用于自定义运算符。
>
运算符两侧的空白被用来区分该运算符是否为前缀运算符、后缀运算符或二元运算符。规则总结如下: 运算符两侧的空白被用来区分该运算符是否为前缀运算符、后缀运算符或二元运算符。规则总结如下:
@ -342,46 +460,82 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
> >
<a id="operator"></a> <a id="operator"></a>
> *运算符* → [*头部运算符*](#operator-head) [*运算符字符组*](#operator-characters)<sub>可选</sub> > *运算符* → [*头部运算符*](#operator-head) [*运算符字符组*](#operator-characters)<sub>可选</sub>
>
> *运算符* → [*头部点运算符*](#dot-operator-head) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub> > *运算符* → [*头部点运算符*](#dot-operator-head) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub>
>
<a id="operator-head"></a> <a id="operator-head"></a>
> *头部运算符* → **/** | **=** | **-** | **+** | **!** | __*__ | **%** | **<** | **>** | **&** | **|** | **^** | **~** | **?** > *头部运算符* → **/** | **=** | **-** | **+** | **!** | __*__ | **%** | **<** | **>** | **&** | **|** | **^** | **~** | **?**
>
> *头部运算符* → U+00A1U+00A7 > *头部运算符* → U+00A1U+00A7
>
> *头部运算符* → U+00A9 或 U+00AB > *头部运算符* → U+00A9 或 U+00AB
>
> *头部运算符* → U+00AC 或 U+00AE > *头部运算符* → U+00AC 或 U+00AE
>
> *头部运算符* → U+00B0U+00B1U+00B6U+00BBU+00BFU+00D7或 U+00F7 > *头部运算符* → U+00B0U+00B1U+00B6U+00BBU+00BFU+00D7或 U+00F7
>
> *头部运算符* → U+2016U+2017 或 U+2020U+2027 > *头部运算符* → U+2016U+2017 或 U+2020U+2027
>
> *头部运算符* → U+2030U+203E > *头部运算符* → U+2030U+203E
>
> *头部运算符* → U+2041U+2053 > *头部运算符* → U+2041U+2053
>
> *头部运算符* → U+2055U+205E > *头部运算符* → U+2055U+205E
>
> *头部运算符* → U+2190U+23FF > *头部运算符* → U+2190U+23FF
>
> *头部运算符* → U+2500U+2775 > *头部运算符* → U+2500U+2775
>
> *头部运算符* → U+2794U+2BFF > *头部运算符* → U+2794U+2BFF
>
> *头部运算符* → U+2E00U+2E7F > *头部运算符* → U+2E00U+2E7F
>
> *头部运算符* → U+3001U+3003 > *头部运算符* → U+3001U+3003
>
> *头部运算符* → U+3008U+3030 > *头部运算符* → U+3008U+3030
>
<a id="operator-character"></a> <a id="operator-character"></a>
> *运算符字符* → [*头部运算符*](#operator-head) > *运算符字符* → [*头部运算符*](#operator-head)
>
> *运算符字符* → U+0300U+036F > *运算符字符* → U+0300U+036F
>
> *运算符字符* → U+1DC0U+1DFF > *运算符字符* → U+1DC0U+1DFF
>
> *运算符字符* → U+20D0U+20FF > *运算符字符* → U+20D0U+20FF
>
> *运算符字符* → U+FE00U+FE0F > *运算符字符* → U+FE00U+FE0F
>
> *运算符字符* → U+FE20U+FE2F > *运算符字符* → U+FE20U+FE2F
>
> *运算符字符* → U+E0100U+E01EF > *运算符字符* → U+E0100U+E01EF
>
> <a id="operator-characters"></a> > <a id="operator-characters"></a>
>
> *运算符字符组* → [*运算符字符*](#operator-character) [*运算符字符组*](#operator-characters)<sub>可选</sub> > *运算符字符组* → [*运算符字符*](#operator-character) [*运算符字符组*](#operator-characters)<sub>可选</sub>
>
<a id="dot-operator-head"></a> <a id="dot-operator-head"></a>
> *头部点运算符* → **..** > *头部点运算符* → **..**
>
> <a id="dot-operator-character"></a> > <a id="dot-operator-character"></a>
>
> *点运算符字符* → **.** | [*运算符字符*](#operator-character) > *点运算符字符* → **.** | [*运算符字符*](#operator-character)
>
> <a id="dot-operator-characters"></a> > <a id="dot-operator-characters"></a>
>
> *点运算符字符组* → [*点运算符字符*](#dot-operator-character) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub> > *点运算符字符组* → [*点运算符字符*](#dot-operator-character) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub>
>
<a id="binary-operator"></a> <a id="binary-operator"></a>
> *二元运算符* → [*运算符*](#operator) > *二元运算符* → [*运算符*](#operator)
>
> <a id="prefix-operator"></a> > <a id="prefix-operator"></a>
>
> *前缀运算符* → [*运算符*](#operator) > *前缀运算符* → [*运算符*](#operator)
>
> <a id="postfix-operator"></a> > <a id="postfix-operator"></a>
>
> *后缀运算符* → [*运算符*](#operator) > *后缀运算符* → [*运算符*](#operator)
>

View File

@ -29,17 +29,29 @@ Swift 语言存在两种类型:命名型类型和复合型类型。命名型
> >
<a name="type"></a> <a name="type"></a>
> *类型* → [*数组类型*](#array-type) > *类型* → [*数组类型*](#array-type)
>
> *类型* → [*字典类型*](#dictionary-type) > *类型* → [*字典类型*](#dictionary-type)
>
> *类型* → [*函数类型*](#function-type) > *类型* → [*函数类型*](#function-type)
>
> *类型* → [*类型标识*](#type-identifier) > *类型* → [*类型标识*](#type-identifier)
>
> *类型* → [*元组类型*](#tuple-type) > *类型* → [*元组类型*](#tuple-type)
>
> *类型* → [*可选类型*](#optional-type) > *类型* → [*可选类型*](#optional-type)
>
> *类型* → [*隐式解析可选类型*](#implicitly-unwrapped-optional-type) > *类型* → [*隐式解析可选类型*](#implicitly-unwrapped-optional-type)
>
> *类型* → [*协议合成类型*](#protocol-composition-type) > *类型* → [*协议合成类型*](#protocol-composition-type)
>
> *类型* → [*元型类型*](#metatype-type) > *类型* → [*元型类型*](#metatype-type)
>
> *类型* → **任意类型** > *类型* → **任意类型**
>
> *类型* → **自身类型** > *类型* → **自身类型**
>
> *类型* → [*(类型)*](#type) > *类型* → [*(类型)*](#type)
>
<a name="type_annotation"></a> <a name="type_annotation"></a>
## 类型注解 ## 类型注解
@ -59,6 +71,7 @@ func someFunction(a: Int) { /* ... */ }
> >
<a name="type-annotation"></a> <a name="type-annotation"></a>
> *类型注解* → **:** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type) > *类型注解* → **:** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type)
>
<a name="type_identifier"></a> <a name="type_identifier"></a>
## 类型标识符 ## 类型标识符
@ -84,8 +97,10 @@ var someValue: ExampleModule.MyType
> >
<a name="type-identifier"></a> <a name="type-identifier"></a>
> *类型标识符* → [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.html#generic_argument_clause)<sub>可选</sub> | [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.html#generic_argument_clause)<sub>可选</sub> **.** [*类型标识符*](#type-identifier) > *类型标识符* → [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.html#generic_argument_clause)<sub>可选</sub> | [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.html#generic_argument_clause)<sub>可选</sub> **.** [*类型标识符*](#type-identifier)
>
<a name="type-name"></a> <a name="type-name"></a>
> *类型名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *类型名称* → [*标识符*](02_Lexical_Structure.html#identifier)
>
<a name="tuple_type"></a> <a name="tuple_type"></a>
## 元组类型 ## 元组类型
@ -109,12 +124,16 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配
> >
<a name="tuple-type"></a> <a name="tuple-type"></a>
> *元组类型* → **(** [*元组类型元素列表*](#tuple-type-element-list) <sub>可选</sub> **)** > *元组类型* → **(** [*元组类型元素列表*](#tuple-type-element-list) <sub>可选</sub> **)**
>
<a name="tuple-type-element-list"></a> <a name="tuple-type-element-list"></a>
> *元组类型元素列表* → [*元组类型元素*](#tuple-type-element) | [*元组类型元素*](#tuple-type-element) **,** [*元组类型元素列表*](#tuple-type-element-list) > *元组类型元素列表* → [*元组类型元素*](#tuple-type-element) | [*元组类型元素*](#tuple-type-element) **,** [*元组类型元素列表*](#tuple-type-element-list)
>
<a name="tuple-type-element"></a> <a name="tuple-type-element"></a>
> *元组类型元素* → [*元素名*](#element-name) [*类型注解*](#type-annotation) | [*类型*](#type) > *元组类型元素* → [*元素名*](#element-name) [*类型注解*](#type-annotation) | [*类型*](#type)
>
<a name="element-name"></a> <a name="element-name"></a>
> *元素名* → [*标识符*](02_Lexical_Structure.html#identifier) > *元素名* → [*标识符*](02_Lexical_Structure.html#identifier)
>
<a name="function_type"></a> <a name="function_type"></a>
## 函数类型 ## 函数类型
@ -122,16 +141,19 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配
函数类型表示一个函数、方法或闭包的类型,它由参数类型和返回值类型组成,中间用箭头(`->`)隔开: 函数类型表示一个函数、方法或闭包的类型,它由参数类型和返回值类型组成,中间用箭头(`->`)隔开:
> `参数类型` -> `返回值类型` > `参数类型` -> `返回值类型`
>
参数类型是由逗号间隔的类型列表。由于参数类型和返回值类型可以是元组类型,所以函数类型支持多参数与多返回值的函数与方法。 参数类型是由逗号间隔的类型列表。由于参数类型和返回值类型可以是元组类型,所以函数类型支持多参数与多返回值的函数与方法。
你可以对函数参数 `() -> T`(其中 T 是任何类型)使用 `autoclosure` 特性。这会自动将参数表达式转化为闭包,表达式的结果即闭包返回值。这从语法结构上提供了一种便捷:延迟对表达式的求值,直到其值在函数体中被调用。以自动闭包做为参数的函数类型的例子详见 [自动闭包](../chapter2/07_Closures.html#autoclosures) 。 你可以对函数参数 `() -> T`(其中 T 是任何类型)使用 `autoclosure` 特性。这会自动将参数表达式转化为闭包,表达式的结果即闭包返回值。这从语法结构上提供了一种便捷:延迟对表达式的求值,直到其值在函数体中被调用。以自动闭包做为参数的函数类型的例子详见 [自动闭包](../chapter2/07_Closures.html#autoclosures) 。
>
函数类型可以拥有一个可变长参数作为参数类型中的最后一个参数。从语法角度上讲,可变长参数由一个基础类型名字紧随三个点(`...`)组成,如 `Int...`。可变长参数被认为是一个包含了基础类型元素的数组。即 `Int...` 就是 `[Int]`。关于使用可变长参数的例子,请参阅 [可变参数](../chapter2/06_Functions.html#variadic_parameters)。 函数类型可以拥有一个可变长参数作为参数类型中的最后一个参数。从语法角度上讲,可变长参数由一个基础类型名字紧随三个点(`...`)组成,如 `Int...`。可变长参数被认为是一个包含了基础类型元素的数组。即 `Int...` 就是 `[Int]`。关于使用可变长参数的例子,请参阅 [可变参数](../chapter2/06_Functions.html#variadic_parameters)。
为了指定一个 `in-out` 参数,可以在参数类型前加 `inout` 前缀。但是你不可以对可变长参数或返回值类型使用 `inout`。关于这种参数的详细讲解请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。 为了指定一个 `in-out` 参数,可以在参数类型前加 `inout` 前缀。但是你不可以对可变长参数或返回值类型使用 `inout`。关于这种参数的详细讲解请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。
如果一个函数类型只有一个形式参数而且形式参数的类型是元组类型,那么元组类型在写函数类型的时候必须用圆括号括起来。比如说,`((Int, Int)) -> Void` 是接收一个元组 `(Int, Int)` 作为形式参数的函数的类型。与此相反,不加括号的 `(Int, Int) -> Void` 是一个接收两个 `Int` 形式参数并且不返回任何值的函数的类型。相似地,因为 `Void` 是空元组类型 `()` 的别名, 函数类型 `(Void)-> Void` 与一个空元组的变量的函数类型 `(()) -> ()` 是一样的。但这些类型和无变量的函数类型 `() -> ()` 是不一样的。 如果一个函数类型只有一个形式参数而且形式参数的类型是元组类型,那么元组类型在写函数类型的时候必须用圆括号括起来。比如说,`((Int, Int)) -> Void` 是接收一个元组 `(Int, Int)` 作为形式参数的函数的类型。与此相反,不加括号的 `(Int, Int) -> Void` 是一个接收两个 `Int` 形式参数并且不返回任何值的函数的类型。相似地,因为 `Void` 是空元组类型 `()` 的别名, 函数类型 `(Void)-> Void` 与一个空元组的变量的函数类型 `(()) -> ()` 是一样的。但这些类型和无变量的函数类型 `() -> ()` 是不一样的。
>
函数和方法中的变量名并不是函数类型的一部分。例如: 函数和方法中的变量名并不是函数类型的一部分。例如:
@ -141,6 +163,7 @@ func anotherFunction(left: Int, right: Int) {}
func functionWithDifferentLabels(top: Int, bottom: Int) {} func functionWithDifferentLabels(top: Int, bottom: Int) {}
var f = someFunction // 函数 f 的类型为 (Int, Int) -> Void, 而不是 (left: Int, right: Int) -> Void. var f = someFunction // 函数 f 的类型为 (Int, Int) -> Void, 而不是 (left: Int, right: Int) -> Void.
>
f = anotherFunction // 正确 f = anotherFunction // 正确
f = functionWithDifferentLabels // 正确 f = functionWithDifferentLabels // 正确
@ -155,11 +178,15 @@ f = functionWithDifferentNumberOfArguments // 错误
```swift ```swift
var operation: (lhs: Int, rhs: Int) -> Int // 错误 var operation: (lhs: Int, rhs: Int) -> Int // 错误
>
var operation: (_ lhs: Int, _ rhs: Int) -> Int // 正确 var operation: (_ lhs: Int, _ rhs: Int) -> Int // 正确
>
var operation: (Int, Int) -> Int // 正确 var operation: (Int, Int) -> Int // 正确
>
``` ```
如果一个函数类型包涵多个箭头(->),那么函数类型将从右向左进行组合。例如,函数类型 `Int -> Int -> Int` 可以理解为 `Int -> (Int -> Int)`,也就是说,该函数类型的参数为 `Int` 类型,其返回类型是一个参数类型为 `Int`,返回类型为 `Int` 的函数类型。 如果一个函数类型包涵多个箭头(->),那么函数类型将从右向左进行组合。例如,函数类型 `Int -> Int -> Int` 可以理解为 `Int -> (Int -> Int)`,也就是说,该函数类型的参数为 `Int` 类型,其返回类型是一个参数类型为 `Int`,返回类型为 `Int` 的函数类型。
>
函数类型若要抛出错误就必须使用 `throws` 关键字来标记,若要重抛错误则必须使用 `rethrows` 关键字来标记。`throws` 关键字是函数类型的一部分,非抛出函数是抛出函数函数的一个子类型。因此,在使用抛出函数的地方也可以使用不抛出函数。抛出和重抛函数的相关描述见章节 [抛出函数与方法](05_Declarations.html#throwing_functions_and_methods) 和 [重抛函数与方法](05_Declarations.html#rethrowing_functions_and_methods)。 函数类型若要抛出错误就必须使用 `throws` 关键字来标记,若要重抛错误则必须使用 `rethrows` 关键字来标记。`throws` 关键字是函数类型的一部分,非抛出函数是抛出函数函数的一个子类型。因此,在使用抛出函数的地方也可以使用不抛出函数。抛出和重抛函数的相关描述见章节 [抛出函数与方法](05_Declarations.html#throwing_functions_and_methods) 和 [重抛函数与方法](05_Declarations.html#rethrowing_functions_and_methods)。
@ -169,7 +196,9 @@ var operation: (Int, Int) -> Int // 正确
```swift ```swift
let external: (Any) -> Void = { _ in () } let external: (Any) -> Void = { _ in () }
>
func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) { func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) {
>
first(first) // 错误 first(first) // 错误
second(second) // 错误 second(second) // 错误
@ -192,16 +221,23 @@ func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) {
> >
<a name="function-type"></a> <a name="function-type"></a>
> *函数类型* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*函数类型子句*](#function-type-argument-clause) **throws**<sub>可选</sub> **->** [*类型*](#type) > *函数类型* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*函数类型子句*](#function-type-argument-clause) **throws**<sub>可选</sub> **->** [*类型*](#type)
>
> *函数类型* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*函数类型子句*](#function-type-argument-clause) **rethrows­** **->** [*类型*](#type) > *函数类型* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*函数类型子句*](#function-type-argument-clause) **rethrows­** **->** [*类型*](#type)
>
<a name="function-type-argument-clause"></a> <a name="function-type-argument-clause"></a>
> *函数类型子句* → (­)­ > *函数类型子句* → (­)­
>
> *函数类型子句* → ([*函数类型参数列表*](#function-type-argument-list)*...*­<sub>可选</sub>)­ > *函数类型子句* → ([*函数类型参数列表*](#function-type-argument-list)*...*­<sub>可选</sub>)­
>
<a name="function-type-argument-list"></a> <a name="function-type-argument-list"></a>
> *函数类型参数列表* → [*函数类型参数*](function-type-argument) | [*函数类型参数*](function-type-argument) [*函数类型参数列表*](#function-type-argument-list) > *函数类型参数列表* → [*函数类型参数*](function-type-argument) | [*函数类型参数*](function-type-argument) [*函数类型参数列表*](#function-type-argument-list)
>
<a name="function-type-argument"></a> <a name="function-type-argument"></a>
> *函数类型参数* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type) | [*参数标签*](#argument-label) [*类型注解*](#type-annotation) > *函数类型参数* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type) | [*参数标签*](#argument-label) [*类型注解*](#type-annotation)
>
<a name="argument-label"></a> <a name="argument-label"></a>
> *参数标签* → [*标识符*](02_Lexical_Structure.html#identifier) > *参数标签* → [*标识符*](02_Lexical_Structure.html#identifier)
>
<a name="array_type"></a> <a name="array_type"></a>
## 数组类型 ## 数组类型
@ -209,11 +245,13 @@ func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) {
Swift 语言为标准库中定义的 `Array<Element>` 类型提供了如下语法糖: Swift 语言为标准库中定义的 `Array<Element>` 类型提供了如下语法糖:
> [`类型`] > [`类型`]
>
换句话说,下面两个声明是等价的: 换句话说,下面两个声明是等价的:
```swift ```swift
let someArray: Array<String> = ["Alex", "Brian", "Dave"] let someArray: Array<String> = ["Alex", "Brian", "Dave"]
>
let someArray: [String] = ["Alex", "Brian", "Dave"] let someArray: [String] = ["Alex", "Brian", "Dave"]
``` ```
@ -233,6 +271,7 @@ var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
> >
<a name="array-type"></a> <a name="array-type"></a>
> *数组类型* → **[** [*类型*](#type) **]** > *数组类型* → **[** [*类型*](#type) **]**
>
<a name="dictionary_type"></a> <a name="dictionary_type"></a>
## 字典类型 ## 字典类型
@ -240,12 +279,14 @@ var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
Swift 语言为标准库中定义的 `Dictionary<Key, Value>` 类型提供了如下语法糖: Swift 语言为标准库中定义的 `Dictionary<Key, Value>` 类型提供了如下语法糖:
> [`键类型` : `值类型`] > [`键类型` : `值类型`]
>
换句话说,下面两个声明是等价的: 换句话说,下面两个声明是等价的:
```swift ```swift
let someDictionary: [String: Int] = ["Alex": 31, "Paul": 39] let someDictionary: [String: Int] = ["Alex": 31, "Paul": 39]
let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39] let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39]
>
``` ```
上面两种情况,常量 `someDictionary` 被声明为一个字典,其中键为 `String` 类型,值为 `Int` 类型。 上面两种情况,常量 `someDictionary` 被声明为一个字典,其中键为 `String` 类型,值为 `Int` 类型。
@ -260,6 +301,7 @@ let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39]
> >
<a name="dictionary-type"></a> <a name="dictionary-type"></a>
> *字典类型* → **[** [*类型*](#type) **:** [*类型*](#type) **]** > *字典类型* → **[** [*类型*](#type) **:** [*类型*](#type) **]**
>
<a name="optional_type"></a> <a name="optional_type"></a>
## 可选类型 ## 可选类型
@ -292,6 +334,7 @@ optionalInteger! // 42
> >
<a name="optional-type"></a> <a name="optional-type"></a>
> *可选类型* → [*类型*](#type) **?** > *可选类型* → [*类型*](#type) **?**
>
<a name="implicitly_unwrapped_optional_type"></a> <a name="implicitly_unwrapped_optional_type"></a>
## 隐式解析可选类型 ## 隐式解析可选类型
@ -327,6 +370,7 @@ let implicitlyUnwrappedArray: [Int]! // 正确
> >
<a name="implicitly-unwrapped-optional-type"></a> <a name="implicitly-unwrapped-optional-type"></a>
> *隐式解析可选类型* → [*类型*](#type) **!** > *隐式解析可选类型* → [*类型*](#type) **!**
>
<a name="protocol_composition_type"></a> <a name="protocol_composition_type"></a>
## 协议合成类型 ## 协议合成类型
@ -336,6 +380,7 @@ let implicitlyUnwrappedArray: [Int]! // 正确
协议合成类型的形式如下: 协议合成类型的形式如下:
> `Protocol 1` & `Procotol 2` > `Protocol 1` & `Procotol 2`
>
协议合成类型允许你指定一个值,其类型符合多个协议的要求且不需要定义一个新的命名型协议来继承它想要符合的各个协议。比如,协议合成类型 `Protocol A & Protocol B & Protocol C` 等效于一个从 `Protocol A``Protocol B``Protocol C` 继承而来的新协议 `Protocol D`,很显然这样做有效率的多,甚至不需引入一个新名字。 协议合成类型允许你指定一个值,其类型符合多个协议的要求且不需要定义一个新的命名型协议来继承它想要符合的各个协议。比如,协议合成类型 `Protocol A & Protocol B & Protocol C` 等效于一个从 `Protocol A``Protocol B``Protocol C` 继承而来的新协议 `Protocol D`,很显然这样做有效率的多,甚至不需引入一个新名字。
@ -345,10 +390,13 @@ let implicitlyUnwrappedArray: [Int]! // 正确
> >
<a name="protocol-composition-type"></a> <a name="protocol-composition-type"></a>
> *协议合成类型* → [*协议标识符*](#protocol-identifier) & [*协议合成延续*](#protocol-composition-continuation) > *协议合成类型* → [*协议标识符*](#protocol-identifier) & [*协议合成延续*](#protocol-composition-continuation)
>
<a name="protocol-composition-continuation"></a> <a name="protocol-composition-continuation"></a>
> *协议合成延续* → [*协议标识符*](#protocol-identifier) | [*协议合成类型*](#protocol-composition-type) > *协议合成延续* → [*协议标识符*](#protocol-identifier) | [*协议合成类型*](#protocol-composition-type)
>
<a name="protocol-identifier"></a> <a name="protocol-identifier"></a>
> *协议标识符* → [*类型标识符*](#type-identifier) > *协议标识符* → [*类型标识符*](#type-identifier)
>
<a name="metatype_type"></a> <a name="metatype_type"></a>
## 元类型 ## 元类型
@ -399,6 +447,7 @@ let anotherInstance = metatype.init(string: "some string")
> >
<a name="metatype-type"></a> <a name="metatype-type"></a>
> *元类型* → [*类型*](#type) **.** **Type** | [*类型*](#type) **.** **Protocol** > *元类型* → [*类型*](#type) **.** **Type** | [*类型*](#type) **.** **Protocol**
>
<a name="type_inheritance_clause"></a> <a name="type_inheritance_clause"></a>
## 类型继承子句 ## 类型继承子句
@ -415,8 +464,10 @@ let anotherInstance = metatype.init(string: "some string")
> >
<a name="type_inheritance_clause"></a> <a name="type_inheritance_clause"></a>
> *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list) > *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list)
>
<a name="type-inheritance-list"></a> <a name="type-inheritance-list"></a>
> *类型继承列表* → [*类型标识符*](#type-identifier) | [*类型标识符*](#type-identifier) **,** [*类型继承列表*](#type-inheritance-list) > *类型继承列表* → [*类型标识符*](#type-identifier) | [*类型标识符*](#type-identifier) **,** [*类型继承列表*](#type-inheritance-list)
>
<a name="class-requirement"></a> <a name="class-requirement"></a>

View File

@ -8,8 +8,10 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
> >
<a name="expression"></a> <a name="expression"></a>
> *表达式* → [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub> > *表达式* → [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
>
<a name="expression-list"></a> <a name="expression-list"></a>
> *表达式列表* → [*表达式*](#expression) | [*表达式*](#expression) **,** [*表达式列表*](#expression-list) > *表达式列表* → [*表达式*](#expression) | [*表达式*](#expression) **,** [*表达式列表*](#expression-list)
>
<a name="prefix_expressions"></a> <a name="prefix_expressions"></a>
## 前缀表达式 ## 前缀表达式
@ -26,9 +28,12 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
> >
<a name="prefix-expression"></a> <a name="prefix-expression"></a>
> *前缀表达式* → [*前缀运算符*](02_Lexical_Structure.md#prefix-operator)<sub>可选</sub> [*后缀表达式*](#postfix-expression) > *前缀表达式* → [*前缀运算符*](02_Lexical_Structure.md#prefix-operator)<sub>可选</sub> [*后缀表达式*](#postfix-expression)
>
> *前缀表达式* → [*输入输出表达式*](#in-out-expression) > *前缀表达式* → [*输入输出表达式*](#in-out-expression)
>
<a name="in-out-expression"></a> <a name="in-out-expression"></a>
> *输入输出表达式* → **&** [*标识符*](02_Lexical_Structure.md#identifier) > *输入输出表达式* → **&** [*标识符*](02_Lexical_Structure.md#identifier)
>
<a name="try_operator"></a> <a name="try_operator"></a>
### Try 运算符 ### Try 运算符
@ -36,16 +41,19 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
try 表达式由 `try` 运算符加上紧随其后的可抛出错误的表达式组成,形式如下: try 表达式由 `try` 运算符加上紧随其后的可抛出错误的表达式组成,形式如下:
> try `可抛出错误的表达式` > try `可抛出错误的表达式`
>
可选的 try 表达式由 `try?` 运算符加上紧随其后的可抛出错误的表达式组成,形式如下: 可选的 try 表达式由 `try?` 运算符加上紧随其后的可抛出错误的表达式组成,形式如下:
> try? `可抛出错误的表达式` > try? `可抛出错误的表达式`
>
如果可抛出错误的表达式没有抛出错误,整个表达式返回的可选值将包含可抛出错误的表达式的返回值,否则,该可选值为 `nil` 如果可抛出错误的表达式没有抛出错误,整个表达式返回的可选值将包含可抛出错误的表达式的返回值,否则,该可选值为 `nil`
强制的 try 表达式由 `try!` 运算符加上紧随其后的可抛出错误的表达式组成,形式如下: 强制的 try 表达式由 `try!` 运算符加上紧随其后的可抛出错误的表达式组成,形式如下:
> try! `可抛出错误的表达式` > try! `可抛出错误的表达式`
>
如果可抛出错误的表达式抛出了错误,将会引发运行时错误。 如果可抛出错误的表达式抛出了错误,将会引发运行时错误。
@ -64,6 +72,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
> >
<a name="try-operator"></a> <a name="try-operator"></a>
> *try 运算符* → **try** | **try?** | **try!** > *try 运算符* → **try** | **try?** | **try!**
>
<a name="binary_expressions"></a> <a name="binary_expressions"></a>
## 二元表达式 ## 二元表达式
@ -71,6 +80,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
*二元表达式*由中缀运算符和左右参数表达式组成。形式如下: *二元表达式*由中缀运算符和左右参数表达式组成。形式如下:
> `左侧参数` `二元运算符` `右侧参数` > `左侧参数` `二元运算符` `右侧参数`
>
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.html) 和 [高级运算符](../chapter2/26_Advanced_Operators.html)。 关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.html) 和 [高级运算符](../chapter2/26_Advanced_Operators.html)。
@ -79,16 +89,22 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
> 注意 > 注意
> >
> 在解析时,一个二元表达式将作为一个扁平列表表示,然后根据运算符的优先级,再进一步进行组合。例如,`2 + 3 * 5` 首先被看作具有五个元素的列表,即 `2`、`+`、`3`、`*`、`5`,随后根据运算符优先级组合为 `(2 + (3 * 5))`。 > 在解析时,一个二元表达式将作为一个扁平列表表示,然后根据运算符的优先级,再进一步进行组合。例如,`2 + 3 * 5` 首先被看作具有五个元素的列表,即 `2`、`+`、`3`、`*`、`5`,随后根据运算符优先级组合为 `(2 + (3 * 5))`。
>
<a name="binary-expression"></a> <a name="binary-expression"></a>
> 二元表达式语法 > 二元表达式语法
> >
> *二元表达式* → [*二元运算符*](02_Lexical_Structure.md#binary-operator) [*前缀表达式*](#prefix-expression) > *二元表达式* → [*二元运算符*](02_Lexical_Structure.md#binary-operator) [*前缀表达式*](#prefix-expression)
>
> *二元表达式* → [*赋值运算符*](#assignment-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) > *二元表达式* → [*赋值运算符*](#assignment-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression)
>
> *二元表达式* → [*条件运算符*](#conditional-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) > *二元表达式* → [*条件运算符*](#conditional-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression)
>
> *二元表达式* → [*类型转换运算符*](#type-casting-operator) > *二元表达式* → [*类型转换运算符*](#type-casting-operator)
>
<a name="binary-expressions"></a> <a name="binary-expressions"></a>
> *二元表达式列表* → [*二元表达式*](#binary-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub> > *二元表达式列表* → [*二元表达式*](#binary-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
>
<a name="assignment_operator"></a> <a name="assignment_operator"></a>
### 赋值表达式 ### 赋值表达式
@ -96,6 +112,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
赋值表达式会为某个给定的表达式赋值,形式如下; 赋值表达式会为某个给定的表达式赋值,形式如下;
> `表达式` = `值` > `表达式` = `值`
>
右边的值会被赋值给左边的表达式。如果左边表达式是一个元组,那么右边必须是一个具有同样元素个数的元组。(嵌套元组也是允许的。)右边的值中的每一部分都会被赋值给左边的表达式中的相应部分。例如: 右边的值会被赋值给左边的表达式。如果左边表达式是一个元组,那么右边必须是一个具有同样元素个数的元组。(嵌套元组也是允许的。)右边的值中的每一部分都会被赋值给左边的表达式中的相应部分。例如:
@ -110,6 +127,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
> >
<a name="assignment-operator"></a> <a name="assignment-operator"></a>
> *赋值运算符* → **=** > *赋值运算符* → **=**
>
<a name="ternary_conditional_operator"></a> <a name="ternary_conditional_operator"></a>
### 三元条件运算符 ### 三元条件运算符
@ -117,6 +135,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
*三元条件运算符*会根据条件来对两个给定表达式中的一个进行求值,形式如下: *三元条件运算符*会根据条件来对两个给定表达式中的一个进行求值,形式如下:
> `条件` ? `表达式(条件为真则使用)` : `表达式(条件为假则使用)` > `条件` ? `表达式(条件为真则使用)` : `表达式(条件为假则使用)`
>
如果条件为真,那么对第一个表达式进行求值并返回结果。否则,对第二个表达式进行求值并返回结果。未使用的表达式不会进行求值。 如果条件为真,那么对第一个表达式进行求值并返回结果。否则,对第二个表达式进行求值并返回结果。未使用的表达式不会进行求值。
@ -126,6 +145,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
> >
<a name="conditional-operator"></a> <a name="conditional-operator"></a>
> *三元条件运算符* → **?** [*表达式*](#expression) **:** > *三元条件运算符* → **?** [*表达式*](#expression) **:**
>
<a name="type-casting_operators"></a> <a name="type-casting_operators"></a>
### 类型转换运算符 ### 类型转换运算符
@ -139,6 +159,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
> `表达式` as? `类型` > `表达式` as? `类型`
> >
> `表达式` as! `类型` > `表达式` as! `类型`
>
`is` 运算符在运行时检查表达式能否向下转化为指定的类型,如果可以则返回 `ture`,否则返回 `false` `is` 运算符在运行时检查表达式能否向下转化为指定的类型,如果可以则返回 `ture`,否则返回 `false`
@ -171,9 +192,13 @@ f(x as Any)
> 类型转换运算符语法 > 类型转换运算符语法
> >
> *类型转换运算符* → **is** [*类型*](03_Types.md#type) > *类型转换运算符* → **is** [*类型*](03_Types.md#type)
>
> *类型转换运算符* → **as** [*类型*](03_Types.md#type) > *类型转换运算符* → **as** [*类型*](03_Types.md#type)
>
> *类型转换运算符* → **as** **?** [*类型*](03_Types.md#type) > *类型转换运算符* → **as** **?** [*类型*](03_Types.md#type)
>
> *类型转换运算符* → **as** **!** [*类型*](03_Types.md#type) > *类型转换运算符* → **as** **!** [*类型*](03_Types.md#type)
>
<a name="primary_expressions"></a> <a name="primary_expressions"></a>
## 基本表达式 ## 基本表达式
@ -184,15 +209,25 @@ f(x as Any)
> >
<a name="primary-expression"></a> <a name="primary-expression"></a>
> *基本表达式* → [*标识符*](02_Lexical_Structure.md#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub> > *基本表达式* → [*标识符*](02_Lexical_Structure.md#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub>
>
> *基本表达式* → [*字面量表达式*](#literal-expression) > *基本表达式* → [*字面量表达式*](#literal-expression)
>
> *基本表达式* → [*self 表达式*](#self-expression) > *基本表达式* → [*self 表达式*](#self-expression)
>
> *基本表达式* → [*父类表达式*](#superclass-expression) > *基本表达式* → [*父类表达式*](#superclass-expression)
>
> *基本表达式* → [*闭包表达式*](#closure-expression) > *基本表达式* → [*闭包表达式*](#closure-expression)
>
> *基本表达式* → [*圆括号表达式*](#parenthesized-expression) > *基本表达式* → [*圆括号表达式*](#parenthesized-expression)
>
> *基本表达式* → [*隐式成员表达式*](#implicit-member-expression) > *基本表达式* → [*隐式成员表达式*](#implicit-member-expression)
>
> *基本表达式* → [*通配符表达式*](#wildcard-expression) > *基本表达式* → [*通配符表达式*](#wildcard-expression)
>
> *基本表达式* → [*选择器表达式*](#selector-expression) > *基本表达式* → [*选择器表达式*](#selector-expression)
>
> *基本表达式* → [*key-path字符串表达式*](#key-patch-string-expression) > *基本表达式* → [*key-path字符串表达式*](#key-patch-string-expression)
>
<a name="literal_expression"></a> <a name="literal_expression"></a>
### 字面量表达式 ### 字面量表达式
@ -224,6 +259,7 @@ myFunction() // 打印“myFunction()”
数组字面量是值的有序集合,形式如下: 数组字面量是值的有序集合,形式如下:
> [`值 1`, `值 2`, `...`] > [`值 1`, `值 2`, `...`]
>
数组中的最后一个表达式可以紧跟一个逗号。数组字面量的类型是 `[T]`,这个 `T` 就是数组中元素的类型。如果数组中包含多种类型,`T` 则是跟这些类型最近的的公共父类型。空数组字面量由一组方括号定义,可用来创建特定类型的空数组。 数组中的最后一个表达式可以紧跟一个逗号。数组字面量的类型是 `[T]`,这个 `T` 就是数组中元素的类型。如果数组中包含多种类型,`T` 则是跟这些类型最近的的公共父类型。空数组字面量由一组方括号定义,可用来创建特定类型的空数组。
@ -234,6 +270,7 @@ var emptyArray: [Double] = []
字典字面量是一个包含无序键值对的集合,形式如下: 字典字面量是一个包含无序键值对的集合,形式如下:
> [`键 1` : `值 1`, `键 2` : `值 2`, `...`] > [`键 1` : `值 1`, `键 2` : `值 2`, `...`]
>
字典中的最后一个表达式可以紧跟一个逗号。字典字面量的类型是 `[Key : Value]``Key` 表示键的类型,`Value` 表示值的类型。如果字典中包含多种类型,那么 `Key` 表示的类型则为所有键最接近的公共父类型,`Value` 与之相似。一个空的字典字面量由方括号中加一个冒号组成(`[:]`),从而与空数组字面量区分开,可以使用空字典字面量来创建特定类型的字典。 字典中的最后一个表达式可以紧跟一个逗号。字典字面量的类型是 `[Key : Value]``Key` 表示键的类型,`Value` 表示值的类型。如果字典中包含多种类型,那么 `Key` 表示的类型则为所有键最接近的公共父类型,`Value` 与之相似。一个空的字典字面量由方括号中加一个冒号组成(`[:]`),从而与空数组字面量区分开,可以使用空字典字面量来创建特定类型的字典。
@ -246,40 +283,66 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者
更多关于在 Xcode 中使用 playground 字面量的信息,请参阅 [添加颜色、文件或图片字面量](https://help.apple.com/xcode/mac/current/#/dev4c60242fc) 更多关于在 Xcode 中使用 playground 字面量的信息,请参阅 [添加颜色、文件或图片字面量](https://help.apple.com/xcode/mac/current/#/dev4c60242fc)
> 字面量表达式语法 > 字面量表达式语法
>
> >
> <a name="literal-expression"></a> > <a name="literal-expression"></a>
>
> *字面量表达式* → [*字面量*](02_Lexical_Structure.md#literal) > *字面量表达式* → [*字面量*](02_Lexical_Structure.md#literal)
>
> *字面量表达式* → [*数组字面量*](#array-literal) | [*字典字面量*](#dictionary-literal) | [*练习场字面量*](#playground-literal) > *字面量表达式* → [*数组字面量*](#array-literal) | [*字典字面量*](#dictionary-literal) | [*练习场字面量*](#playground-literal)
>
> *字面量表达式* → **#file** | **#line** | **#column** | **#function** > *字面量表达式* → **#file** | **#line** | **#column** | **#function**
>
> <a name="array-literal"></a> > <a name="array-literal"></a>
>
> *数组字面量* → [[*数组字面量项列表*](#array-literal-items)<sub>可选</sub> **]** > *数组字面量* → [[*数组字面量项列表*](#array-literal-items)<sub>可选</sub> **]**
>
> <a name="array-literal-items"></a> > <a name="array-literal-items"></a>
>
> *数组字面量项列表* → [*数组字面量项*](#array-literal-item) **,**<sub>可选</sub> | [*数组字面量项*](#array-literal-item) **,** [*数组字面量项列表*](#array-literal-items) > *数组字面量项列表* → [*数组字面量项*](#array-literal-item) **,**<sub>可选</sub> | [*数组字面量项*](#array-literal-item) **,** [*数组字面量项列表*](#array-literal-items)
>
> <a name="array-literal-item"></a> > <a name="array-literal-item"></a>
>
> *数组字面量项* → [*表达式*](#expression) > *数组字面量项* → [*表达式*](#expression)
>
> >
> <a name="dictionary-literal"></a> > <a name="dictionary-literal"></a>
>
> *字典字面量* → [[*字典字面量项列表*](#dictionary-literal-items) **]** | **[** **:** **]** > *字典字面量* → [[*字典字面量项列表*](#dictionary-literal-items) **]** | **[** **:** **]**
>
> <a name="dictionary-literal-items"></a> > <a name="dictionary-literal-items"></a>
>
> *字典字面量项列表* → [*字典字面量项*](#dictionary-literal-item) **,**<sub>可选</sub> | [*字典字面量项*](#dictionary-literal-item) **,** [*字典字面量项列表*](#dictionary-literal-items) > *字典字面量项列表* → [*字典字面量项*](#dictionary-literal-item) **,**<sub>可选</sub> | [*字典字面量项*](#dictionary-literal-item) **,** [*字典字面量项列表*](#dictionary-literal-items)
>
> <a name="dictionary-literal-item"></a> > <a name="dictionary-literal-item"></a>
>
> *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)。 > *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)。
>
> <a name="playground-literal"></a> > <a name="playground-literal"></a>
>
> *playground 字面量* → **#colorLiteral ( red : [*表达式*](#expression) , green :[*表达式*](#expression) [*表达式*](#e[*表达式*](#expression) xpression) , blue :[*表达式*](#expression) , alpha : [*表达式*](#expression) )** > *playground 字面量* → **#colorLiteral ( red : [*表达式*](#expression) , green :[*表达式*](#expression) [*表达式*](#e[*表达式*](#expression) xpression) , blue :[*表达式*](#expression) , alpha : [*表达式*](#expression) )**
>
> *playground 字面量* → **#fileLiteral ( resourceName : [*表达式*](#expression) )** > *playground 字面量* → **#fileLiteral ( resourceName : [*表达式*](#expression) )**
>
> playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**<a name="self_expression"></a> > playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**<a name="self_expression"></a>
>
### Self 表达式 ### Self 表达式
`self` 表达式是对当前类型或者当前实例的显式引用,它有如下形式: `self` 表达式是对当前类型或者当前实例的显式引用,它有如下形式:
> self > self
>
> self.`成员名称` > self.`成员名称`
>
> self[`下标索引`] > self[`下标索引`]
>
> self(`构造器参数`) > self(`构造器参数`)
>
> self.init(`构造器参数`) > self.init(`构造器参数`)
>
如果在构造器、下标、实例方法中,`self` 引用的是当前类型的实例。在一个类型方法中,`self` 引用的是当前的类型。 如果在构造器、下标、实例方法中,`self` 引用的是当前类型的实例。在一个类型方法中,`self` 引用的是当前的类型。
@ -309,13 +372,17 @@ struct Point {
> >
<a name="self-expression"></a> <a name="self-expression"></a>
> *self 表达式* → **self** | [*self 方法表达式*](#self-method-expression) [*self 下标表达式*](#self-subscript-expression) | [*self 构造器表达式*](#self-initializer-expression) > *self 表达式* → **self** | [*self 方法表达式*](#self-method-expression) [*self 下标表达式*](#self-subscript-expression) | [*self 构造器表达式*](#self-initializer-expression)
>
> >
<a name="self-method-expression"></a> <a name="self-method-expression"></a>
> *self 方法表达式* → **self** **.** [*标识符*](02_Lexical_Structure.md#identifier) > *self 方法表达式* → **self** **.** [*标识符*](02_Lexical_Structure.md#identifier)
>
<a name="self-subscript-expression"></a> <a name="self-subscript-expression"></a>
> *self 下标表达式* → **self** **[** [*函数调用参数表*](#function-call-argument-list­) **]** > *self 下标表达式* → **self** **[** [*函数调用参数表*](#function-call-argument-list­) **]**
>
<a name="self-initializer-expression"></a> <a name="self-initializer-expression"></a>
> *self 构造器表达式* → **self** **.** **init** > *self 构造器表达式* → **self** **.** **init**
>
<a name="superclass_expression"></a> <a name="superclass_expression"></a>
### 父类表达式 ### 父类表达式
@ -323,8 +390,11 @@ struct Point {
*父类*表达式可以使我们在某个类中访问它的父类,它有如下形式: *父类*表达式可以使我们在某个类中访问它的父类,它有如下形式:
> super.`成员名称` > super.`成员名称`
>
> super[`下标索引`] > super[`下标索引`]
>
> super.init(`构造器参数`) > super.init(`构造器参数`)
>
第一种形式用来访问父类的某个成员,第二种形式用来访问父类的下标,第三种形式用来访问父类的构造器。 第一种形式用来访问父类的某个成员,第二种形式用来访问父类的下标,第三种形式用来访问父类的构造器。
@ -337,10 +407,13 @@ struct Point {
> >
<a name="superclass-method-expression"></a> <a name="superclass-method-expression"></a>
> *父类方法表达式* → **super** **.** [*标识符*](02_Lexical_Structure.md#identifier) > *父类方法表达式* → **super** **.** [*标识符*](02_Lexical_Structure.md#identifier)
>
<a name="superclass-subscript-expression"></a> <a name="superclass-subscript-expression"></a>
> *父类下标表达式* → **super** [[*函数调用参数表*](#function-call-argument-list­) **]** > *父类下标表达式* → **super** [[*函数调用参数表*](#function-call-argument-list­) **]**
>
<a name="superclass-initializer-expression"></a> <a name="superclass-initializer-expression"></a>
> *父类构造器表达式* → **super** **.** **init** > *父类构造器表达式* → **super** **.** **init**
>
<a name="closure_expression"></a> <a name="closure_expression"></a>
### 闭包表达式 ### 闭包表达式
@ -349,6 +422,7 @@ struct Point {
```swift ```swift
{ (parameters) -> return type in { (parameters) -> return type in
>
statements statements
} }
``` ```
@ -366,6 +440,7 @@ struct Point {
```swift ```swift
myFunction { myFunction {
(x: Int, y: Int) -> Int in (x: Int, y: Int) -> Int in
>
return x + y return x + y
} }
@ -447,27 +522,44 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
关于闭包表达式的更多信息和例子,请参阅 [闭包表达式](../chapter2/07_Closures.html#closure_expressions)。关于捕获列表的更多信息和例子,请参阅 [解决闭包引起的循环强引用](../chapter2/23_Automatic_Reference_Counting.html#resolving_strong_reference_cycles_for_closures)。 关于闭包表达式的更多信息和例子,请参阅 [闭包表达式](../chapter2/07_Closures.html#closure_expressions)。关于捕获列表的更多信息和例子,请参阅 [解决闭包引起的循环强引用](../chapter2/23_Automatic_Reference_Counting.html#resolving_strong_reference_cycles_for_closures)。
> 闭包表达式语法 > 闭包表达式语法
>
> >
> <a name="closure-expression"></a> > <a name="closure-expression"></a>
>
> *闭包表达式* → **{** [*闭包签名*](#closure-signature)<sub>可选</sub> [*语句*](#statements) **}** > *闭包表达式* → **{** [*闭包签名*](#closure-signature)<sub>可选</sub> [*语句*](#statements) **}**
>
> >
> <a name="closure-signature"></a> > <a name="closure-signature"></a>
>
> >
> 闭包签名* → [*参数子句*](#parameter-clause) [*函数结果*](05_Declarations.html#function-result)<sub>可选</sub> **in** > 闭包签名* → [*参数子句*](#parameter-clause) [*函数结果*](05_Declarations.html#function-result)<sub>可选</sub> **in**
>
> *闭包签名* → [*标识符列表*](#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in** > *闭包签名* → [*标识符列表*](#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
>
> *闭包签名* → [*捕获列表*](#capture-list) [*参数子句*](05_Declarations.md#parameter-clause) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in** > *闭包签名* → [*捕获列表*](#capture-list) [*参数子句*](05_Declarations.md#parameter-clause) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
>
> *闭包签名* → [*捕获列表*](#capture-list) [*标识符列表*](02_Lexical_Structure.md#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in** > *闭包签名* → [*捕获列表*](#capture-list) [*标识符列表*](02_Lexical_Structure.md#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
>
> *闭包签名* → [*捕获列表*](#capture-list) **in** > *闭包签名* → [*捕获列表*](#capture-list) **in**
>
> >
> <a name="capture-list"></a> > <a name="capture-list"></a>
>
> >
> 捕获列表* → [ [*捕获列表项列表*](#capture-list-items) **]** > 捕获列表* → [ [*捕获列表项列表*](#capture-list-items) **]**
>
> <a name="capture-list-items"></a> > <a name="capture-list-items"></a>
>
> *捕获列表项列表* → [*捕获列表项*](#capture-list-item) | [*捕获列表项*](#capture-list-item) **,** [*捕获列表项列表*](#capture-list-items) > *捕获列表项列表* → [*捕获列表项*](#capture-list-item) | [*捕获列表项*](#capture-list-item) **,** [*捕获列表项列表*](#capture-list-items)
>
> <a name="capture-list-item"></a> > <a name="capture-list-item"></a>
>
> *捕获列表项* → [*捕获说明符*](#capture-specifier)<sub>可选</sub> [*表达式*](#expression) > *捕获列表项* → [*捕获说明符*](#capture-specifier)<sub>可选</sub> [*表达式*](#expression)
>
> <a name="capture-specifier"></a> > <a name="capture-specifier"></a>
>
> *捕获说明符* → **weak** | **unowned** | **unowned(safe)** | **unowned(unsafe)** > *捕获说明符* → **weak** | **unowned** | **unowned(safe)** | **unowned(unsafe)**
>
<a name="implicit_member_expression"></a> <a name="implicit_member_expression"></a>
### 隐式成员表达式 ### 隐式成员表达式
@ -475,6 +567,7 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
若类型可被推断出来,可以使用*隐式成员表达式*来访问某个类型的成员(例如某个枚举成员或某个类型方法),形式如下: 若类型可被推断出来,可以使用*隐式成员表达式*来访问某个类型的成员(例如某个枚举成员或某个类型方法),形式如下:
> .`成员名称` > .`成员名称`
>
例如: 例如:
@ -487,6 +580,7 @@ x = .AnotherValue
> >
<a name="implicit-member-expression"></a> <a name="implicit-member-expression"></a>
> *隐式成员表达式* → **.** [*标识符*](02_Lexical_Structure.md#identifier) > *隐式成员表达式* → **.** [*标识符*](02_Lexical_Structure.md#identifier)
>
<a name="parenthesized_expression"></a> <a name="parenthesized_expression"></a>
### 圆括号表达式 ### 圆括号表达式
@ -497,6 +591,7 @@ x = .AnotherValue
> >
<a name="parenthesized-expression"></a> <a name="parenthesized-expression"></a>
> *圆括号表达式* → **( [*表达式*](#expression) )** > *圆括号表达式* → **( [*表达式*](#expression) )**
>
<a name="Tuple_Expression"></a> <a name="Tuple_Expression"></a>
### 元组表达式 ### 元组表达式
@ -504,22 +599,28 @@ x = .AnotherValue
*元组表达式*由圆括号和其中多个逗号分隔的子表达式组成。每个子表达式前面可以有一个标识符,用冒号隔开。元组表达式形式如下: *元组表达式*由圆括号和其中多个逗号分隔的子表达式组成。每个子表达式前面可以有一个标识符,用冒号隔开。元组表达式形式如下:
> (`标识符 1` : `表达式 1`, `标识符 2` : `表达式 2`, `...`) > (`标识符 1` : `表达式 1`, `标识符 2` : `表达式 2`, `...`)
>
元组表达式可以一个表达式都没有,也可以包含两个或是更多的表达式。单个表达式用括号括起来就是括号表达式了。 元组表达式可以一个表达式都没有,也可以包含两个或是更多的表达式。单个表达式用括号括起来就是括号表达式了。
> 注意 > 注意
>
> >
> 在 Swift 中,空的元组表达式和空的元组类型都写作 `()`。由于 `Void` 是 `()` 的类型别名,因此可以使用它来表示空的元组类型。虽然如此,`Void` 就像所有的类型别名一样,永远是一个类型——不能表示空的元组表达式。 > 在 Swift 中,空的元组表达式和空的元组类型都写作 `()`。由于 `Void` 是 `()` 的类型别名,因此可以使用它来表示空的元组类型。虽然如此,`Void` 就像所有的类型别名一样,永远是一个类型——不能表示空的元组表达式。
>
> 元组表达式语法 > 元组表达式语法
> >
<a name="tuple-expression"></a> <a name="tuple-expression"></a>
> *元组表达式* → **( )** | **(**[*元组元素*](#tuple-element) [*元组元素列表*](#tuple-element-list) **)** > *元组表达式* → **( )** | **(**[*元组元素*](#tuple-element) [*元组元素列表*](#tuple-element-list) **)**
>
<a name="tuple-element-list"></a> <a name="tuple-element-list"></a>
> *元组元素列表* → [*元组元素*](#tuple-element) | [*元组元素*](#tuple-element) **,** [*元组元素列表*](#tuple-element-list) > *元组元素列表* → [*元组元素*](#tuple-element) | [*元组元素*](#tuple-element) **,** [*元组元素列表*](#tuple-element-list)
>
<a name="tuple-element"></a> <a name="tuple-element"></a>
> *元组元素* → [*表达式*](#expression) | [*标识符*](identifier) **:** [*表达式*](#expression) > *元组元素* → [*表达式*](#expression) | [*标识符*](identifier) **:** [*表达式*](#expression)
>
<a name="wildcard_expression"></a> <a name="wildcard_expression"></a>
@ -536,6 +637,7 @@ x = .AnotherValue
> >
<a name="wildcard-expression"></a> <a name="wildcard-expression"></a>
> *通配符表达式* → **_** > *通配符表达式* → **_**
>
<a name="key-path_expression"></a> <a name="key-path_expression"></a>
@ -544,6 +646,7 @@ x = .AnotherValue
Key-path 表达式引用一个类型的属性或下标。在动态语言中使场景可以使用 Key-path 表达式,例如观察键值对。格式为: Key-path 表达式引用一个类型的属性或下标。在动态语言中使场景可以使用 Key-path 表达式,例如观察键值对。格式为:
> **\类型名.路径** > **\类型名.路径**
>
*类型名*是一个具体类型的名称,包含任何泛型参数,例如 `String``[Int]``Set<Int>` *类型名*是一个具体类型的名称,包含任何泛型参数,例如 `String``[Int]``Set<Int>`
@ -620,6 +723,7 @@ let myGreeting = greetings[keyPath: \[String].[1]]
var index = 2 var index = 2
let path = \[String].[index] let path = \[String].[index]
let fn: ([String]) -> String = { strings in strings[index] } let fn: ([String]) -> String = { strings in strings[index] }
>
print(greetings[keyPath: path]) print(greetings[keyPath: path])
// 打印 "bonjour" // 打印 "bonjour"
@ -668,17 +772,26 @@ print(interestingNumbers[keyPath: \[String: [Int]].["hexagonal"]!.count.bitWidth
关于更多如何使用 key path 与 Objective-C APIs 交互的信息,请参阅 [在 Swift 中使用 Objective-C 运行时特性](https://developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift)。关于更多 key-value 编程和 key-value 观察的信息,请参阅 [Key-Value 编程](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/index.html#//apple_ref/doc/uid/10000107i) 和 [Key-Value 观察编程](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177i) 关于更多如何使用 key path 与 Objective-C APIs 交互的信息,请参阅 [在 Swift 中使用 Objective-C 运行时特性](https://developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift)。关于更多 key-value 编程和 key-value 观察的信息,请参阅 [Key-Value 编程](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/index.html#//apple_ref/doc/uid/10000107i) 和 [Key-Value 观察编程](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177i)
> key-path 表达式语法 > key-path 表达式语法
>
> >
> <a name="key-path-expression"></a> > <a name="key-path-expression"></a>
>
> *key-path 表达式* → **\\** [类型](03_Types.md#type)<sub>可选</sub> **.** [多个 key-path 组件](#key-path-components) > *key-path 表达式* → **\\** [类型](03_Types.md#type)<sub>可选</sub> **.** [多个 key-path 组件](#key-path-components)
>
> <a name="key-path-components"></a> > <a name="key-path-components"></a>
>
> *多个 key-path 组件* → [key-path 组件](#key-path-component) | [key-path 组件](#key-path-component) **.** [多个 key-path 组件](#key-path-components) > *多个 key-path 组件* → [key-path 组件](#key-path-component) | [key-path 组件](#key-path-component) **.** [多个 key-path 组件](#key-path-components)
>
> <a name="key-path-component"></a> > <a name="key-path-component"></a>
>
> *key-path 组件* → [标识符](02_Lexical_Structure.html#identifier) [多个 key-path 后缀](#key-path-postfixes)<sub>可选<sub> | [多个 key-path 后缀](#key-path-postfixes) > *key-path 组件* → [标识符](02_Lexical_Structure.html#identifier) [多个 key-path 后缀](#key-path-postfixes)<sub>可选<sub> | [多个 key-path 后缀](#key-path-postfixes)
>
> <a name="key-path-postfixes"></a> > <a name="key-path-postfixes"></a>
> *多个 key-path 后缀* → [key-path 后缀](#key-path-postfix) [多个 key-path 后缀](#key-path-postfixes)<sub>可选<sub> >
> <a name="key-path-postfixes"></a> > *多个 key-path 后缀* → [key-path 后缀](#key-path-postfix) [多个 key-path 后缀](#key-path-postfixes)<sub>可选<sub> <a name="key-path-postfixes"></a>
>
> *key-path 后缀* → **?** | **!** | **self** | **\[** [函数调用参数表](#function-call-argument-list) **\]** > *key-path 后缀* → **?** | **!** | **self** | **\[** [函数调用参数表](#function-call-argument-list) **\]**
>
@ -689,6 +802,7 @@ print(interestingNumbers[keyPath: \[String: [Int]].["hexagonal"]!.count.bitWidth
*选择器表达式*可以让你通过选择器来引用在 Objective-C 中方法method和属性property的 setter 和 getter 方法。 *选择器表达式*可以让你通过选择器来引用在 Objective-C 中方法method和属性property的 setter 和 getter 方法。
> \#selector(方法名) > \#selector(方法名)
>
\#selector(getter: 属性名) \#selector(getter: 属性名)
\#selector(setter: 属性名) \#selector(setter: 属性名)
@ -718,6 +832,7 @@ extension SomeClass {
func doSomething(_ x: String) { } func doSomething(_ x: String) { }
} }
let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (String) -> Void) let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (String) -> Void)
>
``` ```
由于选择器是在编译时创建的,因此编译器可以检查方法或者属性是否存在,以及是否在运行时暴露给了 Objective-C 。 由于选择器是在编译时创建的,因此编译器可以检查方法或者属性是否存在,以及是否在运行时暴露给了 Objective-C 。
@ -725,14 +840,19 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
> 注意 > 注意
> >
> 虽然方法名或者属性名是个表达式,但是它不会被求值。 > 虽然方法名或者属性名是个表达式,但是它不会被求值。
>
更多关于如何在 Swift 代码中使用选择器来与 Objective-C API 进行交互的信息,请参阅 [在 Swift 中使用 Objective-C 运行时特性](https://developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift)。 更多关于如何在 Swift 代码中使用选择器来与 Objective-C API 进行交互的信息,请参阅 [在 Swift 中使用 Objective-C 运行时特性](https://developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift)。
> 选择器表达式语法 > 选择器表达式语法
>
<a name="selector-expression"></a> <a name="selector-expression"></a>
> *选择器表达式* → __#selector__ **(** [*表达式*](#expression) **)** > *选择器表达式* → __#selector__ **(** [*表达式*](#expression) **)**
>
> *选择器表达式* → __#selector__ **(** [*getter:表达式*](#expression) **)** > *选择器表达式* → __#selector__ **(** [*getter:表达式*](#expression) **)**
>
> *选择器表达式* → __#selector__ **(** [*setter:表达式*](#expression) **)** > *选择器表达式* → __#selector__ **(** [*setter:表达式*](#expression) **)**
>
<a name="key-path_string_expressions"></a> <a name="key-path_string_expressions"></a>
@ -741,6 +861,7 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
key-path 字符串表达式可以访问一个引用 Objective-C 属性的字符串,通常在 key-value 编程和 key-value 观察 APIs 中使用。其格式如下: key-path 字符串表达式可以访问一个引用 Objective-C 属性的字符串,通常在 key-value 编程和 key-value 观察 APIs 中使用。其格式如下:
> `#keyPath` ( `属性名` ) > `#keyPath` ( `属性名` )
>
属性名必须是一个可以在 Objective-C 运行时使用的属性的引用。在编译期key-path 字符串表达式会被一个字符串字面量替换。例如: 属性名必须是一个可以在 Objective-C 运行时使用的属性的引用。在编译期key-path 字符串表达式会被一个字符串字面量替换。例如:
@ -766,6 +887,7 @@ if let value = c.value(forKey: keyPath) {
```swift ```swift
extension SomeClass { extension SomeClass {
func getSomeKeyPath() -> String { func getSomeKeyPath() -> String {
>
return #keyPath(someProperty) return #keyPath(someProperty)
} }
} }
@ -778,12 +900,17 @@ print(keyPath == c.getSomeKeyPath())
关于更多如何使用 key path 与 Objective-C APIs 交互的信息,请参阅 [在 Swift 中使用 Objective-C 运行时特性](https://developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift)。关于更多 key-value 编程和 key-value 观察的信息,请参阅 [Key-Value 编程](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/index.html#//apple_ref/doc/uid/10000107i) 和 [Key-Value 观察编程](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177i) 关于更多如何使用 key path 与 Objective-C APIs 交互的信息,请参阅 [在 Swift 中使用 Objective-C 运行时特性](https://developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift)。关于更多 key-value 编程和 key-value 观察的信息,请参阅 [Key-Value 编程](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/index.html#//apple_ref/doc/uid/10000107i) 和 [Key-Value 观察编程](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177i)
> 注意 > 注意
>
> >
> 尽管*属性名*是一个表达式,但它永远不会被求值 > 尽管*属性名*是一个表达式,但它永远不会被求值
>
> key-path 字符串表达式语法 > key-path 字符串表达式语法
>
> <a name="key-path-string-expression"></a> > <a name="key-path-string-expression"></a>
>
> *key-path 字符串表达式* → **#keyPath (** [表达式](#expression) **)** > *key-path 字符串表达式* → **#keyPath (** [表达式](#expression) **)**
>
<a name="postfix_expressions"></a> <a name="postfix_expressions"></a>
@ -796,17 +923,28 @@ print(keyPath == c.getSomeKeyPath())
关于 Swift 标准库提供的运算符的更多信息,请参阅 [运算符定义](https://developer.apple.com/documentation/swift/operator_declarations)。 关于 Swift 标准库提供的运算符的更多信息,请参阅 [运算符定义](https://developer.apple.com/documentation/swift/operator_declarations)。
> 后缀表达式语法 > 后缀表达式语法
>
<a name="postfix-expression"></a> <a name="postfix-expression"></a>
> *后缀表达式* → [*基本表达式*](#primary-expression) > *后缀表达式* → [*基本表达式*](#primary-expression)
>
> *后缀表达式* → [*后缀表达式*](#postfix-expression) [*后缀运算符*](02_Lexical_Structure.md#postfix-operator) > *后缀表达式* → [*后缀表达式*](#postfix-expression) [*后缀运算符*](02_Lexical_Structure.md#postfix-operator)
>
> *后缀表达式* → [*函数调用表达式*](#function-call-expression) > *后缀表达式* → [*函数调用表达式*](#function-call-expression)
>
> *后缀表达式* → [*构造器表达式*](#initializer-expression) > *后缀表达式* → [*构造器表达式*](#initializer-expression)
>
> *后缀表达式* → [*显式成员表达式*](#explicit-member-expression) > *后缀表达式* → [*显式成员表达式*](#explicit-member-expression)
>
> *后缀表达式* → [*后缀 self 表达式*](#postfix-self-expression) > *后缀表达式* → [*后缀 self 表达式*](#postfix-self-expression)
>
> *后缀表达式* → [*dynamicType 表达式*](#dynamic-type-expression) > *后缀表达式* → [*dynamicType 表达式*](#dynamic-type-expression)
>
> *后缀表达式* → [*下标表达式*](#subscript-expression) > *后缀表达式* → [*下标表达式*](#subscript-expression)
>
> *后缀表达式* → [*强制取值表达式*](#forced-value-expression) > *后缀表达式* → [*强制取值表达式*](#forced-value-expression)
>
> *后缀表达式* → [*可选链表达式*](#optional-chaining-expression) > *后缀表达式* → [*可选链表达式*](#optional-chaining-expression)
>
<a name="function_call_expression"></a> <a name="function_call_expression"></a>
### 函数调用表达式 ### 函数调用表达式
@ -814,12 +952,14 @@ print(keyPath == c.getSomeKeyPath())
*函数调用表达式*由函数名和参数列表组成,形式如下: *函数调用表达式*由函数名和参数列表组成,形式如下:
> `函数名`(`参数 1`, `参数 2`) > `函数名`(`参数 1`, `参数 2`)
>
函数名可以是值为函数类型的任意表达式。 函数名可以是值为函数类型的任意表达式。
如果函数声明中指定了参数的名字,那么在调用的时候也必须得写出来。这种函数调用表达式具有以下形式: 如果函数声明中指定了参数的名字,那么在调用的时候也必须得写出来。这种函数调用表达式具有以下形式:
> `函数名`(`参数名 1`: `参数 1`, `参数名 2`: `参数 2`) > `函数名`(`参数名 1`: `参数 1`, `参数名 2`: `参数 2`)
>
如果函数的最后一个参数是函数类型,可以在函数调用表达式的尾部(右圆括号之后)加上一个闭包,该闭包会作为函数的最后一个参数。如下两种写法是等价的: 如果函数的最后一个参数是函数类型,可以在函数调用表达式的尾部(右圆括号之后)加上一个闭包,该闭包会作为函数的最后一个参数。如下两种写法是等价的:
@ -838,20 +978,33 @@ myData.someMethod {$0 == 13}
``` ```
> 函数调用表达式语法 > 函数调用表达式语法
>
> <a name="function-call-expression"></a> > <a name="function-call-expression"></a>
>
> *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause) > *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause)
>
> *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause)<sub>可选</sub> [*尾随闭包*](#trailing-closure) > *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause)<sub>可选</sub> [*尾随闭包*](#trailing-closure)
>
> >
> <a name="function-call-argument-clause"></a> > <a name="function-call-argument-clause"></a>
>
> *函数调用参数子句* → **(** **)** | **(** [*函数调用参数表*](#function-call-argument-list) **)** > *函数调用参数子句* → **(** **)** | **(** [*函数调用参数表*](#function-call-argument-list) **)**
>
> <a name="function-call-argument-list"></a> > <a name="function-call-argument-list"></a>
>
> *函数调用参数表* → [函数调用参数](#function-call-argument) | [函数调用参数](#function-call-argument) **,** [*函数调用参数表*](#function-call-argument-list) > *函数调用参数表* → [函数调用参数](#function-call-argument) | [函数调用参数](#function-call-argument) **,** [*函数调用参数表*](#function-call-argument-list)
>
> <a name="function-call-argument"></a> > <a name="function-call-argument"></a>
>
> *函数调用参数* → [表达式](#expression) | [标识符](02_Lexical_Structure.html#identifier) **:** [*表达式*](#expression) > *函数调用参数* → [表达式](#expression) | [标识符](02_Lexical_Structure.html#identifier) **:** [*表达式*](#expression)
>
> *函数调用参数* → [运算符](02_Lexical_Structure.html#operator) | [标识符](02_Lexical_Structure.html#identifier) **:** [*运算符*](02_Lexical_Structure.html#operator) > *函数调用参数* → [运算符](02_Lexical_Structure.html#operator) | [标识符](02_Lexical_Structure.html#identifier) **:** [*运算符*](02_Lexical_Structure.html#operator)
>
> >
> <a name="trailing-closure"></a> > <a name="trailing-closure"></a>
>
> *尾随闭包* → [*闭包表达式*](#closure-expression) > *尾随闭包* → [*闭包表达式*](#closure-expression)
>
<a name="initializer_expression"></a> <a name="initializer_expression"></a>
### 构造器表达式 ### 构造器表达式
@ -859,6 +1012,7 @@ myData.someMethod {$0 == 13}
*构造器表达式*用于访问某个类型的构造器,形式如下: *构造器表达式*用于访问某个类型的构造器,形式如下:
> `表达式`.init(`构造器参数`) > `表达式`.init(`构造器参数`)
>
你可以在函数调用表达式中使用构造器表达式来初始化某个类型的新实例。也可以使用构造器表达式来代理给父类构造器。 你可以在函数调用表达式中使用构造器表达式来初始化某个类型的新实例。也可以使用构造器表达式来代理给父类构造器。
@ -876,6 +1030,7 @@ class SomeSubClass: SomeSuperClass {
```swift ```swift
// 类型注解是必须的,因为 String 类型有多种构造器 // 类型注解是必须的,因为 String 类型有多种构造器
let initializer: Int -> String = String.init let initializer: Int -> String = String.init
>
let oneTwoThree = [1, 2, 3].map(initializer).reduce("", combine: +) let oneTwoThree = [1, 2, 3].map(initializer).reduce("", combine: +)
print(oneTwoThree) print(oneTwoThree)
// 打印“123” // 打印“123”
@ -892,9 +1047,12 @@ let s3 = someValue.dynamicType.init(data: 7) // 有效
``` ```
> 构造器表达式语法 > 构造器表达式语法
>
<a name="initializer-expression"></a> <a name="initializer-expression"></a>
> *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** > *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init**
>
> *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** **(** [*参数名称*](#argument-names) **)** > *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** **(** [*参数名称*](#argument-names) **)**
>
<a name="explicit_member_expression"></a> <a name="explicit_member_expression"></a>
### 显式成员表达式 ### 显式成员表达式
@ -902,6 +1060,7 @@ let s3 = someValue.dynamicType.init(data: 7) // 有效
*显式成员表达式*允许我们访问命名类型、元组或者模块的成员,其形式如下: *显式成员表达式*允许我们访问命名类型、元组或者模块的成员,其形式如下:
> `表达式`.`成员名` > `表达式`.`成员名`
>
命名类型的某个成员在原始实现或者扩展中定义,例如: 命名类型的某个成员在原始实现或者扩展中定义,例如:
@ -942,6 +1101,7 @@ let b = instance.someMethod(_:y:) // 无歧义
let d = instance.overloadedMethod // 有歧义 let d = instance.overloadedMethod // 有歧义
let d = instance.overloadedMethod(_:y:) // 有歧义 let d = instance.overloadedMethod(_:y:) // 有歧义
let d: (Int, Bool) -> Void = instance.overloadedMethod(_:y:) // 无歧义 let d: (Int, Bool) -> Void = instance.overloadedMethod(_:y:) // 无歧义
>
``` ```
如果点号(`.`)出现在行首,它会被视为显式成员表达式的一部分,而不是隐式成员表达式的一部分。例如如下代码所展示的被分为多行的链式方法调用: 如果点号(`.`)出现在行首,它会被视为显式成员表达式的一部分,而不是隐式成员表达式的一部分。例如如下代码所展示的被分为多行的链式方法调用:
@ -950,19 +1110,25 @@ let d: (Int, Bool) -> Void = instance.overloadedMethod(_:y:) // 无歧义
let x = [10, 3, 20, 15, 4] let x = [10, 3, 20, 15, 4]
.sort() .sort()
.filter { $0 > 5 } .filter { $0 > 5 }
>
.map { $0 * 100 } .map { $0 * 100 }
``` ```
> 显式成员表达式语法 > 显式成员表达式语法
>
<a name="explicit-member-expression"></a> <a name="explicit-member-expression"></a>
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*十进制数字*] (02_Lexical_Structure.html#decimal-digit) > *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*十进制数字*] (02_Lexical_Structure.html#decimal-digit)
>
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02_Lexical_Structure.html#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.html#generic-argument-clause)<sub>可选</sub><br/> > *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02_Lexical_Structure.html#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.html#generic-argument-clause)<sub>可选</sub><br/>
>
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.html#identifier) **(** [*参数名称*](#argument-names) **)** > *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.html#identifier) **(** [*参数名称*](#argument-names) **)**
> >
<a name="argument-names"></a> <a name="argument-names"></a>
> *参数名称* → [*参数名*](#argument-name) [*参数名称*](#argument-names)<sub>可选</sub><br/> > *参数名称* → [*参数名*](#argument-name) [*参数名称*](#argument-names)<sub>可选</sub><br/>
>
<a name="argument-name"></a> <a name="argument-name"></a>
> *参数名* → [*标识符*](02_Lexical_Structure.html#identifier) **:** > *参数名* → [*标识符*](02_Lexical_Structure.html#identifier) **:**
>
<a name="postfix_self_expression"></a> <a name="postfix_self_expression"></a>
### 后缀 self 表达式 ### 后缀 self 表达式
@ -970,7 +1136,9 @@ let x = [10, 3, 20, 15, 4]
后缀 `self` 表达式由某个表达式或类型名紧跟 `.self` 组成,其形式如下: 后缀 `self` 表达式由某个表达式或类型名紧跟 `.self` 组成,其形式如下:
> `表达式`.self > `表达式`.self
>
> `类型`.self > `类型`.self
>
第一种形式返回表达式的值。例如:`x.self` 返回 `x` 第一种形式返回表达式的值。例如:`x.self` 返回 `x`
@ -980,6 +1148,7 @@ let x = [10, 3, 20, 15, 4]
> >
<a name="postfix-self-expression"></a> <a name="postfix-self-expression"></a>
> *后缀 self 表达式* → [*后缀表达式*](#postfix-expression) **.** **self** > *后缀 self 表达式* → [*后缀表达式*](#postfix-expression) **.** **self**
>
<a name="subscript_expression"></a> <a name="subscript_expression"></a>
@ -988,6 +1157,7 @@ let x = [10, 3, 20, 15, 4]
可通过*下标表达式*访问相应的下标,形式如下: 可通过*下标表达式*访问相应的下标,形式如下:
> `表达式`[`索引表达式`] > `表达式`[`索引表达式`]
>
要获取下标表达式的值,可将索引表达式作为下标表达式的参数来调用下标 getter。下标 setter 的调用方式与之一样。 要获取下标表达式的值,可将索引表达式作为下标表达式的参数来调用下标 getter。下标 setter 的调用方式与之一样。
@ -997,6 +1167,7 @@ let x = [10, 3, 20, 15, 4]
> >
<a name="subscript-expression"></a> <a name="subscript-expression"></a>
> *下标表达式* → [*后缀表达式*](#postfix-expression) **[** [*表达式列表*](#expression-list) **]** > *下标表达式* → [*后缀表达式*](#postfix-expression) **[** [*表达式列表*](#expression-list) **]**
>
<a name="forced-Value_expression"></a> <a name="forced-Value_expression"></a>
### 强制取值表达式 ### 强制取值表达式
@ -1004,6 +1175,7 @@ let x = [10, 3, 20, 15, 4]
当你确定可选值不是 `nil` 时,可以使用*强制取值表达式*来强制解包,形式如下: 当你确定可选值不是 `nil` 时,可以使用*强制取值表达式*来强制解包,形式如下:
> `表达式`! > `表达式`!
>
如果该表达式的值不是 `nil`,则返回解包后的值。否则,抛出运行时错误。 如果该表达式的值不是 `nil`,则返回解包后的值。否则,抛出运行时错误。
@ -1023,6 +1195,7 @@ someDictionary["a"]![0] = 100
> >
<a name="forced-value-expression"></a> <a name="forced-value-expression"></a>
> *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!** > *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!**
>
<a name="optional-chaining_expression"></a> <a name="optional-chaining_expression"></a>
### 可选链表达式 ### 可选链表达式
@ -1030,6 +1203,7 @@ someDictionary["a"]![0] = 100
*可选链表达式*提供了一种使用可选值的便捷方法,形式如下: *可选链表达式*提供了一种使用可选值的便捷方法,形式如下:
> `表达式`? > `表达式`?
>
后缀 `?` 运算符会根据表达式生成可选链表达式而不会改变表达式的值。 后缀 `?` 运算符会根据表达式生成可选链表达式而不会改变表达式的值。
@ -1055,6 +1229,7 @@ if let unwrappedC = c {
```swift ```swift
func someFunctionWithSideEffects() -> Int { func someFunctionWithSideEffects() -> Int {
>
// 译者注:为了能看出此函数是否被执行,加上了一句打印 // 译者注:为了能看出此函数是否被执行,加上了一句打印
print("someFunctionWithSideEffects") print("someFunctionWithSideEffects")
return 42 return 42
@ -1074,3 +1249,4 @@ someDictionary["a"]?[0] = someFunctionWithSideEffects()
> >
<a name="optional-chaining-expression"></a> <a name="optional-chaining-expression"></a>
> *可选链表达式* → [*后缀表达式*](#postfix-expression) **?** > *可选链表达式* → [*后缀表达式*](#postfix-expression) **?**
>

View File

@ -8,18 +8,29 @@
是否将分号(`;`)添加到语句的末尾是可选的。但若要在同一行内写多条独立语句,则必须使用分号。 是否将分号(`;`)添加到语句的末尾是可选的。但若要在同一行内写多条独立语句,则必须使用分号。
> 语句语法 > 语句语法
>
<a name="statement"></a> <a name="statement"></a>
> *语句* → [*表达式*](04_Expressions.md#expression) **;**<sub>可选</sub> > *语句* → [*表达式*](04_Expressions.md#expression) **;**<sub>可选</sub>
>
> *语句* → [*声明*](05_Declarations.md#declaration) **;**<sub>可选</sub> > *语句* → [*声明*](05_Declarations.md#declaration) **;**<sub>可选</sub>
>
> *语句* → [*循环语句*](#loop-statement) **;**<sub>可选</sub> > *语句* → [*循环语句*](#loop-statement) **;**<sub>可选</sub>
>
> *语句* → [*分支语句*](#branch-statement) **;**<sub>可选</sub> > *语句* → [*分支语句*](#branch-statement) **;**<sub>可选</sub>
>
> *语句* → [*带标签的语句*](#labeled-statement) **;**<sub>可选</sub> > *语句* → [*带标签的语句*](#labeled-statement) **;**<sub>可选</sub>
>
> *语句* → [*控制转移语句*](#control-transfer-statement) **;**<sub>可选</sub> > *语句* → [*控制转移语句*](#control-transfer-statement) **;**<sub>可选</sub>
>
> *语句* → [*defer 语句*](#defer-statement) **;**<sub>可选</sub> > *语句* → [*defer 语句*](#defer-statement) **;**<sub>可选</sub>
>
> *语句* → [*do 语句*](#do-statement) **:**<sub>可选</sub> > *语句* → [*do 语句*](#do-statement) **:**<sub>可选</sub>
>
> *语句* → [*编译器控制语句*](#compiler-control-statement) > *语句* → [*编译器控制语句*](#compiler-control-statement)
>
<a name="statements"></a> <a name="statements"></a>
> *多条语句* → [*语句*](#statement) [*多条语句*](#statements)<sub>可选</sub> > *多条语句* → [*语句*](#statement) [*多条语句*](#statements)<sub>可选</sub>
>
<a name="loop_statements"></a> <a name="loop_statements"></a>
## 循环语句 ## 循环语句
@ -29,11 +40,15 @@
通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。 通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。
> 循环语句语法 > 循环语句语法
>
> >
<a name="loop-statement"></a> <a name="loop-statement"></a>
> *循环语句* → [*for-in 语句*](#for-in-statement) > *循环语句* → [*for-in 语句*](#for-in-statement)
>
> *循环语句* → [*while 语句*](#while-statement) > *循环语句* → [*while 语句*](#while-statement)
>
> *循环语句* → [*repeat-while 语句*](#repeat-while-statement) > *循环语句* → [*repeat-while 语句*](#repeat-while-statement)
>
<a name="for-in_statements"></a> <a name="for-in_statements"></a>
### For-In 语句 ### For-In 语句
@ -51,9 +66,11 @@ for item in collection {
`for-in` 语句在循环开始前会调用集合表达式(`collection expression`)的 `makeIterator()` 方法来获取一个实现了 `IteratorProtocol` 协议的迭代器类型。接下来循环开始,反复调用该迭代器的 `next()` 方法。如果其返回值不是 `nil`,它将会被赋给 `item`,然后执行循环体语句,执行完毕后回到循环开始处,继续重复这一过程;否则,既不会赋值也不会执行循环体语句,`for-in` 语句至此执行完毕。 `for-in` 语句在循环开始前会调用集合表达式(`collection expression`)的 `makeIterator()` 方法来获取一个实现了 `IteratorProtocol` 协议的迭代器类型。接下来循环开始,反复调用该迭代器的 `next()` 方法。如果其返回值不是 `nil`,它将会被赋给 `item`,然后执行循环体语句,执行完毕后回到循环开始处,继续重复这一过程;否则,既不会赋值也不会执行循环体语句,`for-in` 语句至此执行完毕。
> for-in 语句语法 > for-in 语句语法
>
> >
<a name="for-in-statement"></a> <a name="for-in-statement"></a>
> *for-in 语句* → **for** **case**<sub>可选</sub> [*模式*](07_Patterns.md#pattern) **in** [*表达式*](04_Expressions.md#expression) [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block) > *for-in 语句* → **for** **case**<sub>可选</sub> [*模式*](07_Patterns.md#pattern) **in** [*表达式*](04_Expressions.md#expression) [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
>
<a name="while_statements"></a> <a name="while_statements"></a>
### While 语句 ### While 语句
@ -78,19 +95,25 @@ while condition {
条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.md#optional_binding)。 条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.md#optional_binding)。
> while 语句语法 > while 语句语法
>
> >
<a name="while-statement"></a> <a name="while-statement"></a>
> *while 语句* → **while** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block) > *while 语句* → **while** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block)
>
<a name="condition-clause"></a> <a name="condition-clause"></a>
> *条件子句* → [*表达式*](04_Expressions.md#expression) | [*表达式*](04_Expressions.md#expression) **,** [*条件列表*](#condition-list) > *条件子句* → [*表达式*](04_Expressions.md#expression) | [*表达式*](04_Expressions.md#expression) **,** [*条件列表*](#condition-list)
>
<a name="condition"></a> <a name="condition"></a>
> *条件* → [*表达式*](04_Expressions.md#expression) |[*可用性条件*](#availability-condition) | [*case 条件*](#case-condition) | [*可选绑定条件*](#optional-binding-condition) > *条件* → [*表达式*](04_Expressions.md#expression) |[*可用性条件*](#availability-condition) | [*case 条件*](#case-condition) | [*可选绑定条件*](#optional-binding-condition)
>
> >
<a name="case-condition"></a> <a name="case-condition"></a>
> *case 条件* → **case** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer) > *case 条件* → **case** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer)
>
<a name="optional-binding-condition"></a> <a name="optional-binding-condition"></a>
> *可选绑定条件* → **let** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer) | **var** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer) > *可选绑定条件* → **let** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer) | **var** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer)
>
<a name="repeat-while_statements"></a> <a name="repeat-while_statements"></a>
### Repeat-While 语句 ### Repeat-While 语句
@ -115,9 +138,11 @@ repeat {
条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.md#optional_binding)。 条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.md#optional_binding)。
> repeat-while 语句语法 > repeat-while 语句语法
>
> >
<a name="repeat-while-statement"></a> <a name="repeat-while-statement"></a>
> *repeat-while 语句* → **repeat** [*代码块*](05_Declarations.md#code-block) **while** [*表达式*](04_Expressions.md#expression) > *repeat-while 语句* → **repeat** [*代码块*](05_Declarations.md#code-block) **while** [*表达式*](04_Expressions.md#expression)
>
<a name="branch_statements"></a> <a name="branch_statements"></a>
## 分支语句 ## 分支语句
@ -127,11 +152,15 @@ repeat {
`if` 语句和 `switch` 语句中的控制流可以用 `break` 语句改变,请参阅 [Break 语句](#break_statement)。 `if` 语句和 `switch` 语句中的控制流可以用 `break` 语句改变,请参阅 [Break 语句](#break_statement)。
> 分支语句语法 > 分支语句语法
>
> >
<a name="branch-statement"></a> <a name="branch-statement"></a>
> *分支语句* → [*if 语句*](#if-statement) > *分支语句* → [*if 语句*](#if-statement)
>
> *分支语句* → [*guard 语句*](#guard-statement) > *分支语句* → [*guard 语句*](#guard-statement)
>
> *分支语句* → [*switch 语句*](#switch-statement) > *分支语句* → [*switch 语句*](#switch-statement)
>
<a name="if_statements"></a> <a name="if_statements"></a>
### If 语句 ### If 语句
@ -173,11 +202,14 @@ if condition 1 {
`if` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.md#optional_binding)。 `if` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.md#optional_binding)。
> if 语句语法 > if 语句语法
>
> >
<a name="if-statement"></a> <a name="if-statement"></a>
> *if 语句* → **if** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block) [*else 子句*](#else-clause)<sub>可选</sub> > *if 语句* → **if** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block) [*else 子句*](#else-clause)<sub>可选</sub>
>
<a name="else-clause"></a> <a name="else-clause"></a>
> *else 子句* → **else** [*代码块*](05_Declarations.md#code-block) | **else** [*if 语句*](#if-statement) > *else 子句* → **else** [*代码块*](05_Declarations.md#code-block) | **else** [*if 语句*](#if-statement)
>
<a name="guard_statements"></a> <a name="guard_statements"></a>
### Guard 语句 ### Guard 语句
@ -206,9 +238,11 @@ guard condition else {
关于控制转移语句,请参阅 [控制转移语句](#control_transfer_statements)。关于 `Never` 返回类型的函数,请参阅 [永不返回的函数](05_Declarations.md#rethrowing_functions_and_methods)。 关于控制转移语句,请参阅 [控制转移语句](#control_transfer_statements)。关于 `Never` 返回类型的函数,请参阅 [永不返回的函数](05_Declarations.md#rethrowing_functions_and_methods)。
> guard 语句语法 > guard 语句语法
>
> >
<a name="guard-statement"></a> <a name="guard-statement"></a>
> *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05_Declarations.html#code-block) > *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05_Declarations.html#code-block)
>
<a name="switch_statements"></a> <a name="switch_statements"></a>
### Switch 语句 ### Switch 语句
@ -279,36 +313,50 @@ case .suppressed:
当匹配到的 `case` 中的代码执行完毕后,`switch` 语句会直接退出,而不会继续执行下一个 `case` 。这就意味着,如果你想执行下一个 `case`,需要显式地在当前 `case` 中使用 `fallthrough` 语句。关于 `fallthrough` 语句的更多信息,请参阅 [Fallthrough 语句](#fallthrough_statements)。 当匹配到的 `case` 中的代码执行完毕后,`switch` 语句会直接退出,而不会继续执行下一个 `case` 。这就意味着,如果你想执行下一个 `case`,需要显式地在当前 `case` 中使用 `fallthrough` 语句。关于 `fallthrough` 语句的更多信息,请参阅 [Fallthrough 语句](#fallthrough_statements)。
> switch 语句语法 > switch 语句语法
>
> >
<a name="switch-statement"></a> <a name="switch-statement"></a>
> *switch 语句* → **switch** [*表达式*](04_Expressions.html#expression) **{** [*switch-case 列表*](#switch-cases)<sub>可选</sub> **}** > *switch 语句* → **switch** [*表达式*](04_Expressions.html#expression) **{** [*switch-case 列表*](#switch-cases)<sub>可选</sub> **}**
>
<a name="switch-cases"></a> <a name="switch-cases"></a>
> *switch case 列表* → [*switch-case*](#switch-case) [*switch-case 列表*](#switch-cases)<sub>可选</sub> > *switch case 列表* → [*switch-case*](#switch-case) [*switch-case 列表*](#switch-cases)<sub>可选</sub>
>
<a name="switch-case"></a> <a name="switch-case"></a>
> *switch case* → [*case 标签*](#case-label) [*多条语句*](#statements) | [*default 标签*](#default-label) [*多条语句*](#statements) | [*conditional-switch-case*](#conditional-switch-case-label) > *switch case* → [*case 标签*](#case-label) [*多条语句*](#statements) | [*default 标签*](#default-label) [*多条语句*](#statements) | [*conditional-switch-case*](#conditional-switch-case-label)
>
<a name="case-label"></a> <a name="case-label"></a>
> *case 标签* → [*属性*](#switch-case-attributes-label)<sub>可选</sub> **case** [*case 项列表*](#case-item-list) **:** > *case 标签* → [*属性*](#switch-case-attributes-label)<sub>可选</sub> **case** [*case 项列表*](#case-item-list) **:**
>
<a name="case-item-list"></a> <a name="case-item-list"></a>
> *case 项列表* → [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> | [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> **,** [*case 项列表*](#case-item-list) > *case 项列表* → [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> | [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> **,** [*case 项列表*](#case-item-list)
>
<a name="default-label"></a> <a name="default-label"></a>
> *default 标签* → [*属性*](#switch-case-attributes-label)<sub>可选</sub> **default** **:** > *default 标签* → [*属性*](#switch-case-attributes-label)<sub>可选</sub> **default** **:**
>
> >
<a name="where-clause"></a> <a name="where-clause"></a>
> *where-clause* → **where** [*where 表达式*](#where-expression) > *where-clause* → **where** [*where 表达式*](#where-expression)
>
<a name="where-expression"></a> <a name="where-expression"></a>
> *where-expression* → [*表达式*](04_Expressions.md#expression) > *where-expression* → [*表达式*](04_Expressions.md#expression)
>
> >
<a name="grammar_conditional-switch-case"></a> <a name="grammar_conditional-switch-case"></a>
> *conditional-switch-case* → [*switch-if-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label) <sub>可选</sub> [*switch-else-directive-clause*](#switch-case-attributes-label) <sub>可选</sub> [*endif-directive*](#switch-case-attributes-label) > *conditional-switch-case* → [*switch-if-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label) <sub>可选</sub> [*switch-else-directive-clause*](#switch-case-attributes-label) <sub>可选</sub> [*endif-directive*](#switch-case-attributes-label)
>
<a name="grammar_switch-if-directive-clause"></a> <a name="grammar_switch-if-directive-clause"></a>
> *switch-if-directive 语句* → [*if-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) <sub>可选</sub> > *switch-if-directive 语句* → [*if-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) <sub>可选</sub>
>
<a name="grammar_switch-elseif-directive-clauses"></a> <a name="grammar_switch-elseif-directive-clauses"></a>
> *switch-elseif-directive 语句(复数)* → [*elseif-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label)<sub>可选</sub> > *switch-elseif-directive 语句(复数)* → [*elseif-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label)<sub>可选</sub>
>
<a name="grammar_switch-elseif-directive-clause"></a> <a name="grammar_switch-elseif-directive-clause"></a>
> *switch-elseif-directive 语句* → [*elseif-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label)<sub>可选</sub> > *switch-elseif-directive 语句* → [*elseif-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label)<sub>可选</sub>
>
<a name="grammar_switch-else-directive-clause"></a> <a name="grammar_switch-else-directive-clause"></a>
> *switch-else-directive 语句* → [*else-directive*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) <sub>可选</sub> > *switch-else-directive 语句* → [*else-directive*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) <sub>可选</sub>
>
<a name="labeled_statements"></a> <a name="labeled_statements"></a>
## 带标签的语句 ## 带标签的语句
@ -320,16 +368,23 @@ case .suppressed:
关于使用带标签的语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章中的 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。 关于使用带标签的语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章中的 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
> 带标签的语句语法 > 带标签的语句语法
>
> >
<a name="labeled-statement"></a> <a name="labeled-statement"></a>
> *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#grammar_loop-statement) > *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#grammar_loop-statement)
>
> *带标签的语句* → [*语句标签*](#statement-label) [*if 语句*](#if-statement) > *带标签的语句* → [*语句标签*](#statement-label) [*if 语句*](#if-statement)
>
> *带标签的语句* → [*语句标签*](#statement-label) [*switch 语句*](#switch-statement) > *带标签的语句* → [*语句标签*](#statement-label) [*switch 语句*](#switch-statement)
>
> > *带标签的语句* → [*语句标签*](#statement-label) [*do 语句*](#sdo-statement) > > *带标签的语句* → [*语句标签*](#statement-label) [*do 语句*](#sdo-statement)
>
<a name="statement-label"></a> <a name="statement-label"></a>
> *语句标签* → [*标签名称*](#label-name) **:** > *语句标签* → [*标签名称*](#label-name) **:**
>
<a name="label-name"></a> <a name="label-name"></a>
> *标签名称* → [*标识符*](02_Lexical_Structure.md#identifier) > *标签名称* → [*标识符*](02_Lexical_Structure.md#identifier)
>
<a name="control_transfer_statements"></a> <a name="control_transfer_statements"></a>
## 控制转移语句 ## 控制转移语句
@ -337,13 +392,19 @@ case .suppressed:
控制转移语句能够无条件地把控制权从一片代码转移到另一片代码从而改变代码执行的顺序。Swift 提供五种类型的控制转移语句:`break` 语句、`continue` 语句、`fallthrough` 语句、`return` 语句和 `throw` 语句。 控制转移语句能够无条件地把控制权从一片代码转移到另一片代码从而改变代码执行的顺序。Swift 提供五种类型的控制转移语句:`break` 语句、`continue` 语句、`fallthrough` 语句、`return` 语句和 `throw` 语句。
> 控制转移语句语法 > 控制转移语句语法
>
> >
<a name="control-transfer-statement"></a> <a name="control-transfer-statement"></a>
> *控制转移语句* → [*break 语句*](#break-statement) > *控制转移语句* → [*break 语句*](#break-statement)
>
> *控制转移语句* → [*continue 语句*](#continue-statement) > *控制转移语句* → [*continue 语句*](#continue-statement)
>
> *控制转移语句* → [*fallthrough 语句*](#fallthrough-statement) > *控制转移语句* → [*fallthrough 语句*](#fallthrough-statement)
>
> *控制转移语句* → [*return 语句*](#return-statement) > *控制转移语句* → [*return 语句*](#return-statement)
>
> *控制转移语句* → [*throw 语句*](#throw-statement) > *控制转移语句* → [*throw 语句*](#throw-statement)
>
<a name="break_statement"></a> <a name="break_statement"></a>
### Break 语句 ### Break 语句
@ -351,7 +412,9 @@ case .suppressed:
`break` 语句用于终止循环语句、`if` 语句或 `switch` 语句的执行。使用 `break` 语句时,可以只写 `break` 这个关键词,也可以在 `break` 后面跟上标签名,像下面这样: `break` 语句用于终止循环语句、`if` 语句或 `switch` 语句的执行。使用 `break` 语句时,可以只写 `break` 这个关键词,也可以在 `break` 后面跟上标签名,像下面这样:
> break > break
>
> break `label name` > break `label name`
>
`break` 语句后面带标签名时,可用于终止由这个标签标记的循环语句、`if` 语句或 `switch` 语句的执行。 `break` 语句后面带标签名时,可用于终止由这个标签标记的循环语句、`if` 语句或 `switch` 语句的执行。
@ -362,9 +425,11 @@ case .suppressed:
关于使用 `break` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Break](../chapter2/05_Control_Flow.html#break) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。 关于使用 `break` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Break](../chapter2/05_Control_Flow.html#break) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
> break 语句语法 > break 语句语法
>
> >
<a name="break-statement"></a> <a name="break-statement"></a>
> *break 语句* → **break** [*标签名称*](#label-name)<sub>可选</sub> > *break 语句* → **break** [*标签名称*](#label-name)<sub>可选</sub>
>
<a name="continue_statement"></a> <a name="continue_statement"></a>
### Continue 语句 ### Continue 语句
@ -372,7 +437,9 @@ case .suppressed:
`continue` 语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用 `continue` 语句时,可以只写 `continue` 这个关键词,也可以在 `continue` 后面跟上标签名,像下面这样: `continue` 语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用 `continue` 语句时,可以只写 `continue` 这个关键词,也可以在 `continue` 后面跟上标签名,像下面这样:
> continue > continue
>
> continue `label name` > continue `label name`
>
`continue` 语句后面带标签名时,可用于终止由这个标签标记的循环中当前迭代的执行。 `continue` 语句后面带标签名时,可用于终止由这个标签标记的循环中当前迭代的执行。
@ -385,9 +452,11 @@ case .suppressed:
关于使用 `continue` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Continue](../chapter2/05_Control_Flow.html#continue) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。 关于使用 `continue` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Continue](../chapter2/05_Control_Flow.html#continue) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
> continue 语句语法 > continue 语句语法
>
> >
<a name="continue-statement"></a> <a name="continue-statement"></a>
> *continue 语句* → **continue** [*标签名称*](#label-name)<sub>可选</sub> > *continue 语句* → **continue** [*标签名称*](#label-name)<sub>可选</sub>
>
<a name="fallthrough_statements"></a> <a name="fallthrough_statements"></a>
### Fallthrough 语句 ### Fallthrough 语句
@ -399,9 +468,11 @@ case .suppressed:
关于在 `switch` 语句中使用 `fallthrough` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [控制转移语句](../chapter2/05_Control_Flow.html#control_transfer_statements)。 关于在 `switch` 语句中使用 `fallthrough` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [控制转移语句](../chapter2/05_Control_Flow.html#control_transfer_statements)。
> fallthrough 语句语法 > fallthrough 语句语法
>
> >
<a name="fallthrough-statement"></a> <a name="fallthrough-statement"></a>
> *fallthrough 语句* → **fallthrough** > *fallthrough 语句* → **fallthrough**
>
<a name="return_statements"></a> <a name="return_statements"></a>
### Return 语句 ### Return 语句
@ -411,20 +482,26 @@ case .suppressed:
使用 `return` 语句时,可以只写 `return` 这个关键词,也可以在 `return` 后面跟上表达式,像下面这样: 使用 `return` 语句时,可以只写 `return` 这个关键词,也可以在 `return` 后面跟上表达式,像下面这样:
> return > return
>
> return `expression` > return `expression`
>
`return` 语句后面带表达式时表达式的值将会返回给调用函数或方法。如果表达式的值的类型与函数或者方法声明的返回类型不匹配Swift 则会在返回表达式的值之前将表达式的值的类型转换为返回类型。 `return` 语句后面带表达式时表达式的值将会返回给调用函数或方法。如果表达式的值的类型与函数或者方法声明的返回类型不匹配Swift 则会在返回表达式的值之前将表达式的值的类型转换为返回类型。
> 注意 > 注意
>
> >
> 正如 [可失败构造器](05_Declarations.html#failable_initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。 > 正如 [可失败构造器](05_Declarations.html#failable_initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。
>
而只写 `return` 时,仅仅是从该函数或方法中返回,而不返回任何值(也就是说,函数或方法的返回类型为 `Void` 或者说 `()`)。 而只写 `return` 时,仅仅是从该函数或方法中返回,而不返回任何值(也就是说,函数或方法的返回类型为 `Void` 或者说 `()`)。
> return 语句语法 > return 语句语法
>
> >
<a name="return-statement"></a> <a name="return-statement"></a>
> *return 语句* → **return** [*表达式*](04_Expressions.html#expression)<sub>可选</sub> > *return 语句* → **return** [*表达式*](04_Expressions.html#expression)<sub>可选</sub>
>
<a name="throw_statements"></a> <a name="throw_statements"></a>
### Throw 语句 ### Throw 语句
@ -436,15 +513,18 @@ case .suppressed:
`throw` 语句由 `throw` 关键字紧跟一个表达式组成,如下所示: `throw` 语句由 `throw` 关键字紧跟一个表达式组成,如下所示:
> throw `expression` > throw `expression`
>
表达式的结果必须符合 `ErrorType` 协议。 表达式的结果必须符合 `ErrorType` 协议。
关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html) 一章的 [用 throwing 函数传递错误](../chapter2/18_Error_Handling.html#propagating_errors_using_throwing_functions)。 关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html) 一章的 [用 throwing 函数传递错误](../chapter2/18_Error_Handling.html#propagating_errors_using_throwing_functions)。
> throw 语句语法 > throw 语句语法
>
> >
<a name="throw-statement"></a> <a name="throw-statement"></a>
> *throw 语句* → **throw** [*表达式*](04_Expressions.html#expression) > *throw 语句* → **throw** [*表达式*](04_Expressions.html#expression)
>
<a name="defer_statements"></a> <a name="defer_statements"></a>
## Defer 语句 ## Defer 语句
@ -478,9 +558,11 @@ f()
`defer` 语句中的语句无法将控制权转移到 `defer` 语句外部。 `defer` 语句中的语句无法将控制权转移到 `defer` 语句外部。
> defer 语句语法 > defer 语句语法
>
> >
<a name="defer-statement"></a> <a name="defer-statement"></a>
> *延迟语句* → **defer** [*代码块*](05_Declarations.html#code-block) > *延迟语句* → **defer** [*代码块*](05_Declarations.html#code-block)
>
<a name="do_statements"></a> <a name="do_statements"></a>
## Do 语句 ## Do 语句
@ -509,13 +591,17 @@ do {
关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../chapter2/17_Error_Handling.html#handling_errors)。 关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../chapter2/17_Error_Handling.html#handling_errors)。
> do 语句语法 > do 语句语法
>
> >
<a name="do-statement"></a> <a name="do-statement"></a>
> *do 语句* → **do** [*代码块*](05_Declarations.html#code-block) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub> > *do 语句* → **do** [*代码块*](05_Declarations.html#code-block) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
>
<a name="catch-clauses"></a> <a name="catch-clauses"></a>
> *多条 catch 子句* → [*catch 子句*](#catch-clause) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub> > *多条 catch 子句* → [*catch 子句*](#catch-clause) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
>
<a name="catch-clause"></a> <a name="catch-clause"></a>
> *catch 子句* → **catch** [*模式*](07_Patterns.html#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.html#code-block) > *catch 子句* → **catch** [*模式*](07_Patterns.html#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.html#code-block)
>
<a name="compiler_control_statements"></a> <a name="compiler_control_statements"></a>
## 编译器控制语句 ## 编译器控制语句
@ -523,11 +609,15 @@ do {
编译器控制语句允许程序改变编译器的行为。Swift 有三种编译器控制语句:条件编译语句、线路控制语句和编译时诊断语句。 编译器控制语句允许程序改变编译器的行为。Swift 有三种编译器控制语句:条件编译语句、线路控制语句和编译时诊断语句。
> 编译器控制语句语法 > 编译器控制语句语法
>
> >
<a name="compiler-control-statement"></a> <a name="compiler-control-statement"></a>
> *编译器控制语句* → [*条件编译语句*](#grammar_conditional-compilation-block) > *编译器控制语句* → [*条件编译语句*](#grammar_conditional-compilation-block)
>
> *编译器控制语句* → [*线路控制语句*](#line-control-statement) > *编译器控制语句* → [*线路控制语句*](#line-control-statement)
>
> *编译器控制语句* → [*诊断语句*](#grammar_diagnostic-statement) > *编译器控制语句* → [*诊断语句*](#grammar_diagnostic-statement)
>
<a name="Conditional_Compilation_Block"></a> <a name="Conditional_Compilation_Block"></a>
### 条件编译代码块 ### 条件编译代码块
@ -577,8 +667,10 @@ print("Compiled with the Swift 5 compiler or later in a Swift mode earlier than
`targetEnvironment()` 当为模拟器编译时返回 `true`,否则返回 `false` `targetEnvironment()` 当为模拟器编译时返回 `true`,否则返回 `false`
> 注意 > 注意
>
> >
> `arch(arm)` 平台检测函数在 ARM 64 位设备上不会返回 `true`。如果代码在 32 位的 iOS 模拟器上编译,`arch(i386)` 平台检测函数会返回 `true`。 > `arch(arm)` 平台检测函数在 ARM 64 位设备上不会返回 `true`。如果代码在 32 位的 iOS 模拟器上编译,`arch(i386)` 平台检测函数会返回 `true`。
>
你可以使用逻辑操作符 `&&``||``!` 来组合多个编译配置,还可以使用圆括号来进行分组。 你可以使用逻辑操作符 `&&``||``!` 来组合多个编译配置,还可以使用圆括号来进行分组。
@ -595,63 +687,94 @@ statements to compile if both compilation conditions are false
``` ```
> 注意 > 注意
>
> >
> 即使没有被编译,编译配置中的语句仍然会被解析。然而,唯一的例外是编译配置语句中包含语言版本检测函数:仅当 `Swift` 编译器版本和语言版本检测函数中指定的版本号匹配时,语句才会被解析。这种设定能确保旧的编译器不会尝试去解析新 Swift 版本的语法。 > 即使没有被编译,编译配置中的语句仍然会被解析。然而,唯一的例外是编译配置语句中包含语言版本检测函数:仅当 `Swift` 编译器版本和语言版本检测函数中指定的版本号匹配时,语句才会被解析。这种设定能确保旧的编译器不会尝试去解析新 Swift 版本的语法。
>
<a name="build-config-statement"></a> <a name="build-config-statement"></a>
> 条件编译代码块语法 > 条件编译代码块语法
>
> >
<a name="grammar_conditional-compilation-block"></a> <a name="grammar_conditional-compilation-block"></a>
> *条件编译代码块* → [*if-directive 语句*](#grammar_if-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses)<sub>可选</sub> [*else-directive 语句*](#grammar_else-directive-clause)<sub>可选</sub> [*endif-directive*](#grammar_endif-directive) > *条件编译代码块* → [*if-directive 语句*](#grammar_if-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses)<sub>可选</sub> [*else-directive 语句*](#grammar_else-directive-clause)<sub>可选</sub> [*endif-directive*](#grammar_endif-directive)
>
<a name="grammar_if-directive-clause"></a> <a name="grammar_if-directive-clause"></a>
> *if-directive 语句* → [*if-directive*](#grammar_if-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub> > *if-directive 语句* → [*if-directive*](#grammar_if-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
>
<a name="grammar_elseif-directive-clauses"></a> <a name="grammar_elseif-directive-clauses"></a>
> *elseif-directive 语句(复数)* → [*elseif-directive 语句*](#grammar_elseif-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses) > *elseif-directive 语句(复数)* → [*elseif-directive 语句*](#grammar_elseif-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses)
>
<a name="grammar_elseif-directive-clauses"></a> <a name="grammar_elseif-directive-clauses"></a>
> *elseif-directive 语句* → [*elseif-directive*](#grammar_elseif-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub> > *elseif-directive 语句* → [*elseif-directive*](#grammar_elseif-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
>
<a name="grammar_else-directive-clause"></a> <a name="grammar_else-directive-clause"></a>
> *else-directive 语句* → [*else-directive*](#grammar_else-directive) [*语句(复数)*](#statements)<sub>可选</sub> > *else-directive 语句* → [*else-directive*](#grammar_else-directive) [*语句(复数)*](#statements)<sub>可选</sub>
>
> *if-directive* → **#if** > *if-directive* → **#if**
>
> *elseif-directive* → **#elseif** > *elseif-directive* → **#elseif**
>
> *else-directive* → **#else** > *else-directive* → **#else**
>
> *endif-directive* → **#endif** > *endif-directive* → **#endif**
>
<a name="compilation-condition"></a> <a name="compilation-condition"></a>
> *编译条件* → [*平台条件*](#grammar_platform-condition) > *编译条件* → [*平台条件*](#grammar_platform-condition)
>
> *编译条件* → [*标识符*](02_Lexical_Structure.md#identifier) > *编译条件* → [*标识符*](02_Lexical_Structure.md#identifier)
>
> *编译条件* → [*布尔值字面量*](02_Lexical_Structure.md#boolean-literal) > *编译条件* → [*布尔值字面量*](02_Lexical_Structure.md#boolean-literal)
>
> *编译条件* → **(** [*编译条件*](#compilation-condition) **)** > *编译条件* → **(** [*编译条件*](#compilation-condition) **)**
>
> *编译条件* → **!** [*编译条件*](#compilation-condition) > *编译条件* → **!** [*编译条件*](#compilation-condition)
>
> *编译条件* → [*编译条件*](#compilation-condition) **&&** [*编译条件*](#compilation-condition) > *编译条件* → [*编译条件*](#compilation-condition) **&&** [*编译条件*](#compilation-condition)
>
> *编译条件* → [*编译条件*](#compilation-condition) **||** [*编译条件*](#compilation-condition) > *编译条件* → [*编译条件*](#compilation-condition) **||** [*编译条件*](#compilation-condition)
>
<a name="grammar_platform-condition"></a> <a name="grammar_platform-condition"></a>
<a name="grammar_platform-condition-os"></a> <a name="grammar_platform-condition-os"></a>
> *平台条件* → **os ( [*操作系统*](#operating-system) )** > *平台条件* → **os ( [*操作系统*](#operating-system) )**
>
<a name="grammar_platform-condition-arch"></a> <a name="grammar_platform-condition-arch"></a>
> *平台条件* → **arch ( [*架构*](#architecture) )** > *平台条件* → **arch ( [*架构*](#architecture) )**
>
<a name="grammar_platform-condition-swift"></a> <a name="grammar_platform-condition-swift"></a>
> *平台条件* → **swift ( >= [*swift 版本*](#swift-version) )** | **swift ( < [*swift 版本*](#swift-version) )** > *平台条件* → **swift ( >= [*swift 版本*](#swift-version) )** | **swift ( < [*swift 版本*](#swift-version) )**
>
<a name="grammar_platform-condition-compiler"></a> <a name="grammar_platform-condition-compiler"></a>
> *平台条件* → **compiler ( >= [*swift 版本*](#swift-version) )** | **compiler ( < [*swift 版本*](#swift-version) )** > *平台条件* → **compiler ( >= [*swift 版本*](#swift-version) )** | **compiler ( < [*swift 版本*](#swift-version) )**
>
<a name="grammar_platform-condition-canImport"></a> <a name="grammar_platform-condition-canImport"></a>
> *平台条件* → **canImport ( [*模块名*](#grammar_module-name) )** > *平台条件* → **canImport ( [*模块名*](#grammar_module-name) )**
>
<a name="grammar_platform-condition-targetEnvironment"></a> <a name="grammar_platform-condition-targetEnvironment"></a>
> *平台条件* → **targetEnvironment ( [*环境*](#grammar_environment) )** > *平台条件* → **targetEnvironment ( [*环境*](#grammar_environment) )**
>
<a name="operating-system"></a> <a name="operating-system"></a>
> *操作系统* → **macOS** | **iOS** | **watchOS** | **tvOS** > *操作系统* → **macOS** | **iOS** | **watchOS** | **tvOS**
>
<a name="architecture"></a> <a name="architecture"></a>
> *架构* → **i386** | **x86_64** | **arm** | **arm64** > *架构* → **i386** | **x86_64** | **arm** | **arm64**
>
<a name="swift-version"></a> <a name="swift-version"></a>
> *swift 版本* → [*十进制数字*](02_Lexical_Structure.md#decimal-digit) ­**.** ­[*swift 版本延续*](#grammar_swift-version-continuation) <sub>可选</sub> > *swift 版本* → [*十进制数字*](02_Lexical_Structure.md#decimal-digit) ­**.** ­[*swift 版本延续*](#grammar_swift-version-continuation) <sub>可选</sub>
>
<a name="grammar_swift-version-continuation"></a> <a name="grammar_swift-version-continuation"></a>
> *swift 版本延续* → **.** [*十进制数字*](02_Lexical_Structure.md#decimal-digit) [*swift 版本延续*](#grammar_swift-version-continuation) <sub>可选</sub> > *swift 版本延续* → **.** [*十进制数字*](02_Lexical_Structure.md#decimal-digit) [*swift 版本延续*](#grammar_swift-version-continuation) <sub>可选</sub>
>
<a name="grammar_module-name"></a> <a name="grammar_module-name"></a>
> *模块名* → [*identifier*](02_Lexical_Structure.md#identifier) > *模块名* → [*identifier*](02_Lexical_Structure.md#identifier)
>
<a name="grammar_environment"></a> <a name="grammar_environment"></a>
> *环境* → **模拟器** > *环境* → **模拟器**
>
<a name="line_control_statements"></a> <a name="line_control_statements"></a>
### 行控制语句 ### 行控制语句
@ -661,8 +784,10 @@ statements to compile if both compilation conditions are false
行控制语句形式如下: 行控制语句形式如下:
> \#sourceLocation(file: `filename` , line:`line number`) > \#sourceLocation(file: `filename` , line:`line number`)
>
> \#sourceLocation() > \#sourceLocation()
>
第一种的行控制语句会改变该语句之后的代码中的字面量表达式 `#line``#file` 所表示的值。`行号` 是一个大于 0 的整形字面量,会改变 `#line` 表达式的值。`文件名` 是一个字符串字面量,会改变 `#file` 表达式的值。 第一种的行控制语句会改变该语句之后的代码中的字面量表达式 `#line``#file` 所表示的值。`行号` 是一个大于 0 的整形字面量,会改变 `#line` 表达式的值。`文件名` 是一个字符串字面量,会改变 `#file` 表达式的值。
@ -670,13 +795,18 @@ statements to compile if both compilation conditions are false
<a name="line-control-statement"></a> <a name="line-control-statement"></a>
> 行控制语句语法 > 行控制语句语法
>
> >
> *行控制语句* → **#sourceLocation(file:[*文件名*](#file-name),line:[*行号*](#line-number))** > *行控制语句* → **#sourceLocation(file:[*文件名*](#file-name),line:[*行号*](#line-number))**
>
> *行控制语句* → **#sourceLocation()** > *行控制语句* → **#sourceLocation()**
>
<a name="line-number"></a> <a name="line-number"></a>
> *行号* → 大于 0 的十进制整数 > *行号* → 大于 0 的十进制整数
>
<a name="file-name"></a> <a name="file-name"></a>
> *文件名* → [*静态字符串字面量*](02_Lexical_Structure.md#static-string-literal) > *文件名* → [*静态字符串字面量*](02_Lexical_Structure.md#static-string-literal)
>
### 编译时诊断语句 ### 编译时诊断语句
@ -690,12 +820,16 @@ statements to compile if both compilation conditions are false
第一句会抛出错误信息并终止编译,第二句会发出警告信息但是编译会继续进行。你可以通过静态字符串字面量来书写诊断信息,静态字符串字面量不能使用字符串 `interpolation` 或者 `concatenation`,但可以使用多行的形式。 第一句会抛出错误信息并终止编译,第二句会发出警告信息但是编译会继续进行。你可以通过静态字符串字面量来书写诊断信息,静态字符串字面量不能使用字符串 `interpolation` 或者 `concatenation`,但可以使用多行的形式。
> 编译时诊断语句语法 > 编译时诊断语句语法
>
> >
<a name="grammar_compile-time-diagnostic-statement"></a> <a name="grammar_compile-time-diagnostic-statement"></a>
> *诊断语句* → **#error** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)** > *诊断语句* → **#error** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)**
>
> *诊断语句* → **#warning** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)** > *诊断语句* → **#warning** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)**
>
<a name="grammar_diagnostic-message"></a> <a name="grammar_diagnostic-message"></a>
> *诊断语句* → [*静态字符串字面量*](02_Lexical_Structure.md#static-string-literal) > *诊断语句* → [*静态字符串字面量*](02_Lexical_Structure.md#static-string-literal)
>
<a name="availability_condition"></a> <a name="availability_condition"></a>
## 可用性条件 ## 可用性条件
@ -719,21 +853,33 @@ if #available(platform name version, ..., *) {
与布尔类型的条件不同,不能用逻辑运算符 `&&``||` 组合可用性条件。 与布尔类型的条件不同,不能用逻辑运算符 `&&``||` 组合可用性条件。
> 可用性条件语法 > 可用性条件语法
>
> >
<a name="availability-condition"></a> <a name="availability-condition"></a>
> *可用性条件* → **#available** **(** [*可用性参数列表*](#availability-arguments) **)** > *可用性条件* → **#available** **(** [*可用性参数列表*](#availability-arguments) **)**
>
<a name="availability-arguments"></a> <a name="availability-arguments"></a>
> *可用性参数列表* → [*可用性参数*](#availability-argument) | [*可用性参数*](#availability-argument) **,** [*可用性参数列表*](#availability-arguments) > *可用性参数列表* → [*可用性参数*](#availability-argument) | [*可用性参数*](#availability-argument) **,** [*可用性参数列表*](#availability-arguments)
>
<a name="availability-argument"></a> <a name="availability-argument"></a>
> *可用性参数* → [平台名称](#platform-name) [平台版本](#platform-version) > *可用性参数* → [平台名称](#platform-name) [平台版本](#platform-version)
>
> *可用性条件* → __*__ > *可用性条件* → __*__
>
> >
<a name="platform-name"></a> <a name="platform-name"></a>
> *平台名称* → **iOS** | **iOSApplicationExtension** > *平台名称* → **iOS** | **iOSApplicationExtension**
>
> *平台名称* → **OSX** | **macOSApplicationExtension** > *平台名称* → **OSX** | **macOSApplicationExtension**
>
> *平台名称* → **watchOS** > *平台名称* → **watchOS**
>
> *平台名称* → **tvOS** > *平台名称* → **tvOS**
>
<a name="platform-version"></a> <a name="platform-version"></a>
> *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) > *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits)
>
> *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits) > *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits)
>
> *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits) > *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits)
>

View File

@ -7,23 +7,41 @@
在 Swift 中,大多数声明在某种意义上讲也是定义,因为它们在声明时往往伴随着实现或初始化。由于协议并不提供实现,大多数协议成员仅仅只是声明而已。为了方便起见,也是因为这些区别在 Swift 中并不是很重要,“声明”这个术语同时包含了声明和定义两种含义。 在 Swift 中,大多数声明在某种意义上讲也是定义,因为它们在声明时往往伴随着实现或初始化。由于协议并不提供实现,大多数协议成员仅仅只是声明而已。为了方便起见,也是因为这些区别在 Swift 中并不是很重要,“声明”这个术语同时包含了声明和定义两种含义。
> 声明语法 > 声明语法
>
> <a name="declaration"></a> > <a name="declaration"></a>
>
> *声明* → [*导入声明*](#import-declaration) > *声明* → [*导入声明*](#import-declaration)
>
> *声明* → [*常量声明*](#constant-declaration) > *声明* → [*常量声明*](#constant-declaration)
>
> *声明* → [*变量声明*](#variable-declaration) > *声明* → [*变量声明*](#variable-declaration)
>
> *声明* → [*类型别名声明*](#typealias-declaration) > *声明* → [*类型别名声明*](#typealias-declaration)
>
> *声明* → [*函数声明*](#function-declaration) > *声明* → [*函数声明*](#function-declaration)
>
> *声明* → [*枚举声明*](#enum-declaration) > *声明* → [*枚举声明*](#enum-declaration)
>
> *声明* → [*结构体声明*](#struct-declaration) > *声明* → [*结构体声明*](#struct-declaration)
>
> *声明* → [*类声明*](#class-declaration) > *声明* → [*类声明*](#class-declaration)
>
> *声明* → [*协议声明*](#protocol-declaration) > *声明* → [*协议声明*](#protocol-declaration)
>
> *声明* → [*构造器声明*](#initializer-declaration) > *声明* → [*构造器声明*](#initializer-declaration)
>
> *声明* → [*析构器声明*](#deinitializer-declaration) > *声明* → [*析构器声明*](#deinitializer-declaration)
>
> *声明* → [*扩展声明*](#extension-declaration) > *声明* → [*扩展声明*](#extension-declaration)
>
> *声明* → [*下标声明*](#subscript-declaration) > *声明* → [*下标声明*](#subscript-declaration)
>
> *声明* → [*运算符声明*](#operator-declaration) > *声明* → [*运算符声明*](#operator-declaration)
>
> <a name="declarations"></a> > <a name="declarations"></a>
>
> *多条声明* → [*声明*](#declaration) [*多条声明*](#declarations)<sub>可选</sub> > *多条声明* → [*声明*](#declaration) [*多条声明*](#declarations)<sub>可选</sub>
>
<a name="top-level_code"></a> <a name="top-level_code"></a>
## 顶级代码 ## 顶级代码
@ -33,6 +51,7 @@ Swift 的源文件中的顶级代码top-level code由零个或多个语句
> 顶级声明语法 > 顶级声明语法
> >
> *顶级声明* → [*多条语句*](10_Statements.html#statements)<sub>可选</sub> > *顶级声明* → [*多条语句*](10_Statements.html#statements)<sub>可选</sub>
>
<a name="code_blocks"></a> <a name="code_blocks"></a>
## 代码块 ## 代码块
@ -50,7 +69,9 @@ Swift 的源文件中的顶级代码top-level code由零个或多个语句
> 代码块语法 > 代码块语法
> >
> <a name="code-block"></a> > <a name="code-block"></a>
>
> *代码块* → **{** [*多条语句*](10_Statements.html#statements)<sub>可选</sub> **}** > *代码块* → **{** [*多条语句*](10_Statements.html#statements)<sub>可选</sub> **}**
>
<a name="import_declaration"></a> <a name="import_declaration"></a>
## 导入声明 ## 导入声明
@ -72,14 +93,21 @@ import 模块.子模块
> 导入声明语法 > 导入声明语法
> >
> <a name="import-declaration"></a> > <a name="import-declaration"></a>
>
> *导入声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **import** [*导入类型*](#import-kind)<sub>可选</sub> [*导入路径*](#import-path) > *导入声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **import** [*导入类型*](#import-kind)<sub>可选</sub> [*导入路径*](#import-path)
> >
> <a name="import-kind"></a> > <a name="import-kind"></a>
>
> *导入类型* → **typealias** | **struct** | **class** | **enum** | **protocol** | **let** | **var** | **func** > *导入类型* → **typealias** | **struct** | **class** | **enum** | **protocol** | **let** | **var** | **func**
>
> <a name="import-path"></a> > <a name="import-path"></a>
>
> *导入路径* → [*导入路径标识符*](#import-path-identifier) | [*导入路径标识符*](#import-path-identifier) **.** [*导入路径*](#import-path) > *导入路径* → [*导入路径标识符*](#import-path-identifier) | [*导入路径标识符*](#import-path-identifier) **.** [*导入路径*](#import-path)
>
> <a name="import-path-identifier"></a> > <a name="import-path-identifier"></a>
>
> *导入路径标识符* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator) > *导入路径标识符* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator)
>
<a name="constant_declaration"></a> <a name="constant_declaration"></a>
## 常量声明 ## 常量声明
@ -119,13 +147,21 @@ print("The second number is \(secondNumber).")
> 常量声明语法 > 常量声明语法
> >
> <a name="constant-declaration"></a> > <a name="constant-declaration"></a>
>
> *常量声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **let** [*模式构造器列表*](pattern-initializer-list) > *常量声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **let** [*模式构造器列表*](pattern-initializer-list)
>
> <a name="pattern-initializer-list"></a> > <a name="pattern-initializer-list"></a>
>
> *模式构造器列表* → [*模式构造器*](#pattern-initializer) | [*模式构造器*](#pattern-initializer) **,** [*模式构造器列表*](#pattern-initializer-list) > *模式构造器列表* → [*模式构造器*](#pattern-initializer) | [*模式构造器*](#pattern-initializer) **,** [*模式构造器列表*](#pattern-initializer-list)
>
> <a name="pattern-initializer"></a> > <a name="pattern-initializer"></a>
>
> *模式构造器* → [*模式*](07_Patterns.html#pattern) [*构造器*](#initializer)<sub>可选</sub> > *模式构造器* → [*模式*](07_Patterns.html#pattern) [*构造器*](#initializer)<sub>可选</sub>
>
> <a name="initializer"></a> > <a name="initializer"></a>
>
> *构造器* → **=** [*表达式*](04_Expressions.html#expression) > *构造器* → **=** [*表达式*](04_Expressions.html#expression)
>
<a name="variable_declaration"></a> <a name="variable_declaration"></a>
@ -138,6 +174,7 @@ print("The second number is \(secondNumber).")
> 注意 > 注意
> >
> 也可以在协议声明中声明属性,详情请参阅 [协议属性声明](#protocol_property_declaration)。 > 也可以在协议声明中声明属性,详情请参阅 [协议属性声明](#protocol_property_declaration)。
>
可以在子类中重写继承来的变量属性,使用 `override` 声明修饰符标记属性的声明即可,详情请参阅 [重写](../chapter2/13_Inheritance.html#overriding)。 可以在子类中重写继承来的变量属性,使用 `override` 声明修饰符标记属性的声明即可,详情请参阅 [重写](../chapter2/13_Inheritance.html#overriding)。
@ -225,49 +262,80 @@ var 变量名称: 类型 = 表达式 {
> 注意 > 注意
> >
> 在一个类声明中,使用关键字 `static` 与同时使用 `class` 和 `final` 去标记一个声明的效果相同。 > 在一个类声明中,使用关键字 `static` 与同时使用 `class` 和 `final` 去标记一个声明的效果相同。
>
<a name="grammer_of_a_variable_declaration"></a> <a name="grammer_of_a_variable_declaration"></a>
> 变量声明语法 > 变量声明语法
> >
<a name="variable-declaration"></a> <a name="variable-declaration"></a>
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*模式构造器列表*](#pattern-initializer-list) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*模式构造器列表*](#pattern-initializer-list)
>
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*代码块*](#code-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*代码块*](#code-block)
>
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 代码块*](#getter-setter-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 代码块*](#getter-setter-block)
>
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
>
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*构造器*](#initializer) [*willSet-didSet 代码块*](#willSet-didSet-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*构造器*](#initializer) [*willSet-didSet 代码块*](#willSet-didSet-block)
>
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*构造器*](#initializer)<sub>可选</sub> [*willSet-didSet 代码块*](#willSet-didSet-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*构造器*](#initializer)<sub>可选</sub> [*willSet-didSet 代码块*](#willSet-didSet-block)
>
<a name="variable-declaration-head"></a> <a name="variable-declaration-head"></a>
> *变量声明头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **var** > *变量声明头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **var**
>
> <a name="variable-name"></a> > <a name="variable-name"></a>
>
> *变量名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *变量名称* → [*标识符*](02_Lexical_Structure.html#identifier)
>
<a name="getter-setter-block"></a> <a name="getter-setter-block"></a>
> *getter-setter 代码块* → [*代码块*](#code-block) > *getter-setter 代码块* → [*代码块*](#code-block)
>
> *getter-setter 代码块* → **{** [*getter 子句*](#getter-clause) [*setter 子句*](#setter-clause)<sub>可选</sub> **}** > *getter-setter 代码块* → **{** [*getter 子句*](#getter-clause) [*setter 子句*](#setter-clause)<sub>可选</sub> **}**
>
> *getter-setter 代码块* → **{** [*setter 子句*](#setter-clause) [*getter 子句*](#getter-clause) **}** > *getter-setter 代码块* → **{** [*setter 子句*](#setter-clause) [*getter 子句*](#getter-clause) **}**
>
> <a name="getter-clause"></a> > <a name="getter-clause"></a>
>
> *getter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get** [*代码块*](#code-block) > *getter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get** [*代码块*](#code-block)
>
> <a name="setter-clause"></a> > <a name="setter-clause"></a>
>
> *setter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block) > *setter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
>
> <a name="setter-name"></a> > <a name="setter-name"></a>
>
> *setter 名称* → **(** [*标识符*](02_Lexical_Structure.html#identifier) **)** > *setter 名称* → **(** [*标识符*](02_Lexical_Structure.html#identifier) **)**
>
<a name="getter-setter-keyword-block"></a> <a name="getter-setter-keyword-block"></a>
> *getter-setter 关键字代码块* → **{** [*getter 关键字子句*](#getter-keyword-clause) [*setter 关键字子句*](#setter-keyword-clause)<sub>可选</sub> **}** > *getter-setter 关键字代码块* → **{** [*getter 关键字子句*](#getter-keyword-clause) [*setter 关键字子句*](#setter-keyword-clause)<sub>可选</sub> **}**
>
> *getter-setter 关键字代码块* → **{** [*setter 关键字子句*](#setter-keyword-clause) [*getter 关键字子句*](#getter-keyword-clause) **}** > *getter-setter 关键字代码块* → **{** [*setter 关键字子句*](#setter-keyword-clause) [*getter 关键字子句*](#getter-keyword-clause) **}**
>
> <a name="getter-keyword-clause"></a> > <a name="getter-keyword-clause"></a>
>
> *getter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get** > *getter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get**
>
> <a name="setter-keyword-clause"></a> > <a name="setter-keyword-clause"></a>
>
> *setter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set** > *setter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set**
>
<a name="willSet-didSet-block"></a> <a name="willSet-didSet-block"></a>
> *willSet-didSet 代码块* → **{** [*willSet 子句*](#willSet-clause) [*didSet 子句*](#didSet-clause)<sub>可选</sub> **}** > *willSet-didSet 代码块* → **{** [*willSet 子句*](#willSet-clause) [*didSet 子句*](#didSet-clause)<sub>可选</sub> **}**
>
> *willSet-didSet 代码块* → **{** [*didSet 子句*](#didSet-clause) [*willSet 子句*](#willSet-clause)<sub>可选</sub> **}** > *willSet-didSet 代码块* → **{** [*didSet 子句*](#didSet-clause) [*willSet 子句*](#willSet-clause)<sub>可选</sub> **}**
>
> <a name="willSet-clause"></a> > <a name="willSet-clause"></a>
>
> *willSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **willSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block) > *willSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **willSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
>
> <a name="didSet-clause"></a> > <a name="didSet-clause"></a>
>
> *didSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **didSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block) > *didSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **didSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
>
<a name="type_alias_declaration"></a> <a name="type_alias_declaration"></a>
## 类型别名声明 ## 类型别名声明
@ -284,16 +352,20 @@ typealias 类型别名 = 现存类型
```swift ```swift
typealias StringDictionary<Value> = Dictionary<String, Value> typealias StringDictionary<Value> = Dictionary<String, Value>
>
// 下列两个字典拥有同样的类型 // 下列两个字典拥有同样的类型
var dictionary1: StringDictionary<Int> = [:] var dictionary1: StringDictionary<Int> = [:]
>
var dictionary2: Dictionary<String, Int> = [:] var dictionary2: Dictionary<String, Int> = [:]
>
``` ```
当一个类型别名带着泛型参数一起被声明时,这些参数的约束必须与现有参数的约束完全匹配。例如: 当一个类型别名带着泛型参数一起被声明时,这些参数的约束必须与现有参数的约束完全匹配。例如:
```swift ```swift
typealias DictionaryOfInts<Key: Hashable> = Dictionary<Key, Int> typealias DictionaryOfInts<Key: Hashable> = Dictionary<Key, Int>
>
``` ```
因为类型别名可以和现有类型相互交换使用,类型别名不可以引入额外的类型约束。 因为类型别名可以和现有类型相互交换使用,类型别名不可以引入额外的类型约束。
@ -313,6 +385,7 @@ protocol Sequence {
} }
func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int { func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
>
// ... // ...
} }
``` ```
@ -323,13 +396,20 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
<a name="grammer_of_a_type_alias_declaration"></a> <a name="grammer_of_a_type_alias_declaration"></a>
> 类型别名声明语法 > 类型别名声明语法
>
> >
> <a name="typealias-declaration"></a> > <a name="typealias-declaration"></a>
>
> *类型别名声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **typealias** [*类型别名名称*](#typealias-name) [*类型别子句*](#typealias-clause) [*类型别名赋值*](#typealias-assignment) > *类型别名声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **typealias** [*类型别名名称*](#typealias-name) [*类型别子句*](#typealias-clause) [*类型别名赋值*](#typealias-assignment)
>
> <a name="typealias-name"></a> > <a name="typealias-name"></a>
>
> *类型别名名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *类型别名名称* → [*标识符*](02_Lexical_Structure.html#identifier)
>
> <a name="typealias-assignment"></a> > <a name="typealias-assignment"></a>
>
> *类型别名赋值* → **=** [*类型*](03_Types.html#type) > *类型别名赋值* → **=** [*类型*](03_Types.html#type)
>
<a name="function_declaration"></a> <a name="function_declaration"></a>
## 函数声明 ## 函数声明
@ -338,6 +418,7 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
```swift ```swift
func 函数名称(参数列表) -> 返回类型 { func 函数名称(参数列表) -> 返回类型 {
>
语句 语句
} }
``` ```
@ -371,6 +452,7 @@ func 函数名称(参数列表) {
```swift ```swift
func f(x: Int, y: Int) -> Int { return x + y } func f(x: Int, y: Int) -> Int { return x + y }
>
f(x: 1, y: 2) // 参数 x 和 y 都有标签 f(x: 1, y: 2) // 参数 x 和 y 都有标签
``` ```
@ -411,6 +493,7 @@ repeatGreeting("Hello, world!", count: 2) // count 有标签, greeting 没有
```swift ```swift
func someFunction(a: inout Int) -> () -> Int { func someFunction(a: inout Int) -> () -> Int {
>
return { [a] in return a + 1 } return { [a] in return a + 1 }
} }
``` ```
@ -453,6 +536,7 @@ _ : 参数类型
```swift ```swift
func f(x: Int = 42) -> Int { return x } func f(x: Int = 42) -> Int { return x }
>
f() // 有效,使用默认值 f() // 有效,使用默认值
f(7) // 有效,提供了值 f(7) // 有效,提供了值
f(x: 7) // 无效,该参数没有外部名称 f(x: 7) // 无效,该参数没有外部名称
@ -474,6 +558,7 @@ f(x: 7) // 无效,该参数没有外部名称
```swift ```swift
func 函数名称(参数列表) throws -> 返回类型 { func 函数名称(参数列表) throws -> 返回类型 {
>
语句 语句
} }
``` ```
@ -493,6 +578,7 @@ func 函数名称(参数列表) throws -> 返回类型 {
```swift ```swift
func someFunction(callback: () throws -> Void) rethrows { func someFunction(callback: () throws -> Void) rethrows {
>
try callback() try callback()
} }
``` ```
@ -504,6 +590,7 @@ func alwaysThrows() throws {
throw SomeError.error throw SomeError.error
} }
func someFunction(callback: () throws -> Void) rethrows { func someFunction(callback: () throws -> Void) rethrows {
>
do { do {
try callback() try callback()
try alwaysThrows() // 非法, alwaysThrows() 不是一个抛出函数类型的参数 try alwaysThrows() // 非法, alwaysThrows() 不是一个抛出函数类型的参数
@ -529,35 +616,60 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
> >
<a name="function-declaration"></a> <a name="function-declaration"></a>
> *函数声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause) [*函数体*](#function-body)<sub>可选</sub> > *函数声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause) [*函数体*](#function-body)<sub>可选</sub>
>
<a name="function-head"></a> <a name="function-head"></a>
> *函数头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **func** > *函数头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **func**
>
> <a name="function-name"></a> > <a name="function-name"></a>
>
> *函数名* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator) > *函数名* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator)
>
> >
> <a name="function-signature"></a> > <a name="function-signature"></a>
>
> >
> *函数签名* → [*参数子句列表*](#parameter-clauses) **throws**<sub>可选</sub> [*函数结果*](#function-result)<sub>可选</sub> > *函数签名* → [*参数子句列表*](#parameter-clauses) **throws**<sub>可选</sub> [*函数结果*](#function-result)<sub>可选</sub>
>
> *函数签名* → [*参数子句列表*](#parameter-clauses) **rethrows** [*函数结果*](#function-result)<sub>可选</sub> > *函数签名* → [*参数子句列表*](#parameter-clauses) **rethrows** [*函数结果*](#function-result)<sub>可选</sub>
>
> <a name="function-result"></a> > <a name="function-result"></a>
>
> *函数结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type) > *函数结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type)
>
> <a name="function-body"></a> > <a name="function-body"></a>
>
> *函数体* → [*代码块*](#code-block) > *函数体* → [*代码块*](#code-block)
>
> <a name="parameter-clause"></a> > <a name="parameter-clause"></a>
>
> *参数子句* → **(** **)** | **(** [*参数列表*](#parameter-list) **)** > *参数子句* → **(** **)** | **(** [*参数列表*](#parameter-list) **)**
>
> <a name="parameter-list"></a> > <a name="parameter-list"></a>
>
> *参数列表* → [*参数*](#parameter) | [*参数*](#parameter) **,** [*参数列表*](#parameter-list) > *参数列表* → [*参数*](#parameter) | [*参数*](#parameter) **,** [*参数列表*](#parameter-list)
>
> <a name="parameter"></a> > <a name="parameter"></a>
>
> *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) [*默认参数子句*](#default-argument-clause)<sub>可选</sub> > *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) [*默认参数子句*](#default-argument-clause)<sub>可选</sub>
>
> *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) > *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation)
>
> *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) **...** > *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) **...**
>
> <a name="external-parameter-name"></a> > <a name="external-parameter-name"></a>
>
> *外部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_** > *外部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_**
>
> <a name="local-parameter-name"></a> > <a name="local-parameter-name"></a>
>
> *内部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_** > *内部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_**
>
> <a name="default-argument-clause"></a> > <a name="default-argument-clause"></a>
>
> *默认参数子句* → **=** [*表达式*](04_Expressions.html#expression) > *默认参数子句* → **=** [*表达式*](04_Expressions.html#expression)
>
<a name="enumeration_declaration"></a> <a name="enumeration_declaration"></a>
@ -600,6 +712,7 @@ enum Number {
} }
// f 的类型为 (Int) -> Number // f 的类型为 (Int) -> Number
>
let f = Number.integer let f = Number.integer
// 利用 f 把一个整数数组转成 Number 数组 // 利用 f 把一个整数数组转成 Number 数组
@ -617,6 +730,7 @@ let evenInts: [Number] = [0, 2, 4, 6].map(f)
```swift ```swift
enum Tree<T> { enum Tree<T> {
>
case empty case empty
indirect case node(value: T, left: Tree, right:Tree) indirect case node(value: T, left: Tree, right:Tree)
} }
@ -671,44 +785,79 @@ enum GamePlayMode: String {
<a name="grammer_of_an_enumeration_declaration"></a> <a name="grammer_of_an_enumeration_declaration"></a>
> 枚举声明语法 > 枚举声明语法
>
> >
> <a name="enum-declaration"></a> > <a name="enum-declaration"></a>
>
> *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> [*联合风格枚举*](#union-style-enum) > *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> [*联合风格枚举*](#union-style-enum)
>
> *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> [*原始值风格枚举*](#raw-value-style-enum) > *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> [*原始值风格枚举*](#raw-value-style-enum)
>
> >
> *联合风格枚举* → **indirect**<sub>可选</sub> **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause)<sub>可选</sub> **{** [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub> **}** > *联合风格枚举* → **indirect**<sub>可选</sub> **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause)<sub>可选</sub> **{** [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub> **}**
>
> <a name="union-style-enum-members"></a> > <a name="union-style-enum-members"></a>
>
> *多个联合风格枚举成员* → [*联合风格枚举成员*](#union-style-enum-member) [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub> > *多个联合风格枚举成员* → [*联合风格枚举成员*](#union-style-enum-member) [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub>
>
> <a name="union-style-enum-member"></a> > <a name="union-style-enum-member"></a>
>
> *联合风格枚举成员* → [*声明*](#declaration) | [*联合风格枚举用例子句*](#union-style-enum-case-clause) | [*编译控制流语句*](05_Statements.html#compiler-control-statement) > *联合风格枚举成员* → [*声明*](#declaration) | [*联合风格枚举用例子句*](#union-style-enum-case-clause) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
>
> <a name="union-style-enum-case-clause"></a> > <a name="union-style-enum-case-clause"></a>
>
> *联合风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **indirect**<sub>可选</sub> **case** [*联合风格枚举用例列表*](#union-style-enum-case-list) > *联合风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **indirect**<sub>可选</sub> **case** [*联合风格枚举用例列表*](#union-style-enum-case-list)
>
> <a name="union-style-enum-case-list"></a> > <a name="union-style-enum-case-list"></a>
>
> *联合风格枚举用例列表* → [*联合风格枚举用例*](#union-style-enum-case) | [*联合风格枚举用例*](#union-style-enum-case) **,** [*联合风格枚举用例列表*](#union-style-enum-case-list) > *联合风格枚举用例列表* → [*联合风格枚举用例*](#union-style-enum-case) | [*联合风格枚举用例*](#union-style-enum-case) **,** [*联合风格枚举用例列表*](#union-style-enum-case-list)
>
> <a name="union-style-enum-case"></a> > <a name="union-style-enum-case"></a>
>
> *联合风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*元组类型*](03_Types.html#tuple-type)<sub>可选</sub> > *联合风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*元组类型*](03_Types.html#tuple-type)<sub>可选</sub>
>
> <a name="enum-name"></a> > <a name="enum-name"></a>
>
> *枚举名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *枚举名称* → [*标识符*](02_Lexical_Structure.html#identifier)
>
> <a name="enum-case-name"></a> > <a name="enum-case-name"></a>
>
> *枚举用例名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *枚举用例名称* → [*标识符*](02_Lexical_Structure.html#identifier)
>
> >
> <a name="raw-value-style-enum"></a> > <a name="raw-value-style-enum"></a>
>
> >
> *原始值风格枚举* → **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause) **{** [*多个原始值风格枚举成员*](#raw-value-style-enum-members) **}** > *原始值风格枚举* → **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause) **{** [*多个原始值风格枚举成员*](#raw-value-style-enum-members) **}**
>
> <a name="raw-value-style-enum-members"></a> > <a name="raw-value-style-enum-members"></a>
>
> *多个原始值风格枚举成员* → [*原始值风格枚举成员*](#raw-value-style-enum-member) [*多个原始值风格枚举成员*](#raw-value-style-enum-members)<sub>可选</sub> > *多个原始值风格枚举成员* → [*原始值风格枚举成员*](#raw-value-style-enum-member) [*多个原始值风格枚举成员*](#raw-value-style-enum-members)<sub>可选</sub>
>
> <a name="raw-value-style-enum-member"></a> > <a name="raw-value-style-enum-member"></a>
>
> *原始值风格枚举成员* → [*声明*](#declaration) | [*原始值风格枚举用例子句*](#raw-value-style-enum-case-clause) | [*编译控制流语句*](05_Statements.html#compiler-control-statement) > *原始值风格枚举成员* → [*声明*](#declaration) | [*原始值风格枚举用例子句*](#raw-value-style-enum-case-clause) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
>
> <a name="raw-value-style-enum-case-clause"></a> > <a name="raw-value-style-enum-case-clause"></a>
>
> *原始值风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **case** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list) > *原始值风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **case** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list)
>
> <a name="raw-value-style-enum-case-list"></a> > <a name="raw-value-style-enum-case-list"></a>
>
> *原始值风格枚举用例列表* → [*原始值风格枚举用例*](#raw-value-style-enum-case) | [*原始值风格枚举用例*](#raw-value-style-enum-case) **,** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list) > *原始值风格枚举用例列表* → [*原始值风格枚举用例*](#raw-value-style-enum-case) | [*原始值风格枚举用例*](#raw-value-style-enum-case) **,** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list)
>
> <a name="raw-value-style-enum-case"></a> > <a name="raw-value-style-enum-case"></a>
>
> *原始值风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*原始值赋值*](#raw-value-assignment)<sub>可选</sub> > *原始值风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*原始值赋值*](#raw-value-assignment)<sub>可选</sub>
>
> <a name="raw-value-assignment"></a> > <a name="raw-value-assignment"></a>
>
> *原始值赋值* → **=** [*原始值字面量*](#raw-value-literal) > *原始值赋值* → **=** [*原始值字面量*](#raw-value-literal)
>
> <a name="raw-value-literal"></a> > <a name="raw-value-literal"></a>
>
> *原始值字面量* → [数字型字面量](02_Lexical_Structure.html#numeric-literal) | [字符串型字面量](02_Lexical_Structure.html#static-string-literal) | [布尔型字面量](02_Lexical_Structure.html#boolean-literal) > *原始值字面量* → [数字型字面量](02_Lexical_Structure.html#numeric-literal) | [字符串型字面量](02_Lexical_Structure.html#static-string-literal) | [布尔型字面量](02_Lexical_Structure.html#boolean-literal)
>
<a name="structure_declaration"></a> <a name="structure_declaration"></a>
## 结构体声明 ## 结构体声明
@ -744,18 +893,29 @@ struct 结构体名称: 采纳的协议 {
<a name="grammer_of_a_structure_declaration"></a> <a name="grammer_of_a_structure_declaration"></a>
> 结构体声明语法 > 结构体声明语法
>
> >
> <a name="struct-declaration"></a> > <a name="struct-declaration"></a>
>
> *结构体声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> **struct** [*结构体名称*](#struct-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*结构体主体*](#struct-body) > *结构体声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> **struct** [*结构体名称*](#struct-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*结构体主体*](#struct-body)
>
> <a name="struct-name"></a> > <a name="struct-name"></a>
>
> *结构体名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *结构体名称* → [*标识符*](02_Lexical_Structure.html#identifier)
>
> <a name="struct-body"></a> > <a name="struct-body"></a>
>
> *结构体主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}** > *结构体主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
>
> <a name="struct-name"></a> > <a name="struct-name"></a>
>
> >
> *结构体多个成员* → [*结构体成员*](#struct-member) [*结构体多个成员*](#struct-members)<sub>可选</sub> > *结构体多个成员* → [*结构体成员*](#struct-member) [*结构体多个成员*](#struct-members)<sub>可选</sub>
>
> <a name="struct-member"></a> > <a name="struct-member"></a>
>
> *结构体成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.html#compiler-control-statement) > *结构体成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
>
<a name="class_declaration"></a> <a name="class_declaration"></a>
@ -795,18 +955,29 @@ class 类名: 超类, 采纳的协议 {
<a name="grammer_of_a_class_declaration"></a> <a name="grammer_of_a_class_declaration"></a>
> 类声明语法 > 类声明语法
>
> >
> <a name="class-declaration"></a> > <a name="class-declaration"></a>
>
> *类声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **final**<sub>可选</sub> **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*类主体*](#class-body) > *类声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **final**<sub>可选</sub> **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*类主体*](#class-body)
>
> *类声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **final** [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*类主体*](#class-body) > *类声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **final** [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*类主体*](#class-body)
>
> <a name="class-name"></a> > <a name="class-name"></a>
>
> *类名* → [*标识符*](02_Lexical_Structure.html#identifier) > *类名* → [*标识符*](02_Lexical_Structure.html#identifier)
>
> <a name="class-body"></a> > <a name="class-body"></a>
>
> *类主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}** > *类主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
>
> >
> *类多个成员* → [*类成员*](#class-member) [*类多个成员*](#class-members)<sub>可选</sub> > *类多个成员* → [*类成员*](#class-member) [*类多个成员*](#class-members)<sub>可选</sub>
>
> <a name="class-member"></a> > <a name="class-member"></a>
>
> *类成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.html#compiler-control-statement) > *类成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
>
<a name="protocol_declaration"></a> <a name="protocol_declaration"></a>
## 协议声明 ## 协议声明
@ -826,6 +997,7 @@ protocol 协议名称: 继承的协议 {
> 注意 > 注意
> >
> 也可以使用协议合成类型来聚合多个协议的一致性要求,请参阅 [协议合成类型](03_Types.html#protocol_composition_type) 和 [协议合成](../chapter2/21_Protocols.html#protocol_composition)。 > 也可以使用协议合成类型来聚合多个协议的一致性要求,请参阅 [协议合成类型](03_Types.html#protocol_composition_type) 和 [协议合成](../chapter2/21_Protocols.html#protocol_composition)。
>
可以通过类型的扩展声明来采纳协议,从而为之前声明的类型添加协议一致性。在扩展中,必须实现所有采纳协议的要求。如果该类型已经实现了所有的要求,可以让这个扩展声明的主体留空。 可以通过类型的扩展声明来采纳协议,从而为之前声明的类型添加协议一致性。在扩展中,必须实现所有采纳协议的要求。如果该类型已经实现了所有的要求,可以让这个扩展声明的主体留空。
@ -844,6 +1016,7 @@ protocol SomeProtocol: AnyObject {
> 注意 > 注意
> >
> 如果协议已经用 `objc` 特性标记了,`AnyObject` 要求就隐式地应用于该协议,无需显式使用 `AnyObject` 关键字。 > 如果协议已经用 `objc` 特性标记了,`AnyObject` 要求就隐式地应用于该协议,无需显式使用 `AnyObject` 关键字。
>
协议类型是命名的类型,因此它们可以像其他命名类型一样使用,正如 [协议作为类型](../chapter2/21_Protocols.html#protocols_as_types) 所讨论的。然而,不能构造一个协议的实例,因为协议实际上不提供它们指定的要求的实现。 协议类型是命名的类型,因此它们可以像其他命名类型一样使用,正如 [协议作为类型](../chapter2/21_Protocols.html#protocols_as_types) 所讨论的。然而,不能构造一个协议的实例,因为协议实际上不提供它们指定的要求的实现。
@ -851,26 +1024,44 @@ protocol SomeProtocol: AnyObject {
<a name="grammer_of_a_protocol_declaration"></a> <a name="grammer_of_a_protocol_declaration"></a>
> 协议声明语法 > 协议声明语法
>
> >
> <a name="protocol-declaration"></a> > <a name="protocol-declaration"></a>
>
> *协议声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*协议主体*](#protocol-body) > *协议声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*协议主体*](#protocol-body)
>
> <a name="protocol-name"></a> > <a name="protocol-name"></a>
>
> *协议名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *协议名称* → [*标识符*](02_Lexical_Structure.html#identifier)
>
> <a name="protocol-body"></a> > <a name="protocol-body"></a>
>
> *协议主体* → **{** [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub> **}** > *协议主体* → **{** [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub> **}**
>
> >
> *协议多个成员* → [*协议成员*](#protocol-member) [*协议多个成员*](#protocol-members)<sub>可选</sub> > *协议多个成员* → [*协议成员*](#protocol-member) [*协议多个成员*](#protocol-members)<sub>可选</sub>
>
> <a name="protocol-member"></a> > <a name="protocol-member"></a>
>
> *协议成员* → [*协议成员声明*](#protocol-member-declaration) | [*编译控制流语句*](05_Statements.html#compiler-control-statement) > *协议成员* → [*协议成员声明*](#protocol-member-declaration) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
>
> >
> <a name="protocol-member-declaration"></a> > <a name="protocol-member-declaration"></a>
>
> *协议成员声明* → [*协议属性声明*](#protocol-property-declaration) > *协议成员声明* → [*协议属性声明*](#protocol-property-declaration)
>
> *协议成员声明* → [*协议方法声明*](#protocol-method-declaration) > *协议成员声明* → [*协议方法声明*](#protocol-method-declaration)
>
> *协议成员声明* → [*协议构造器声明*](#protocol-initializer-declaration) > *协议成员声明* → [*协议构造器声明*](#protocol-initializer-declaration)
>
> *协议成员声明* → [*协议下标声明*](#protocol-subscript-declaration) > *协议成员声明* → [*协议下标声明*](#protocol-subscript-declaration)
>
> *协议成员声明* → [*协议关联类型声明*](#protocol-associated-type-declaration) > *协议成员声明* → [*协议关联类型声明*](#protocol-associated-type-declaration)
>
> <a name="protocol-member-declarations"></a> > <a name="protocol-member-declarations"></a>
>
> *协议成员声明列表* → [*协议成员声明*](#protocol-member-declaration) [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub> > *协议成员声明列表* → [*协议成员声明*](#protocol-member-declaration) [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub>
>
<a name="protocol_property_declaration"></a> <a name="protocol_property_declaration"></a>
### 协议属性声明 ### 协议属性声明
@ -891,7 +1082,9 @@ var 属性名: 类型 { get set }
> 协议属性声明语法 > 协议属性声明语法
> >
> <a name="protocol-property-declaration"></a> > <a name="protocol-property-declaration"></a>
>
> *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
>
<a name="protocol_method_declaration"></a> <a name="protocol_method_declaration"></a>
### 协议方法声明 ### 协议方法声明
@ -906,7 +1099,9 @@ var 属性名: 类型 { get set }
> 协议方法声明语法 > 协议方法声明语法
> >
> <a name="protocol-method-declaration"></a> > <a name="protocol-method-declaration"></a>
>
> *协议方法声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> > *协议方法声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub>
>
<a name="protocol_initializer_declaration"></a> <a name="protocol_initializer_declaration"></a>
@ -925,8 +1120,11 @@ var 属性名: 类型 { get set }
> 协议构造器声明语法 > 协议构造器声明语法
> >
> <a name="protocol-initializer-declaration"></a> > <a name="protocol-initializer-declaration"></a>
>
> *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> > *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub>
>
> *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows** [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> > *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows** [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub>
>
<a name="protocol_subscript_declaration"></a> <a name="protocol_subscript_declaration"></a>
### 协议下标声明 ### 协议下标声明
@ -935,6 +1133,7 @@ var 属性名: 类型 { get set }
```swift ```swift
subscript (参数列表) -> 返回类型 { get set } subscript (参数列表) -> 返回类型 { get set }
>
``` ```
下标声明只为符合类型声明了 getter 和 setter 要求。如果下标声明包含 `get``set` 关键字,符合类型也必须实现 getter 和 setter 子句。如果下标声明只包含 `get` 关键字,符合类型必须实现 getter 子句,可以选择是否实现 setter 子句。 下标声明只为符合类型声明了 getter 和 setter 要求。如果下标声明包含 `get``set` 关键字,符合类型也必须实现 getter 和 setter 子句。如果下标声明只包含 `get` 关键字,符合类型必须实现 getter 子句,可以选择是否实现 setter 子句。
@ -943,8 +1142,11 @@ subscript (参数列表) -> 返回类型 { get set }
<a name="grammer_of_a_protocol_subscript_declaration"></a> <a name="grammer_of_a_protocol_subscript_declaration"></a>
> 协议下标声明语法 > 协议下标声明语法
>
> <a name="protocol-subscript-declaration"></a> > <a name="protocol-subscript-declaration"></a>
>
> *协议下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > *协议下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
>
<a name="protocol_associated_type_declaration"></a> <a name="protocol_associated_type_declaration"></a>
@ -976,7 +1178,9 @@ protocol SubProtocolB: SomeProtocol where SomeType: Equatable { }
> 协议关联类型声明语法 > 协议关联类型声明语法
> >
> <a name="protocol-associated-type-declaration"></a> > <a name="protocol-associated-type-declaration"></a>
>
> *协议关联类型声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **associatedtype** [*类型别名头*](#typealias-head) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*类型别名赋值*](#typealias-assignment)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> > *协议关联类型声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **associatedtype** [*类型别名头*](#typealias-head) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*类型别名赋值*](#typealias-assignment)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub>
>
<a name="initializer_declaration"></a> <a name="initializer_declaration"></a>
## 构造器声明 ## 构造器声明
@ -1018,6 +1222,7 @@ convenience init(参数列表) {
> 注意 > 注意
> >
> 如果使用 `required` 声明修饰符标记一个构造器,在子类中重写这种构造器时,无需使用 `override` 修饰符。 > 如果使用 `required` 声明修饰符标记一个构造器,在子类中重写这种构造器时,无需使用 `override` 修饰符。
>
就像函数和方法,构造器也可以抛出或者重抛错误,你可以在构造器参数列表的圆括号之后使用 `throws``rethrows` 关键字来表明相应的抛出行为。 就像函数和方法,构造器也可以抛出或者重抛错误,你可以在构造器参数列表的圆括号之后使用 `throws``rethrows` 关键字来表明相应的抛出行为。
@ -1068,14 +1273,23 @@ if let actualInstance = SomeStruct(input: "Hello") {
> 构造器声明语法 > 构造器声明语法
> >
> <a name="initializer-declaration"></a> > <a name="initializer-declaration"></a>
>
> *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*构造器主体*](#initializer-body) > *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*构造器主体*](#initializer-body)
>
> *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows**<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*构造器主体*](#initializer-body) > *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows**<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*构造器主体*](#initializer-body)
>
> <a name="initializer-head"></a> > <a name="initializer-head"></a>
>
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** > *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init**
>
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **?** > *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **?**
>
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **!** > *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **!**
>
> <a name="initializer-body"></a> > <a name="initializer-body"></a>
>
> *构造器主体* → [*代码块*](#code-block) > *构造器主体* → [*代码块*](#code-block)
>
<a name="deinitializer_declaration"></a> <a name="deinitializer_declaration"></a>
## 析构器声明 ## 析构器声明
@ -1100,7 +1314,9 @@ deinit {
> 析构器声明语法 > 析构器声明语法
> >
> <a name="deinitializer-declaration"></a> > <a name="deinitializer-declaration"></a>
>
> *析构器声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block) > *析构器声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block)
>
<a name="extension_declaration"></a> <a name="extension_declaration"></a>
## 扩展声明 ## 扩展声明
@ -1221,15 +1437,18 @@ doSomething(with: oneAndTwo)
```swift ```swift
protocol Serializable { protocol Serializable {
func serialize() -> Any func serialize() -> Any
>
} }
extension Array: Serializable where Element == Int { extension Array: Serializable where Element == Int {
func serialize() -> Any { func serialize() -> Any {
>
// implementation // implementation
} }
} }
extension Array: Serializable where Element == String { extension Array: Serializable where Element == String {
func serialize() -> Any { func serialize() -> Any {
>
// implementation // implementation
} }
} }
@ -1245,6 +1464,7 @@ extension String: SerializableInArray { }
extension Array: Serializable where Element: SerializableInArray { extension Array: Serializable where Element: SerializableInArray {
func serialize() -> Any { func serialize() -> Any {
>
// 具体实现 // 具体实现
} }
} }
@ -1292,13 +1512,20 @@ extension Array: Loggable where Element: MarkedLoggable { }
<a name="grammer_of_an_extension_declaration"></a> <a name="grammer_of_an_extension_declaration"></a>
> 扩展声明语法 > 扩展声明语法
>
> >
> <a name="extension-declaration"></a> > <a name="extension-declaration"></a>
>
> *扩展声明* → [特性](06_Attributes.html#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.html#type-identifier) [*类型-继承-子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*扩展主体*](#extension-body) > *扩展声明* → [特性](06_Attributes.html#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.html#type-identifier) [*类型-继承-子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*扩展主体*](#extension-body)
>
> <a name="extension-body"></a> > <a name="extension-body"></a>
>
> *扩展主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}** > *扩展主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
>
> *多条声明* → [单条声明](#subscript_declaration) [多条声明](#declarations) <sub>可选</sub> > *多条声明* → [单条声明](#subscript_declaration) [多条声明](#declarations) <sub>可选</sub>
>
> *单条声明* → [声明语句](#declarations) | [*编译控制流语句*](05_Statements.html#compiler-control-statement) > *单条声明* → [声明语句](#declarations) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
>
<a name="subscript_declaration"></a> <a name="subscript_declaration"></a>
@ -1308,6 +1535,7 @@ extension Array: Loggable where Element: MarkedLoggable { }
```swift ```swift
subscript (参数列表) -> 返回类型 { subscript (参数列表) -> 返回类型 {
>
get { get {
语句 语句
} }
@ -1335,15 +1563,24 @@ subscript (参数列表) -> 返回类型 {
<a name="grammer_of_a_subscript_declaration"></a> <a name="grammer_of_a_subscript_declaration"></a>
> 下标声明语法 > 下标声明语法
>
> >
> <a name="subscript-declaration"></a> > <a name="subscript-declaration"></a>
>
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*代码块*](#code-block) > *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*代码块*](#code-block)
>
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*getter-setter 代码块*](#getter-setter-block) > *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*getter-setter 代码块*](#getter-setter-block)
>
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.html#generic-where-clause)<sub>可选</sub> [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
>
> <a name="subscript-head"></a> > <a name="subscript-head"></a>
>
> *下标头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **subscript** [*泛型参数子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) > *下标头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **subscript** [*泛型参数子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause)
>
> <a name="subscript-result"></a> > <a name="subscript-result"></a>
>
> *下标结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type) > *下标结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type)
>
<a name="operator_declaration"></a> <a name="operator_declaration"></a>
## 运算符声明 ## 运算符声明
@ -1394,13 +1631,18 @@ postfix operator 运算符名称 {}
> >
<a name="prefix-operator-declaration"></a> <a name="prefix-operator-declaration"></a>
> *前缀运算符声明* → **prefix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** **}** > *前缀运算符声明* → **prefix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** **}**
>
> <a name="postfix-operator-declaration"></a> > <a name="postfix-operator-declaration"></a>
>
> *后缀运算符声明* → **postfix** **运算符** [*运算符*] (02_Lexical_Structure.html#operator) **{** **}** > *后缀运算符声明* → **postfix** **运算符** [*运算符*] (02_Lexical_Structure.html#operator) **{** **}**
>
> <a name="infix-operator-declaration"></a> > <a name="infix-operator-declaration"></a>
>
> *中缀运算符声明* → **infix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** [*中缀运算符属性*](#infix-operator-attributes)<sub>可选</sub> **}** > *中缀运算符声明* → **infix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** [*中缀运算符属性*](#infix-operator-attributes)<sub>可选</sub> **}**
> >
<a name="infix-operator-group"></a> <a name="infix-operator-group"></a>
> *中缀运算符组* → [*优先级组名称*](#precedence-group-name) > *中缀运算符组* → [*优先级组名称*](#precedence-group-name)
>
<a name="precedence_group_declaration_modifiers"></a> <a name="precedence_group_declaration_modifiers"></a>
## 优先级组声明 ## 优先级组声明
@ -1422,6 +1664,7 @@ precedencegroup 优先级组名称{
> 注意 > 注意
> >
> 使用较低和较高优先级组相互联系的优先级组必须保持单一层次关系,但它们不必是线性关系。这意味着优先级组也许会有未定义的相关优先级。这些优先级组的运算符在没有用圆括号分组的情况下是不能紧邻着使用的。 > 使用较低和较高优先级组相互联系的优先级组必须保持单一层次关系,但它们不必是线性关系。这意味着优先级组也许会有未定义的相关优先级。这些优先级组的运算符在没有用圆括号分组的情况下是不能紧邻着使用的。
>
Swift 定义了大量的优先级组来与标准库的运算符配合使用,例如相加(`+`)和相减(`-`)属于 `AdditionPrecedence` 组,相乘(`*`)和相除(`/`)属于 `MultiplicationPrecedence` 组,详细关于 Swift 标准库中一系列运算符和优先级组内容,参阅[Swift 标准库操作符参考](https://developer.apple.com/reference/swift/1851035-swift_standard_library_operators)。 Swift 定义了大量的优先级组来与标准库的运算符配合使用,例如相加(`+`)和相减(`-`)属于 `AdditionPrecedence` 组,相乘(`*`)和相除(`/`)属于 `MultiplicationPrecedence` 组,详细关于 Swift 标准库中一系列运算符和优先级组内容,参阅[Swift 标准库操作符参考](https://developer.apple.com/reference/swift/1851035-swift_standard_library_operators)。
@ -1434,26 +1677,40 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
> >
<a name="precedence-group-declaration"></a> <a name="precedence-group-declaration"></a>
> *优先级组声明* → **precedence**[*优先级组名称*](#precedence-group-name){[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> } > *优先级组声明* → **precedence**[*优先级组名称*](#precedence-group-name){[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> }
>
<a name="precedence-group-attributes"></a> <a name="precedence-group-attributes"></a>
> *优先级组属性* → [*优先级组属性*](#precedence-group-attribute)[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> **{** **}** > *优先级组属性* → [*优先级组属性*](#precedence-group-attribute)[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> **{** **}**
>
<a name="precedence-group-attribute"></a> <a name="precedence-group-attribute"></a>
> *优先级组属性* → [*优先级组关系*](#precedence-group-relation) > *优先级组属性* → [*优先级组关系*](#precedence-group-relation)
>
> *优先级组属性* → [*优先级组赋值性*](#precedence-group-assignment) > *优先级组属性* → [*优先级组赋值性*](#precedence-group-assignment)
>
> *优先级组属性* → [*优先级组相关性*](#precedence-group-associativity) > *优先级组属性* → [*优先级组相关性*](#precedence-group-associativity)
>
> <a name="precedence-group-relation"></a> > <a name="precedence-group-relation"></a>
>
> *优先级组关系* → **higherThan:**[*多优先级组名称*](#precedence-group-names) > *优先级组关系* → **higherThan:**[*多优先级组名称*](#precedence-group-names)
>
> *优先级组关系* → **lowerThan:**[*多优先级组名称*](#precedence-group-names) > *优先级组关系* → **lowerThan:**[*多优先级组名称*](#precedence-group-names)
>
> <a name="precedence-group-assignment"></a> > <a name="precedence-group-assignment"></a>
>
> *优先级组赋值* → **assignment:**[*布尔字面值*](02_Lexical_Structure.html#boolean-literal) > *优先级组赋值* → **assignment:**[*布尔字面值*](02_Lexical_Structure.html#boolean-literal)
>
<a name="precedence-group-associativity"></a> <a name="precedence-group-associativity"></a>
> *优先级组结合性* → **associativity:left** > *优先级组结合性* → **associativity:left**
>
> *优先级组结合性* → **associativity:right** > *优先级组结合性* → **associativity:right**
>
> *优先级组结合性* → **associativity:none** > *优先级组结合性* → **associativity:none**
> >
<a name="precedence-group-names"></a> <a name="precedence-group-names"></a>
> *多优先级组名称* → [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) > *多优先级组名称* → [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name)
>
<a name="precedence-group-name"></a> <a name="precedence-group-name"></a>
> *优先级组名称* →[*标识符*](02_Lexical_Structure.html#identifier) > *优先级组名称* →[*标识符*](02_Lexical_Structure.html#identifier)
>
<a name="Declaration_Modifiers"></a> <a name="Declaration_Modifiers"></a>
## 声明修饰符 ## 声明修饰符
@ -1524,11 +1781,17 @@ Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`
> >
<a name="declaration-modifier"></a> <a name="declaration-modifier"></a>
> *声明修饰符* → **class** | **convenience**| **dynamic** | **final** | **infix** | **lazy** | **mutating** | **nonmutating** | **optional** | **override** | **postfix** | **prefix** | **required** | **static** | **unowned** | **unowned ( safe )** | **unowned ( unsafe )** | **weak** > *声明修饰符* → **class** | **convenience**| **dynamic** | **final** | **infix** | **lazy** | **mutating** | **nonmutating** | **optional** | **override** | **postfix** | **prefix** | **required** | **static** | **unowned** | **unowned ( safe )** | **unowned ( unsafe )** | **weak**
>
> 声明修饰符 → [*访问级别修饰符*](#access-level-modifier) > 声明修饰符 → [*访问级别修饰符*](#access-level-modifier)
>
> <a name="declaration-modifiers"></a> > <a name="declaration-modifiers"></a>
>
> *声明修饰符列表* → [*声明修饰符*](#declaration-modifier) [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> > *声明修饰符列表* → [*声明修饰符*](#declaration-modifier) [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub>
> >
<a name="access-level-modifier"></a> <a name="access-level-modifier"></a>
> 访问级别修饰符 → **internal** | **internal ( set )** > 访问级别修饰符 → **internal** | **internal ( set )**
>
> 访问级别修饰符 → **private** | **private ( set )** > 访问级别修饰符 → **private** | **private ( set )**
>
> 访问级别修饰符 → **public** | **public ( set )** > 访问级别修饰符 → **public** | **public ( set )**
>

View File

@ -155,6 +155,7 @@ dial.dynamicallyCall(withArguments: [4, 1, 1])
@dynamicCallable @dynamicCallable
struct Repeater { struct Repeater {
func dynamicallyCall(withKeywordArguments pairs: KeyValuePairs<String, Int>) -> String { func dynamicallyCall(withKeywordArguments pairs: KeyValuePairs<String, Int>) -> String {
>
return pairs return pairs
.map { label, count in .map { label, count in
repeatElement(label, count: count).joined(separator: " ") repeatElement(label, count: count).joined(separator: " ")
@ -195,6 +196,7 @@ struct DynamicStruct {
let dictionary = ["someDynamicMember": 325, let dictionary = ["someDynamicMember": 325,
"someOtherMember": 787] "someOtherMember": 787]
subscript(dynamicMember member: String) -> Int { subscript(dynamicMember member: String) -> Int {
>
return dictionary[member] ?? 1054 return dictionary[member] ?? 1054
} }
} }
@ -379,21 +381,38 @@ convention 特性总是与下面的参数之一一起出现。
次特性用于 switch case表示在编译时该地方不会匹配枚举的任何情况。有关如何使用 `unknown` 特性的示例,可参阅 [Switching over Future Enumeration Cases](https://docs.swift.org/swift-book/ReferenceManual/Statements.html#ID602)。 次特性用于 switch case表示在编译时该地方不会匹配枚举的任何情况。有关如何使用 `unknown` 特性的示例,可参阅 [Switching over Future Enumeration Cases](https://docs.swift.org/swift-book/ReferenceManual/Statements.html#ID602)。
> 特性语法 > 特性语法
>
> >
> <a name="attribute"></a> > <a name="attribute"></a>
>
> *特性*→ [特性名](#attribute_name) [特性参数子句](#atribute_argument_clause)<sub>可选</sub> > *特性*→ [特性名](#attribute_name) [特性参数子句](#atribute_argument_clause)<sub>可选</sub>
>
> <a name="attribute_name"></a> > <a name="attribute_name"></a>
>
> *特性名* → [标识符](02_Lexical_Structure.html#identifier) > *特性名* → [标识符](02_Lexical_Structure.html#identifier)
>
> <a name="atribute_argument_clause"></a> > <a name="atribute_argument_clause"></a>
>
> *特性参数子句* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)** > *特性参数子句* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)**
>
> <a name="attributes"></a> > <a name="attributes"></a>
>
> *特性列表* → [特性](#attribute) [特性列表](#attributes)<sub>可选</sub> > *特性列表* → [特性](#attribute) [特性列表](#attributes)<sub>可选</sub>
>
> >
> <a name="balanced_tokens"></a> > <a name="balanced_tokens"></a>
>
> *均衡令牌列表* → [均衡令牌](#balanced_token) [均衡令牌列表](#balanced_tokens)<sub>可选</sub> > *均衡令牌列表* → [均衡令牌](#balanced_token) [均衡令牌列表](#balanced_tokens)<sub>可选</sub>
>
> <a name="balanced_token"></a> > <a name="balanced_token"></a>
>
> *均衡令牌* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)** > *均衡令牌* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)**
>
> *均衡令牌* → **\[** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **\]** > *均衡令牌* → **\[** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **\]**
>
> *均衡令牌* → **{** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **}** > *均衡令牌* → **{** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **}**
>
> *均衡令牌* → 任意标识符,关键字,字面量或运算符 > *均衡令牌* → 任意标识符,关键字,字面量或运算符
>
> *均衡令牌* → 任意标点除了 **(****)****[****]****{**,或 **}** > *均衡令牌* → 任意标点除了 **(****)****[****]****{**,或 **}**
>

View File

@ -12,13 +12,21 @@ Swift 中的模式分为两类:一种能成功匹配任何类型的值,另
> >
<a name="pattern"></a> <a name="pattern"></a>
> *模式* → [*通配符模式*](#wildcard_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub> > *模式* → [*通配符模式*](#wildcard_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
>
> *模式* → [*标识符模式*](#identifier_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub> > *模式* → [*标识符模式*](#identifier_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
>
> *模式* → [*值绑定模式*](#value-binding-pattern) > *模式* → [*值绑定模式*](#value-binding-pattern)
>
> *模式* → [*元组模式*](#tuple-pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub> > *模式* → [*元组模式*](#tuple-pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
>
> *模式* → [*枚举用例模式*](#enum-case-pattern) > *模式* → [*枚举用例模式*](#enum-case-pattern)
>
> *模式* → [*可选模式*](#optional-pattern) > *模式* → [*可选模式*](#optional-pattern)
>
> *模式* → [*类型转换模式*](#type-casting-pattern) > *模式* → [*类型转换模式*](#type-casting-pattern)
>
> *模式* → [*表达式模式*](#expression-pattern) > *模式* → [*表达式模式*](#expression-pattern)
>
<a name="wildcard_pattern"></a> <a name="wildcard_pattern"></a>
## 通配符模式Wildcard Pattern ## 通配符模式Wildcard Pattern
@ -35,6 +43,7 @@ for _ in 1...3 {
> >
<a name="wildcard-pattern"></a> <a name="wildcard-pattern"></a>
> *通配符模式* → **_** > *通配符模式* → **_**
>
<a name="identifier_pattern"></a> <a name="identifier_pattern"></a>
## 标识符模式Identifier Pattern ## 标识符模式Identifier Pattern
@ -53,6 +62,7 @@ let someValue = 42
> >
<a name="identifier-pattern"></a> <a name="identifier-pattern"></a>
> *标识符模式* → [*标识符*](02_Lexical_Structure.html#identifier) > *标识符模式* → [*标识符*](02_Lexical_Structure.html#identifier)
>
<a name="value-binding_pattern"></a> <a name="value-binding_pattern"></a>
## 值绑定模式Value-Binding Pattern ## 值绑定模式Value-Binding Pattern
@ -77,6 +87,7 @@ case let (x, y):
> >
<a name="value-binding-pattern"></a> <a name="value-binding-pattern"></a>
> *值绑定模式* → **var** [*模式*](#pattern) | **let** [*模式*](#pattern) > *值绑定模式* → **var** [*模式*](#pattern) | **let** [*模式*](#pattern)
>
<a name="tuple_pattern"></a> <a name="tuple_pattern"></a>
## 元组模式 ## 元组模式
@ -107,10 +118,13 @@ let (a): Int = 2 // a: Int = 2
> >
<a name="tuple-pattern"></a> <a name="tuple-pattern"></a>
> *元组模式* → **(** [*元组模式元素列表*](#tuple-pattern-element-list)<sub>可选</sub> **)** > *元组模式* → **(** [*元组模式元素列表*](#tuple-pattern-element-list)<sub>可选</sub> **)**
>
<a name="tuple-pattern-element-list"></a> <a name="tuple-pattern-element-list"></a>
> *元组模式元素列表* → [*元组模式元素*](#tuple-pattern-element) | [*元组模式元素*](#tuple-pattern-element) **,** [*元组模式元素列表*](#tuple-pattern-element-list) > *元组模式元素列表* → [*元组模式元素*](#tuple-pattern-element) | [*元组模式元素*](#tuple-pattern-element) **,** [*元组模式元素列表*](#tuple-pattern-element-list)
>
<a name="tuple-pattern-element"></a> <a name="tuple-pattern-element"></a>
> *元组模式元素* → [*模式*](#pattern) > *元组模式元素* → [*模式*](#pattern)
>
<a name="enumeration_case_pattern"></a> <a name="enumeration_case_pattern"></a>
## 枚举用例模式Enumeration Case Pattern ## 枚举用例模式Enumeration Case Pattern
@ -123,6 +137,7 @@ let (a): Int = 2 // a: Int = 2
> >
<a name="enum-case-pattern"></a> <a name="enum-case-pattern"></a>
> *枚举用例模式* → [*类型标识*](03_Types.html#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](05_Declarations.html#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub> > *枚举用例模式* → [*类型标识*](03_Types.html#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](05_Declarations.html#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
>
<a name="optional_pattern"></a> <a name="optional_pattern"></a>
## 可选模式Optional Pattern ## 可选模式Optional Pattern
@ -161,6 +176,7 @@ for case let number? in arrayOfOptinalInts {
> >
<a name="optional-pattern"></a> <a name="optional-pattern"></a>
> *可选模式* → [*标识符模式*](03_Types.html#type-identifier) **?** > *可选模式* → [*标识符模式*](03_Types.html#type-identifier) **?**
>
<a name="type-casting_patterns"></a> <a name="type-casting_patterns"></a>
## 类型转换模式Type-Casting Patterns ## 类型转换模式Type-Casting Patterns
@ -168,7 +184,9 @@ for case let number? in arrayOfOptinalInts {
有两种类型转换模式,`is` 模式和 `as` 模式。`is` 模式只出现在 `switch` 语句中的 `case` 标签中。`is` 模式和 `as` 模式形式如下: 有两种类型转换模式,`is` 模式和 `as` 模式。`is` 模式只出现在 `switch` 语句中的 `case` 标签中。`is` 模式和 `as` 模式形式如下:
> is `类型` > is `类型`
>
> `模式` as `类型` > `模式` as `类型`
>
`is` 模式仅当一个值的类型在运行时和 `is` 模式右边的指定类型一致,或者是其子类的情况下,才会匹配这个值。`is` 模式和 `is` 运算符有相似表现,它们都进行类型转换,但是 `is` 模式没有返回类型。 `is` 模式仅当一个值的类型在运行时和 `is` 模式右边的指定类型一致,或者是其子类的情况下,才会匹配这个值。`is` 模式和 `is` 运算符有相似表现,它们都进行类型转换,但是 `is` 模式没有返回类型。
@ -180,10 +198,13 @@ for case let number? in arrayOfOptinalInts {
> >
<a name="type-casting-pattern"></a> <a name="type-casting-pattern"></a>
> *类型转换模式* → [*is 模式*](#is-pattern) | [*as 模式*](#as-pattern) > *类型转换模式* → [*is 模式*](#is-pattern) | [*as 模式*](#as-pattern)
>
<a name="is-pattern"></a> <a name="is-pattern"></a>
> *is 模式* → **is** [*类型*](03_Types.html#type) > *is 模式* → **is** [*类型*](03_Types.html#type)
>
<a name="as-pattern"></a> <a name="as-pattern"></a>
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.html#type) > *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.html#type)
>
<a name="expression_pattern"></a> <a name="expression_pattern"></a>
## 表达式模式Expression Pattern ## 表达式模式Expression Pattern
@ -210,6 +231,7 @@ default:
```swift ```swift
// 重载 ~= 运算符对字符串和整数进行比较 // 重载 ~= 运算符对字符串和整数进行比较
func ~=(pattern: String, value: Int) -> Bool { func ~=(pattern: String, value: Int) -> Bool {
>
return pattern == "\(value)" return pattern == "\(value)"
} }
@ -226,3 +248,4 @@ default:
> >
<a name="expression-pattern"></a> <a name="expression-pattern"></a>
> *表达式模式* → [*表达式*](04_Expressions.html#expression) > *表达式模式* → [*表达式*](04_Expressions.html#expression)
>

View File

@ -10,10 +10,12 @@
*泛型形参子句*指定泛型类型或函数的类型形参,以及这些参数相关的约束和要求。泛型形参子句用尖括号(`<>`)包住,形式如下: *泛型形参子句*指定泛型类型或函数的类型形参,以及这些参数相关的约束和要求。泛型形参子句用尖括号(`<>`)包住,形式如下:
> <`泛型形参列表`> > <`泛型形参列表`>
>
泛型形参列表中泛型形参用逗号分开,其中每一个采用以下形式: 泛型形参列表中泛型形参用逗号分开,其中每一个采用以下形式:
> `类型形参` : `约束` > `类型形参` : `约束`
>
泛型形参由两部分组成:类型形参及其后的可选约束。类型形参只是占位符类型(如 `T``U``V``Key``Value` 等)的名字而已。你可以在泛型类型、函数的其余部分或者构造器声明,包括函数或构造器的签名中使用它(以及它的关联类型)。 泛型形参由两部分组成:类型形参及其后的可选约束。类型形参只是占位符类型(如 `T``U``V``Key``Value` 等)的名字而已。你可以在泛型类型、函数的其余部分或者构造器声明,包括函数或构造器的签名中使用它(以及它的关联类型)。
@ -21,6 +23,7 @@
```swift ```swift
func simpleMax<T: Comparable>(_ x: T, _ y: T) -> T { func simpleMax<T: Comparable>(_ x: T, _ y: T) -> T {
>
if x < y { if x < y {
return y return y
} }
@ -41,10 +44,13 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
要想对类型形参及其关联类型指定额外要求,可以在函数体或者类型的大括号之前添加 `where` 子句。`where` 子句由关键字 `where` 及其后的用逗号分隔的一个或多个要求组成。 要想对类型形参及其关联类型指定额外要求,可以在函数体或者类型的大括号之前添加 `where` 子句。`where` 子句由关键字 `where` 及其后的用逗号分隔的一个或多个要求组成。
> `where` : `类型要求` > `where` : `类型要求`
>
`where` 子句中的要求用于指明该类型形参继承自某个类或符合某个协议或协议组合。尽管 `where` 子句提供了语法糖使其有助于表达类型形参上的简单约束(如 `<T: Comparable>` 等同于 `<T> where T: Comparable`,等等),但是依然可以用来对类型形参及其关联类型提供更复杂的约束,例如你可以强制形参的关联类型遵守协议,如,`<S: Sequence> where S.Iterator.Element: Equatable` 表示泛型类型 `S` 遵守 `Sequence` 协议并且关联类型 `S.Iterator.Element` 遵守 `Equatable` 协议,这个约束确保队列的每一个元素都是符合 `Equatable` 协议的。 `where` 子句中的要求用于指明该类型形参继承自某个类或符合某个协议或协议组合。尽管 `where` 子句提供了语法糖使其有助于表达类型形参上的简单约束(如 `<T: Comparable>` 等同于 `<T> where T: Comparable`,等等),但是依然可以用来对类型形参及其关联类型提供更复杂的约束,例如你可以强制形参的关联类型遵守协议,如,`<S: Sequence> where S.Iterator.Element: Equatable` 表示泛型类型 `S` 遵守 `Sequence` 协议并且关联类型 `S.Iterator.Element` 遵守 `Equatable` 协议,这个约束确保队列的每一个元素都是符合 `Equatable` 协议的。
>
也可以用操作符 `==` 来指定两个类型必须相同。例如,泛型形参子句 `<S1: Sequence, S2: Sequence> where S1.Iterator.Element == S2.Iterator.Element` 表示 `S1``S2` 必须都符合 `SequenceType` 协议,而且两个序列中的元素类型必须相同。 也可以用操作符 `==` 来指定两个类型必须相同。例如,泛型形参子句 `<S1: Sequence, S2: Sequence> where S1.Iterator.Element == S2.Iterator.Element` 表示 `S1``S2` 必须都符合 `SequenceType` 协议,而且两个序列中的元素类型必须相同。
>
当然,替代类型形参的类型实参必须满足所有的约束和要求。 当然,替代类型形参的类型实参必须满足所有的约束和要求。
@ -56,23 +62,36 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
> >
<a name="generic-parameter-clause"></a> <a name="generic-parameter-clause"></a>
> *泛型形参子句* → **<** [*泛型形参列表*](#generic-parameter-list) [*约束子句*](#requirement-clause)<sub>可选</sub> **>** > *泛型形参子句* → **<** [*泛型形参列表*](#generic-parameter-list) [*约束子句*](#requirement-clause)<sub>可选</sub> **>**
>
<a name="generic-parameter-list"></a> <a name="generic-parameter-list"></a>
> *泛型形参列表* → [*泛形形参*](#generic-parameter) | [*泛形形参*](#generic-parameter) **,** [*泛型形参列表*](#generic-parameter-list) > *泛型形参列表* → [*泛形形参*](#generic-parameter) | [*泛形形参*](#generic-parameter) **,** [*泛型形参列表*](#generic-parameter-list)
>
<a name="generic-parameter"></a> <a name="generic-parameter"></a>
> *泛形形参* → [*类型名称*](03_Types.html#type-name) > *泛形形参* → [*类型名称*](03_Types.html#type-name)
>
> *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*类型标识符*](03_Types.html#type-identifier) > *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*类型标识符*](03_Types.html#type-identifier)
>
> *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*协议合成类型*](03_Types.html#protocol-composition-type) > *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*协议合成类型*](03_Types.html#protocol-composition-type)
>
> <a name="requirement-clause"></a> > <a name="requirement-clause"></a>
>
> *约束子句* → **where** [*约束列表*](#requirement-list) > *约束子句* → **where** [*约束列表*](#requirement-list)
>
<a name="requirement-list"></a> <a name="requirement-list"></a>
> *约束列表* → [*约束*](#requirement) | [*约束*](#requirement) **,** [*约束列表*](#requirement-list) > *约束列表* → [*约束*](#requirement) | [*约束*](#requirement) **,** [*约束列表*](#requirement-list)
>
<a name="requirement"></a> <a name="requirement"></a>
> *约束* → [*一致性约束*](#conformance-requirement) | [*同类型约束*](#same-type-requirement) > *约束* → [*一致性约束*](#conformance-requirement) | [*同类型约束*](#same-type-requirement)
>
> <a name="conformance-requirement"></a> > <a name="conformance-requirement"></a>
>
> *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*类型标识符*](03_Types.html#type-identifier) > *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*类型标识符*](03_Types.html#type-identifier)
>
> *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*协议合成类型*](03_Types.html#protocol-composition-type) > *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*协议合成类型*](03_Types.html#protocol-composition-type)
>
<a name="same-type-requirement"></a> <a name="same-type-requirement"></a>
> *同类型约束* → [*类型标识符*](03_Types.html#type-identifier) **==** [*类型*](03_Types.html#type) > *同类型约束* → [*类型标识符*](03_Types.html#type-identifier) **==** [*类型*](03_Types.html#type)
>
<a name="generic_argument"></a> <a name="generic_argument"></a>
## 泛型实参子句 ## 泛型实参子句
@ -80,6 +99,7 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
*泛型实参子句*指定泛型类型的类型实参。泛型实参子句用尖括号(`<>`)包住,形式如下: *泛型实参子句*指定泛型类型的类型实参。泛型实参子句用尖括号(`<>`)包住,形式如下:
> <`泛型实参列表`> > <`泛型实参列表`>
>
泛型实参列表中类型实参用逗号分开。类型实参是实际具体类型的名字用来替代泛型类型的泛型形参子句中的相应的类型形参。从而得到泛型类型的一个特化版本。例如Swift 标准库中的泛型字典类型的的简化定义如下: 泛型实参列表中类型实参用逗号分开。类型实参是实际具体类型的名字用来替代泛型类型的泛型形参子句中的相应的类型形参。从而得到泛型类型的一个特化版本。例如Swift 标准库中的泛型字典类型的的简化定义如下:
@ -95,6 +115,7 @@ struct Dictionary<Key: Hashable, Value>: CollectionType, DictionaryLiteralConver
```swift ```swift
let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>
``` ```
如 [泛型形参子句](#generic_parameter) 所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。 如 [泛型形参子句](#generic_parameter) 所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。
@ -103,7 +124,10 @@ let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
> >
<a name="generic-argument-clause"></a> <a name="generic-argument-clause"></a>
> *泛型实参子句* → **<** [*泛型实参列表*](#generic-argument-list) **>** > *泛型实参子句* → **<** [*泛型实参列表*](#generic-argument-list) **>**
>
<a name="generic-argument-list"></a> <a name="generic-argument-list"></a>
> *泛型实参列表* → [*泛型实参*](#generic-argument) | [*泛型实参*](#generic-argument) **,** [*泛型实参列表*](#generic-argument-list) > *泛型实参列表* → [*泛型实参*](#generic-argument) | [*泛型实参*](#generic-argument) **,** [*泛型实参列表*](#generic-argument-list)
>
<a name="generic-argument"></a> <a name="generic-argument"></a>
> *泛型实参* → [*类型*](03_Types.html#type) > *泛型实参* → [*类型*](03_Types.html#type)
>

File diff suppressed because it is too large Load Diff