diff --git a/source/chapter3/02_Lexical_Structure.md b/source/chapter3/02_Lexical_Structure.md index c04c0ce7..afc994f3 100755 --- a/source/chapter3/02_Lexical_Structure.md +++ b/source/chapter3/02_Lexical_Structure.md @@ -13,7 +13,7 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言 > 空白语法 > -whitespace {#whitespace} +###### {#whitespace} > *空白* → [*空白项*](#whitespace-item) [*空白*](#whitespace)可选 > > *空白项* → [*断行符*](#line-break) @@ -25,7 +25,7 @@ whitespace {#whitespace} > *空白项* → U+0000,U+0009,U+000B,U+000C 或者 U+0020 > -line-break {#line-break} +###### {#line-break} > *断行符* → U+000A > > *断行符* → U+000D @@ -33,7 +33,7 @@ line-break {#line-break} > *断行符* → U+000D 接着是 U+000A > -comment {#comment} +###### {#comment} > *注释* → // [*注释内容 断行*](#comment-text line-break) > > *多行注释* → `/*` [*多行注释内容*](#multiline-commnet-text) `*/` @@ -64,7 +64,7 @@ comment {#comment} > 标识符语法 > -identifier {#identifier} +###### {#identifier} > *标识符* → [*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)可选 > > *标识符* → \`[*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)可选\` @@ -72,11 +72,11 @@ identifier {#identifier} > *标识符* → [*隐式参数名*](#implicit-parameter-name) > -identifier-list {#identifier-list} +###### {#identifier-list} > *标识符列表* → [*标识符*](#identifier) | [*标识符*](#identifier) **,** [*标识符列表*](#identifier-list) > -identifier-head {#identifier-head} +###### {#identifier-head} > *头部标识符* → 大写或小写字母 A - Z > > *头部标识符* → _ @@ -110,19 +110,19 @@ identifier-head {#identifier-head} > *头部标识符* → U+D0000–U+DFFFD 或者 U+E0000–U+EFFFD > -identifier-character {#identifier-character} +###### {#identifier-character} > *标识符字符* → 数值 0 - 9 > > *标识符字符* → U+0300–U+036F,U+1DC0–U+1DFF,U+20D0–U+20FF,或者 U+FE20–U+FE2F > > *标识符字符* → [*头部标识符*](#identifier-head) > -> identifier-characters {#identifier-characters} +> ###### identifier-characters {#identifier-characters} > > *标识符字符组* → [*标识符字符*](#identifier-character) [*标识符字符组*](#identifier-characters)可选 > -implicit-parameter-name {#implicit-parameter-name} +###### {#implicit-parameter-name} > *隐式参数名* → **$** [*十进制数字列表*](#decimal-digits) > @@ -161,14 +161,14 @@ true // 布尔值字面量 > *字面量* → [*数值字面量*](#numeric-literal) | [*字符串字面量*](#string-literal) | [*布尔值字面量*](#boolean-literal) | [*nil 字面量*](#nil-literal) > -numeric-literal {#numeric-literal} +###### {#numeric-literal} > *数值字面量* → **-**可选 [*整数字面量*](#integer-literal) | **-**可选 [*浮点数字面量*](#floating-point-literal) > -> boolean-literal {#boolean-literal} +> ###### boolean-literal {#boolean-literal} > > *布尔值字面量* → **true** | **false** > -> nil-literal {#nil-literal} +> ###### nil-literal {#nil-literal} > > *nil 字面量* → **nil** > @@ -187,7 +187,7 @@ numeric-literal {#numeric-literal} > 整数字面量语法 > -integer-literal {#integer-literal} +###### {#integer-literal} > *整数字面量* → [*二进制字面量*](#binary-literal) > > *整数字面量* → [*八进制字面量*](#octal-literal) @@ -197,70 +197,70 @@ integer-literal {#integer-literal} > *整数字面量* → [*十六进制字面量*](#hexadecimal-literal) > -binary-literal {#binary-literal} +###### {#binary-literal} > *二进制字面量* → **0b** [*二进制数字*](#binary-digit) [*二进制字面量字符组*](#binary-literal-characters)可选 > -> binary-digit {#binary-digit} +> ###### binary-digit {#binary-digit} > > *二进制数字* → 数值 0 到 1 > -> binary-literal-character {#binary-literal-character} +> ###### binary-literal-character {#binary-literal-character} > > *二进制字面量字符* → [*二进制数字*](#binary-digit) | _ > -> binary-literal-characters {#binary-literal-characters} +> ###### binary-literal-characters {#binary-literal-characters} > > *二进制字面量字符组* → [*二进制字面量字符*](#binary-literal-character) [*二进制字面量字符组*](#binary-literal-characters)可选 > -octal-literal {#octal-literal} +###### {#octal-literal} > *八进制字面量* → **0o** [*八进字数字*](#octal-digit) [*八进制字符组*](#octal-literal-characters)可选 > -> octal-digit {#octal-digit} +> ###### octal-digit {#octal-digit} > > *八进字数字* → 数值 0 到 7 > -> octal-literal-character {#octal-literal-character} +> ###### octal-literal-character {#octal-literal-character} > > *八进制字符* → [*八进字数字*](#octal-digit) | _ > -> octal-literal-characters {#octal-literal-characters} +> ###### octal-literal-characters {#octal-literal-characters} > > *八进制字符组* → [*八进制字符*](#octal-literal-character) [*八进制字符组*](#octal-literal-characters)可选 > -decimal-literal {#decimal-literal} +###### {#decimal-literal} > *十进制字面量* → [*十进制数字*](#decimal-digit) [*十进制字符组*](#decimal-literal-characters)可选 > -> decimal-digit {#decimal-digit} +> ###### decimal-digit {#decimal-digit} > > *十进制数字* → 数值 0 到 9 > -> decimal-digits {#decimal-digits} +> ###### decimal-digits {#decimal-digits} > > *十进制数字组* → [*十进制数字*](#decimal-digit) [*十进制数字组*](#decimal-digits)可选 > -> decimal-literal-character {#decimal-literal-character} +> ###### decimal-literal-character {#decimal-literal-character} > > *十进制字符* → [*十进制数字*](#decimal-digit) | _ > -> decimal-literal-characters {#decimal-literal-characters} +> ###### decimal-literal-characters {#decimal-literal-characters} > > *十进制字符组* → [*十进制字符*](#decimal-literal-character) [*十进制字符组*](#decimal-literal-characters)可选 > -hexadecimal-literal {#hexadecimal-literal} +###### {#hexadecimal-literal} > *十六进制字面量* → **0x** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)可选 > -> hexadecimal-digit {#hexadecimal-digit} +> ###### hexadecimal-digit {#hexadecimal-digit} > > *十六进制数字* → 数值 0 到 9, 字母 a 到 f, 或 A 到 F > -> hexadecimal-literal-character {#hexadecimal-literal-character} +> ###### hexadecimal-literal-character {#hexadecimal-literal-character} > > *十六进制字符* → [*十六进制数字*](#hexadecimal-digit) | _ > -> hexadecimal-literal-characters {#hexadecimal-literal-characters} +> ###### hexadecimal-literal-characters {#hexadecimal-literal-characters} > > *十六进制字面量字符组* → [*十六进制字符*](#hexadecimal-literal-character) [*十六进制字面量字符组*](#hexadecimal-literal-characters)可选 > @@ -283,36 +283,36 @@ hexadecimal-literal {#hexadecimal-literal} > 浮点数字面量语法 > -floating-point-literal {#floating-point-literal} +###### {#floating-point-literal} > *浮点数字面量* → [*十进制字面量*](#decimal-literal) [*十进制分数*](#decimal-fraction)可选 [*十进制指数*](#decimal-exponent)可选 > > *浮点数字面量* → [*十六进制字面量*](#hexadecimal-literal) [*十六进制分数*](#hexadecimal-fraction)可选 [*十六进制指数*](#hexadecimal-exponent) > -decimal-fraction {#decimal-fraction} +###### {#decimal-fraction} > *十进制分数* → **.** [*十进制字面量*](#decimal-literal) > -> decimal-exponent {#decimal-exponent} +> ###### decimal-exponent {#decimal-exponent} > > *十进制指数* → [*十进制指数 e*](#floating-point-e) [*正负号*](#sign)可选 [*十进制字面量*](#decimal-literal) > -hexadecimal-fraction {#hexadecimal-fraction} +###### {#hexadecimal-fraction} > *十六进制分数* → **.** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)可选 > -> hexadecimal-exponent {#hexadecimal-exponent} +> ###### hexadecimal-exponent {#hexadecimal-exponent} > > *十六进制指数* → [*十六进制指数 p*](#floating-point-p) [*正负号*](#sign)可选 [*十进制字面量*](#decimal-literal) > -floating-point-e {#floating-point-e} +###### {#floating-point-e} > *十进制指数 e* → **e** | **E** > -> floating-point-p {#floating-point-p} +> ###### floating-point-p {#floating-point-p} > > *十六进制指数 p* → **p** | **P** > -> sign {#sign} +> ###### sign {#sign} > > *正负号* → **+** | **-** > @@ -378,42 +378,42 @@ let textB = "Hello world" > 字符串字面量语法 > -string-literal {#string-literal} +###### {#string-literal} > *字符串字面量* → [*静态字符串字面量*](#static-string-literal) | [*插值字符串字面量*](#interpolated-string-literal) > -static-string-literal {#static-string-literal} +###### {#static-string-literal} > *静态字符串字面量* → **"**[*引用文本*](#quoted-text)可选**"** > -> quoted-text {#quoted-text} +> ###### quoted-text {#quoted-text} > > *引用文本* → [*引用文本项*](#quoted-text-item) [*引用文本*](#quoted-text)可选 > -> quoted-text-item {#quoted-text-item} +> ###### quoted-text-item {#quoted-text-item} > > *引用文本项* → [*转义字符*](#escaped-character) > > *引用文本项* → 除了 **"**、**\\**、U+000A、U+000D 以外的所有 Unicode 字符 > -interpolated-string-literal {#interpolated-string-literal} +###### {#interpolated-string-literal} > *插值字符串字面量* → **"**[*插值文本*](#interpolated-text)可选**"** > -> interpolated-text {#interpolated-text} +> ###### interpolated-text {#interpolated-text} > > *插值文本* → [*插值文本项*](#interpolated-text-item) [*插值文本*](#interpolated-text)可选 > -> interpolated-text-item {#interpolated-text-item} +> ###### interpolated-text-item {#interpolated-text-item} > > *插值文本项* → **\\****(**[*表达式*](./04_Expressions.md)**)** | [*引用文本项*](#quoted-text-item) > -escaped-character {#escaped-character} +###### {#escaped-character} > *转义字符* → **\\****0** | **\\****\\** | **\t** | **\n** | **\r** | **\\"** | **\\'** > > *转义字符* → **\u {** [*unicode 标量数字*](#unicode-scalar-digits) **}** > -> unicode-scalar-digits {#unicode-scalar-digits} +> ###### unicode-scalar-digits {#unicode-scalar-digits} > > *unicode 标量数字* → 一到八位的十六进制数字 > @@ -450,13 +450,13 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基 > 运算符语法 > -operator {#operator} +###### {#operator} > *运算符* → [*头部运算符*](#operator-head) [*运算符字符组*](#operator-characters)可选 > > *运算符* → [*头部点运算符*](#dot-operator-head) [*点运算符字符组*](#dot-operator-characters)可选 > -operator-head {#operator-head} +###### {#operator-head} > *头部运算符* → **/** | **=** | **-** | **+** | **!** | __*__ | **%** | **<** | **>** | **&** | **|** | **^** | **~** | **?** > > *头部运算符* → U+00A1–U+00A7 @@ -488,7 +488,7 @@ operator-head {#operator-head} > *头部运算符* → U+3008–U+3030 > -operator-character {#operator-character} +###### {#operator-character} > *运算符字符* → [*头部运算符*](#operator-head) > > *运算符字符* → U+0300–U+036F @@ -503,31 +503,31 @@ operator-character {#operator-character} > > *运算符字符* → U+E0100–U+E01EF > -> operator-characters {#operator-characters} +> ###### operator-characters {#operator-characters} > > *运算符字符组* → [*运算符字符*](#operator-character) [*运算符字符组*](#operator-characters)可选 > -dot-operator-head {#dot-operator-head} +###### {#dot-operator-head} > *头部点运算符* → **..** > -> dot-operator-character {#dot-operator-character} +> ###### dot-operator-character {#dot-operator-character} > > *点运算符字符* → **.** | [*运算符字符*](#operator-character) > -> dot-operator-characters {#dot-operator-characters} +> ###### dot-operator-characters {#dot-operator-characters} > > *点运算符字符组* → [*点运算符字符*](#dot-operator-character) [*点运算符字符组*](#dot-operator-characters)可选 > -binary-operator {#binary-operator} +###### {#binary-operator} > *二元运算符* → [*运算符*](#operator) > -> prefix-operator {#prefix-operator} +> ###### prefix-operator {#prefix-operator} > > *前缀运算符* → [*运算符*](#operator) > -> postfix-operator {#postfix-operator} +> ###### postfix-operator {#postfix-operator} > > *后缀运算符* → [*运算符*](#operator) > diff --git a/source/chapter3/03_Types.md b/source/chapter3/03_Types.md index 0d96571f..e5627a56 100644 --- a/source/chapter3/03_Types.md +++ b/source/chapter3/03_Types.md @@ -27,7 +27,7 @@ Swift 语言存在两种类型:命名型类型和复合型类型。命名型 > 类型语法 > -type {#type} +###### {#type} > *类型* → [*数组类型*](#array-type) > > *类型* → [*字典类型*](#dictionary-type) @@ -53,9 +53,7 @@ type {#type} > *类型* → [*(类型)*](#type) > -type_annotation {#type_annotation} -## 类型注解 - +## 类型注解 {#type_annotation} 类型注解显式地指定一个变量或表达式的值。类型注解始于冒号 `:` 终于类型,比如下面两个例子: ```swift @@ -69,13 +67,11 @@ func someFunction(a: Int) { /* ... */ } > 类型注解语法 > -type-annotation {#type-annotation} +###### {#type-annotation} > *类型注解* → **:** [*特性列表*](./07_Attributes.md#attributes)可选 **输入输出参数**可选 [*类型*](#type) > -type_identifier {#type_identifier} -## 类型标识符 - +## 类型标识符 {#type_identifier} 类型标识符引用命名型类型,还可引用命名型或复合型类型的别名。 大多数情况下,类型标识符引用的是与之同名的命名型类型。例如类型标识符 `Int` 引用命名型类型 `Int`,同样,类型标识符 `Dictionary` 引用命名型类型 `Dictionary`。 @@ -95,16 +91,14 @@ var someValue: ExampleModule.MyType > 类型标识符语法 > -type-identifier {#type-identifier} +###### {#type-identifier} > *类型标识符* → [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.md#generic_argument_clause)可选 | [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.md#generic_argument_clause)可选 **.** [*类型标识符*](#type-identifier) > -type-name {#type-name} +###### {#type-name} > *类型名称* → [*标识符*](./02_Lexical_Structure.md#identifier) > -tuple_type {#tuple_type} -## 元组类型 - +## 元组类型 {#tuple_type} 元组类型是使用括号括起来的零个或多个类型,类型间用逗号隔开。 你可以使用元组类型作为一个函数的返回类型,这样就可以使函数返回多个值。你也可以命名元组类型中的元素,然后用这些名字来引用每个元素的值。元素的名字由一个标识符紧跟一个冒号 `(:)` 组成。[函数和多返回值](../chapter2/06_Functions.md#functions_with_multiple_return_values) 章节里有一个展示上述特性的例子。 @@ -122,22 +116,20 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配 > 元组类型语法 > -tuple-type {#tuple-type} +###### {#tuple-type} > *元组类型* → **(** [*元组类型元素列表*](#tuple-type-element-list) 可选 **)** > -tuple-type-element-list {#tuple-type-element-list} +###### {#tuple-type-element-list} > *元组类型元素列表* → [*元组类型元素*](#tuple-type-element) | [*元组类型元素*](#tuple-type-element) **,** [*元组类型元素列表*](#tuple-type-element-list) > -tuple-type-element {#tuple-type-element} +###### {#tuple-type-element} > *元组类型元素* → [*元素名*](#element-name) [*类型注解*](#type-annotation) | [*类型*](#type) > -element-name {#element-name} +###### {#element-name} > *元素名* → [*标识符*](./02_Lexical_Structure.md#identifier) > -function_type {#function_type} -## 函数类型 - +## 函数类型 {#function_type} 函数类型表示一个函数、方法或闭包的类型,它由参数类型和返回值类型组成,中间用箭头(`->`)隔开: > `参数类型` -> `返回值类型` @@ -190,9 +182,7 @@ var operation: (Int, Int) -> Int // 正确 函数类型若要抛出错误就必须使用 `throws` 关键字来标记,若要重抛错误则必须使用 `rethrows` 关键字来标记。`throws` 关键字是函数类型的一部分,非抛出函数是抛出函数函数的一个子类型。因此,在使用抛出函数的地方也可以使用不抛出函数。抛出和重抛函数的相关描述见章节 [抛出函数与方法](./06_Declarations.md#throwing_functions_and_methods) 和 [重抛函数与方法](./06_Declarations.md#rethrowing_functions_and_methods)。 -Restrictions for Nonescaping Closures {#Restrictions for Nonescaping Closures} -### 对非逃逸闭包的限制 -非逃逸闭包函数不能作为参数传递到另一个非逃逸闭包函数的参数。这样的限制可以让 Swift 在编译时就完成更多的内存访问冲突检查, 而不是在运行时。举个例子: +### 对非逃逸闭包的限制 {#Restrictions for Nonescaping Closures}非逃逸闭包函数不能作为参数传递到另一个非逃逸闭包函数的参数。这样的限制可以让 Swift 在编译时就完成更多的内存访问冲突检查, 而不是在运行时。举个例子: ```swift let external: (Any) -> Void = { _ in () } @@ -219,29 +209,27 @@ func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) { > 函数类型语法 > -function-type {#function-type} +###### {#function-type} > *函数类型* → [*特性列表*](./07_Attributes.md#attributes)可选 [*函数类型子句*](#function-type-argument-clause) **throws**可选 **->** [*类型*](#type) > > *函数类型* → [*特性列表*](./07_Attributes.md#attributes)可选 [*函数类型子句*](#function-type-argument-clause) **rethrows­** **->** [*类型*](#type) > -function-type-argument-clause {#function-type-argument-clause} +###### {#function-type-argument-clause} > *函数类型子句* → (­)­ > > *函数类型子句* → ([*函数类型参数列表*](#function-type-argument-list)*...*­可选)­ > -function-type-argument-list {#function-type-argument-list} +###### {#function-type-argument-list} > *函数类型参数列表* → [*函数类型参数*](function-type-argument) | [*函数类型参数*](function-type-argument), [*函数类型参数列表*](#function-type-argument-list) > -function-type-argument {#function-type-argument} +###### {#function-type-argument} > *函数类型参数* → [*特性列表*](./07_Attributes.md#attributes)可选 **输入输出参数**可选 [*类型*](#type) | [*参数标签*](#argument-label) [*类型注解*](#type-annotation) > -argument-label {#argument-label} +###### {#argument-label} > *参数标签* → [*标识符*](./02_Lexical_Structure.md#identifier) > -array_type {#array_type} -## 数组类型 - +## 数组类型 {#array_type} Swift 语言为标准库中定义的 `Array` 类型提供了如下语法糖: > [`类型`] @@ -269,13 +257,11 @@ var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] > 数组类型语法 > -array-type {#array-type} +###### {#array-type} > *数组类型* → **[** [*类型*](#type) **]** > -dictionary_type {#dictionary_type} -## 字典类型 - +## 字典类型 {#dictionary_type} Swift 语言为标准库中定义的 `Dictionary` 类型提供了如下语法糖: > [`键类型` : `值类型`] @@ -299,13 +285,11 @@ let someDictionary: Dictionary = ["Alex": 31, "Paul": 39] > 字典类型语法 > -dictionary-type {#dictionary-type} +###### {#dictionary-type} > *字典类型* → **[** [*类型*](#type) **:** [*类型*](#type) **]** > -optional_type {#optional_type} -## 可选类型 - +## 可选类型 {#optional_type} Swift 定义后缀 `?` 来作为标准库中的定义的命名型类型 `Optional` 的语法糖。换句话说,下面两个声明是等价的: ```swift @@ -332,13 +316,11 @@ optionalInteger! // 42 > 可选类型语法 > -optional-type {#optional-type} +###### {#optional-type} > *可选类型* → [*类型*](#type) **?** > -implicitly_unwrapped_optional_type {#implicitly_unwrapped_optional_type} -## 隐式解析可选类型 - +## 隐式解析可选类型 {#implicitly_unwrapped_optional_type} 当可以被访问时,Swift 语言定义后缀 `!` 作为标准库中命名类型 `Optional` 的语法糖,来实现自动解包的功能。换句话说,下面两个声明等价: ```swift @@ -368,13 +350,11 @@ let implicitlyUnwrappedArray: [Int]! // 正确 > 隐式解析可选类型语法 > -implicitly-unwrapped-optional-type {#implicitly-unwrapped-optional-type} +###### {#implicitly-unwrapped-optional-type} > *隐式解析可选类型* → [*类型*](#type) **!** > -protocol_composition_type {#protocol_composition_type} -## 协议合成类型 - +## 协议合成类型 {#protocol_composition_type} 协议合成类型是一种符合协议列表中每个指定协议的类型。协议合成类型可能会用在类型注解和泛型参数中。 协议合成类型的形式如下: @@ -388,19 +368,17 @@ protocol_composition_type {#protocol_composition_type} > 协议合成类型语法 > -protocol-composition-type {#protocol-composition-type} +###### {#protocol-composition-type} > *协议合成类型* → [*协议标识符*](#protocol-identifier) & [*协议合成延续*](#protocol-composition-continuation) > -protocol-composition-continuation {#protocol-composition-continuation} +###### {#protocol-composition-continuation} > *协议合成延续* → [*协议标识符*](#protocol-identifier) | [*协议合成类型*](#protocol-composition-type) > -protocol-identifier {#protocol-identifier} +###### {#protocol-identifier} > *协议标识符* → [*类型标识符*](#type-identifier) > -metatype_type {#metatype_type} -## 元类型 - +## 元类型 {#metatype_type} 元类型是指类型的类型,包括类类型、结构体类型、枚举类型和协议类型。 类、结构体或枚举类型的元类型是相应的类型名紧跟 `.Type`。协议类型的元类型——并不是运行时符合该协议的具体类型——而是该协议名字紧跟 `.Protocol`。比如,类 `SomeClass` 的元类型就是 `SomeClass.Type`,协议 `SomeProtocol` 的元类型就是 `SomeProtocal.Protocol`。 @@ -445,13 +423,11 @@ let anotherInstance = metatype.init(string: "some string") > 元类型语法 > -metatype-type {#metatype-type} +###### {#metatype-type} > *元类型* → [*类型*](#type) **.** **Type** | [*类型*](#type) **.** **Protocol** > -type_inheritance_clause {#type_inheritance_clause} -## 类型继承子句 - +## 类型继承子句 {#type_inheritance_clause} 类型继承子句被用来指定一个命名型类型继承自哪个类、采纳哪些协议。类型继承子句也用来指定一个类类型专属协议。类型继承子句开始于冒号 `:`,其后是所需要的类、类型标识符列表或两者都有。 类可以继承单个超类,采纳任意数量的协议。当定义一个类时,超类的名字必须出现在类型标识符列表首位,然后跟上该类需要采纳的任意数量的协议。如果一个类不是从其它类继承而来,那么列表可以以协议开头。关于类继承更多的讨论和例子,请参阅 [继承](../chapter2/13_Inheritance.md)。 @@ -462,18 +438,16 @@ type_inheritance_clause {#type_inheritance_clause} > 类型继承子句语法 > -type_inheritance_clause {#type_inheritance_clause} +###### {#type_inheritance_clause} > *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list) > -type-inheritance-list {#type-inheritance-list} +###### {#type-inheritance-list} > *类型继承列表* → [*类型标识符*](#type-identifier) | [*类型标识符*](#type-identifier) **,** [*类型继承列表*](#type-inheritance-list) > -class-requirement {#class-requirement} +###### {#class-requirement} -type_inference {#type_inference} -## 类型推断 - +## 类型推断 {#type_inference} Swift 广泛使用类型推断,从而允许你省略代码中很多变量和表达式的类型或部分类型。比如,对于 `var x: Int = 0`,你可以完全省略类型而简写成 `var x = 0`,编译器会正确推断出 `x` 的类型 `Int`。类似的,当完整的类型可以从上下文推断出来时,你也可以省略类型的一部分。比如,如果你写了 `let dict: Dictionary = ["A" : 1]`,编译器能推断出 `dict` 的类型是 `Dictionary`。 在上面的两个例子中,类型信息从表达式树的叶子节点传向根节点。也就是说,`var x: Int = 0` 中 `x` 的类型首先根据 `0` 的类型进行推断,然后将该类型信息传递到根节点(变量 `x`)。 diff --git a/source/chapter3/04_Expressions.md b/source/chapter3/04_Expressions.md index e80943a3..7eebe85c 100644 --- a/source/chapter3/04_Expressions.md +++ b/source/chapter3/04_Expressions.md @@ -6,10 +6,10 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表 > 表达式语法 > -expression {#expression} +###### {#expression} > *表达式* → [*try 运算符*](#try-operator)可选 [*前缀表达式*](#prefix-expression) [*二元表达式列表*](#binary-expressions)可选 > -expression-list {#expression-list} +###### {#expression-list} > *表达式列表* → [*表达式*](#expression) | [*表达式*](#expression) **,** [*表达式列表*](#expression-list) > @@ -24,12 +24,12 @@ expression-list {#expression-list} > 前缀表达式语法 > -prefix-expression {#prefix-expression} +###### {#prefix-expression} > *前缀表达式* → [*前缀运算符*](./02_Lexical_Structure.md#prefix-operator)可选 [*后缀表达式*](#postfix-expression) > > *前缀表达式* → [*输入输出表达式*](#in-out-expression) > -in-out-expression {#in-out-expression} +###### {#in-out-expression} > *输入输出表达式* → **&** [*标识符*](./02_Lexical_Structure.md#identifier) > @@ -66,7 +66,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try 关于 `try`、`try?` 和 `try!` 的更多信息,以及该如何使用的例子,请参阅 [错误处理](../chapter2/17_Error_Handling.md)。 > Try 表达式语法 > -try-operator {#try-operator} +###### {#try-operator} > *try 运算符* → **try** | **try?** | **try!** > @@ -85,7 +85,7 @@ try-operator {#try-operator} > 在解析时,一个二元表达式将作为一个扁平列表表示,然后根据运算符的优先级,再进一步进行组合。例如,`2 + 3 * 5` 首先被看作具有五个元素的列表,即 `2`、`+`、`3`、`*`、`5`,随后根据运算符优先级组合为 `(2 + (3 * 5))`。 > -binary-expression {#binary-expression} +###### {#binary-expression} > 二元表达式语法 > > *二元表达式* → [*二元运算符*](./02_Lexical_Structure.md#binary-operator) [*前缀表达式*](#prefix-expression) @@ -96,7 +96,7 @@ binary-expression {#binary-expression} > > *二元表达式* → [*类型转换运算符*](#type-casting-operator) > -binary-expressions {#binary-expressions} +###### {#binary-expressions} > *二元表达式列表* → [*二元表达式*](#binary-expression) [*二元表达式列表*](#binary-expressions)可选 > @@ -117,7 +117,7 @@ binary-expressions {#binary-expressions} > 赋值运算符语法 > -assignment-operator {#assignment-operator} +###### {#assignment-operator} > *赋值运算符* → **=** > @@ -133,7 +133,7 @@ assignment-operator {#assignment-operator} > 三元条件运算符语法 > -conditional-operator {#conditional-operator} +###### {#conditional-operator} > *三元条件运算符* → **?** [*表达式*](#expression) **:** > @@ -176,7 +176,7 @@ f(x as Any) 关于类型转换的更多内容和例子,请参阅 [类型转换](../chapter2/18_Type_Casting.md)。 -type-casting-operator {#type-casting-operator} +###### {#type-casting-operator} > 类型转换运算符语法 > > *类型转换运算符* → **is** [*类型*](./03_Types.md#type) @@ -193,7 +193,7 @@ type-casting-operator {#type-casting-operator} > 基本表达式语法 > -primary-expression {#primary-expression} +###### {#primary-expression} > *基本表达式* → [*标识符*](./02_Lexical_Structure.md#identifier) [*泛型实参子句*](./09_Generic_Parameters_and_Arguments.md#generic-argument-clause)可选 > > *基本表达式* → [*字面量表达式*](#literal-expression) @@ -269,7 +269,7 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者 > 字面量表达式语法 > > -> literal-expression {#literal-expression} +> ###### literal-expression {#literal-expression} > > *字面量表达式* → [*字面量*](./02_Lexical_Structure.md#literal) > @@ -279,38 +279,38 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者 > -> array-literal {#array-literal} +> ###### array-literal {#array-literal} > > *数组字面量* → [[*数组字面量项列表*](#array-literal-items)可选 **]** > -> array-literal-items {#array-literal-items} +> ###### array-literal-items {#array-literal-items} > > *数组字面量项列表* → [*数组字面量项*](#array-literal-item) **,**可选 | [*数组字面量项*](#array-literal-item) **,** [*数组字面量项列表*](#array-literal-items) > -> array-literal-item {#array-literal-item} +> ###### array-literal-item {#array-literal-item} > > *数组字面量项* → [*表达式*](#expression) > > -> dictionary-literal {#dictionary-literal} +> ###### dictionary-literal {#dictionary-literal} > > *字典字面量* → [[*字典字面量项列表*](#dictionary-literal-items) **]** | **[** **:** **]** > -> dictionary-literal-items {#dictionary-literal-items} +> ###### dictionary-literal-items {#dictionary-literal-items} > > *字典字面量项列表* → [*字典字面量项*](#dictionary-literal-item) **,**可选 | [*字典字面量项*](#dictionary-literal-item) **,** [*字典字面量项列表*](#dictionary-literal-items) > -> dictionary-literal-item {#dictionary-literal-item} +> ###### dictionary-literal-item {#dictionary-literal-item} > > *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)。 > -> playground-literal {#playground-literal} +> ###### playground-literal {#playground-literal} > > *playground 字面量* → **#colorLiteral ( red : [*表达式*](#expression) , green :[*表达式*](#expression) [*表达式*](#e[*表达式*](#expression) xpression) , blue :[*表达式*](#expression) , alpha : [*表达式*](#expression) )** > > *playground 字面量* → **#fileLiteral ( resourceName : [*表达式*](#expression) )** > -> playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**self_expression {#self_expression} +> ###### playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**self_expression {#self_expression} > ### Self 表达式 @@ -354,17 +354,17 @@ struct Point { > Self 表达式语法 > -self-expression {#self-expression} +###### {#self-expression} > *self 表达式* → **self** | [*self 方法表达式*](#self-method-expression) | [*self 下标表达式*](#self-subscript-expression) | [*self 构造器表达式*](#self-initializer-expression) > > -self-method-expression {#self-method-expression} +###### {#self-method-expression} > *self 方法表达式* → **self** **.** [*标识符*](./02_Lexical_Structure.md#identifier) > -self-subscript-expression {#self-subscript-expression} +###### {#self-subscript-expression} > *self 下标表达式* → **self** **[** [*函数调用参数表*](#function-call-argument-list­) **]** > -self-initializer-expression {#self-initializer-expression} +###### {#self-initializer-expression} > *self 构造器表达式* → **self** **.** **init** > @@ -384,16 +384,16 @@ self-initializer-expression {#self-initializer-expression} > 父类表达式语法 > -superclass-expression {#superclass-expression} +###### {#superclass-expression} > *父类表达式* → [*父类方法表达式*](#superclass-method-expression) | [*父类下标表达式*](#superclass-subscript-expression) | [*父类构造器表达式*](#superclass-initializer-expression) > -superclass-method-expression {#superclass-method-expression} +###### {#superclass-method-expression} > *父类方法表达式* → **super** **.** [*标识符*](./02_Lexical_Structure.md#identifier) > -superclass-subscript-expression {#superclass-subscript-expression} +###### {#superclass-subscript-expression} > *父类下标表达式* → **super** [[*函数调用参数表*](#function-call-argument-list­) **]** > -superclass-initializer-expression {#superclass-initializer-expression} +###### {#superclass-initializer-expression} > *父类构造器表达式* → **super** **.** **init** > @@ -501,12 +501,12 @@ myFunction { [weak parent = self.parent] in print(parent!.title) } > 闭包表达式语法 > > -> closure-expression {#closure-expression} +> ###### closure-expression {#closure-expression} > > *闭包表达式* → **{** [*闭包签名*](#closure-signature)可选 [*语句*](#statements) **}** > > -> closure-signature {#closure-signature} +> ###### closure-signature {#closure-signature} > > > 闭包签名* → [*参数子句*](#parameter-clause) [*函数结果*](05_Declarations.md#function-result)可选 **in** @@ -520,20 +520,20 @@ myFunction { [weak parent = self.parent] in print(parent!.title) } > *闭包签名* → [*捕获列表*](#capture-list) **in** > > -> capture-list {#capture-list} +> ###### capture-list {#capture-list} > > > 捕获列表* → [ [*捕获列表项列表*](#capture-list-items) **]** > -> capture-list-items {#capture-list-items} +> ###### capture-list-items {#capture-list-items} > > *捕获列表项列表* → [*捕获列表项*](#capture-list-item) | [*捕获列表项*](#capture-list-item) **,** [*捕获列表项列表*](#capture-list-items) > -> capture-list-item {#capture-list-item} +> ###### capture-list-item {#capture-list-item} > > *捕获列表项* → [*捕获说明符*](#capture-specifier)可选 [*表达式*](#expression) > -> capture-specifier {#capture-specifier} +> ###### capture-specifier {#capture-specifier} > > *捕获说明符* → **weak** | **unowned** | **unowned(safe)** | **unowned(unsafe)** > @@ -553,7 +553,7 @@ x = .AnotherValue > 隐式成员表达式语法 > -implicit-member-expression {#implicit-member-expression} +###### {#implicit-member-expression} > *隐式成员表达式* → **.** [*标识符*](./02_Lexical_Structure.md#identifier) > @@ -562,7 +562,7 @@ implicit-member-expression {#implicit-member-expression} > 圆括号表达式语法 > -parenthesized-expression {#parenthesized-expression} +###### {#parenthesized-expression} > *圆括号表达式* → **( [*表达式*](#expression) )** > @@ -583,13 +583,13 @@ parenthesized-expression {#parenthesized-expression} > 元组表达式语法 > -tuple-expression {#tuple-expression} +###### {#tuple-expression} > *元组表达式* → **( )** | **(**[*元组元素*](#tuple-element), [*元组元素列表*](#tuple-element-list) **)** > -tuple-element-list {#tuple-element-list} +###### {#tuple-element-list} > *元组元素列表* → [*元组元素*](#tuple-element) | [*元组元素*](#tuple-element) **,** [*元组元素列表*](#tuple-element-list) > -tuple-element {#tuple-element} +###### {#tuple-element} > *元组元素* → [*表达式*](#expression) | [*标识符*](identifier) **:** [*表达式*](#expression) > @@ -603,7 +603,7 @@ tuple-element {#tuple-element} > 通配符表达式语法 > -wildcard-expression {#wildcard-expression} +###### {#wildcard-expression} > *通配符表达式* → **_** > @@ -740,21 +740,21 @@ print(interestingNumbers[keyPath: \[String: [Int]].["hexagonal"]!.count.bitWidth > key-path 表达式语法 > > -> key-path-expression {#key-path-expression} +> ###### key-path-expression {#key-path-expression} > > *key-path 表达式* → **\\** [类型](./03_Types.md#type)可选 **.** [多个 key-path 组件](#key-path-components) > -> key-path-components {#key-path-components} +> ###### key-path-components {#key-path-components} > > *多个 key-path 组件* → [key-path 组件](#key-path-component) | [key-path 组件](#key-path-component) **.** [多个 key-path 组件](#key-path-components) > -> key-path-component {#key-path-component} +> ###### key-path-component {#key-path-component} > > *key-path 组件* → [标识符](./02_Lexical_Structure.md#identifier) [多个 key-path 后缀](#key-path-postfixes)可选 | [多个 key-path 后缀](#key-path-postfixes) > -> key-path-postfixes {#key-path-postfixes} +> ###### key-path-postfixes {#key-path-postfixes} > -> *多个 key-path 后缀* → [key-path 后缀](#key-path-postfix) [多个 key-path 后缀](#key-path-postfixes)可选 key-path-postfixes {#key-path-postfixes} +> ###### *多个 key-path 后缀* → [key-path 后缀](#key-path-postfix) [多个 key-path 后缀](#key-path-postfixes)可选 key-path-postfixes {#key-path-postfixes} > > *key-path 后缀* → **?** | **!** | **self** | **\[** [函数调用参数表](#function-call-argument-list) **\]** > @@ -809,7 +809,7 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str > 选择器表达式语法 > -selector-expression {#selector-expression} +###### {#selector-expression} > *选择器表达式* → __#selector__ **(** [*表达式*](#expression) **)** > > *选择器表达式* → __#selector__ **(** [*getter:表达式*](#expression) **)** @@ -867,7 +867,7 @@ print(keyPath == c.getSomeKeyPath()) > key-path 字符串表达式语法 > -> key-path-string-expression {#key-path-string-expression} +> ###### key-path-string-expression {#key-path-string-expression} > > *key-path 字符串表达式* → **#keyPath (** [表达式](#expression) **)** > @@ -881,7 +881,7 @@ print(keyPath == c.getSomeKeyPath()) > 后缀表达式语法 > -postfix-expression {#postfix-expression} +###### {#postfix-expression} > *后缀表达式* → [*基本表达式*](#primary-expression) > > *后缀表达式* → [*后缀表达式*](#postfix-expression) [*后缀运算符*](02_Lexical_Structure.md#postfix-operator) @@ -934,29 +934,29 @@ myData.someMethod {$0 == 13} > 函数调用表达式语法 > -> function-call-expression {#function-call-expression} +> ###### function-call-expression {#function-call-expression} > > *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause) > > *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause)可选 [*尾随闭包*](#trailing-closure) > > -> function-call-argument-clause {#function-call-argument-clause} +> ###### function-call-argument-clause {#function-call-argument-clause} > > *函数调用参数子句* → **(** **)** | **(** [*函数调用参数表*](#function-call-argument-list) **)** > -> function-call-argument-list {#function-call-argument-list} +> ###### function-call-argument-list {#function-call-argument-list} > > *函数调用参数表* → [函数调用参数](#function-call-argument) | [函数调用参数](#function-call-argument) **,** [*函数调用参数表*](#function-call-argument-list) > -> function-call-argument {#function-call-argument} +> ###### function-call-argument {#function-call-argument} > > *函数调用参数* → [表达式](#expression) | [标识符](02_Lexical_Structure.md#identifier) **:** [*表达式*](#expression) > > *函数调用参数* → [运算符](./02_Lexical_Structure.md#operator) | [标识符](./02_Lexical_Structure.md#identifier) **:** [*运算符*](./02_Lexical_Structure.md#operator) > > -> trailing-closure {#trailing-closure} +> ###### trailing-closure {#trailing-closure} > > *尾随闭包* → [*闭包表达式*](#closure-expression) > @@ -1001,7 +1001,7 @@ let s3 = someValue.dynamicType.init(data: 7) // 有效 > 构造器表达式语法 > -initializer-expression {#initializer-expression} +###### {#initializer-expression} > *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** > > *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** **(** [*参数名称*](#argument-names) **)** @@ -1067,17 +1067,17 @@ let x = [10, 3, 20, 15, 4] > 显式成员表达式语法 > -explicit-member-expression {#explicit-member-expression} +###### {#explicit-member-expression} > *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*十进制数字*] (02_Lexical_Structure.md#decimal-digit) > > *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02_Lexical_Structure.md#identifier) [*泛型实参子句*](./09_Generic_Parameters_and_Arguments.md#generic-argument-clause)可选
> > *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.md#identifier) **(** [*参数名称*](#argument-names) **)** > -argument-names {#argument-names} +###### {#argument-names} > *参数名称* → [*参数名*](#argument-name) [*参数名称*](#argument-names)可选
> -argument-name {#argument-name} +###### {#argument-name} > *参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) **:** > @@ -1095,7 +1095,7 @@ argument-name {#argument-name} > 后缀 self 表达式语法 > -postfix-self-expression {#postfix-self-expression} +###### {#postfix-self-expression} > *后缀 self 表达式* → [*后缀表达式*](#postfix-expression) **.** **self** > @@ -1112,7 +1112,7 @@ postfix-self-expression {#postfix-self-expression} > 下标表达式语法 > -subscript-expression {#subscript-expression} +###### {#subscript-expression} > *下标表达式* → [*后缀表达式*](#postfix-expression) **[** [*表达式列表*](#expression-list) **]** > @@ -1138,7 +1138,7 @@ someDictionary["a"]![0] = 100 > 强制取值语法 > -forced-value-expression {#forced-value-expression} +###### {#forced-value-expression} > *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!** > @@ -1190,6 +1190,6 @@ someDictionary["a"]?[0] = someFunctionWithSideEffects() > 可选链表达式语法 > -optional-chaining-expression {#optional-chaining-expression} +###### {#optional-chaining-expression} > *可选链表达式* → [*后缀表达式*](#postfix-expression) **?** > diff --git a/source/chapter3/05_Statements.md b/source/chapter3/05_Statements.md index 59b0b262..51259de0 100755 --- a/source/chapter3/05_Statements.md +++ b/source/chapter3/05_Statements.md @@ -7,7 +7,7 @@ > 语句语法 > -statement {#statement} +###### {#statement} > *语句* → [*表达式*](./04_Expressions.md#expression) **;**可选 > > *语句* → [*声明*](./06_Declarations.md#declaration) **;**可选 @@ -26,7 +26,7 @@ statement {#statement} > > *语句* → [*编译器控制语句*](#compiler-control-statement) > -statements {#statements} +###### {#statements} > *多条语句* → [*语句*](#statement) [*多条语句*](#statements)可选 > @@ -38,7 +38,7 @@ statements {#statements} > 循环语句语法 > > -loop-statement {#loop-statement} +###### {#loop-statement} > *循环语句* → [*for-in 语句*](#for-in-statement) > > *循环语句* → [*while 语句*](#while-statement) @@ -62,7 +62,7 @@ for item in collection { > for-in 语句语法 > > -for-in-statement {#for-in-statement} +###### {#for-in-statement} > *for-in 语句* → **for** **case**可选 [*模式*](./08_Patterns.md#pattern) **in** [*表达式*](./04_Expressions.md#expression) [*where 子句*](#where-clause)可选 [*代码块*](05_Declarations.md#code-block) > @@ -89,21 +89,21 @@ while condition { > while 语句语法 > > -while-statement {#while-statement} +###### {#while-statement} > *while 语句* → **while** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block) > -condition-clause {#condition-clause} +###### {#condition-clause} > *条件子句* → [*表达式*](./04_Expressions.md#expression) | [*表达式*](./04_Expressions.md#expression) **,** [*条件列表*](#condition-list) > -condition {#condition} +###### {#condition} > *条件* → [*表达式*](./04_Expressions.md#expression) |[*可用性条件*](#availability-condition) | [*case 条件*](#case-condition) | [*可选绑定条件*](#optional-binding-condition) > > -case-condition {#case-condition} +###### {#case-condition} > *case 条件* → **case** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer) > -optional-binding-condition {#optional-binding-condition} +###### {#optional-binding-condition} > *可选绑定条件* → **let** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer) | **var** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer) > @@ -130,7 +130,7 @@ repeat { > repeat-while 语句语法 > > -repeat-while-statement {#repeat-while-statement} +###### {#repeat-while-statement} > *repeat-while 语句* → **repeat** [*代码块*](./06_Declarations.md#code-block) **while** [*表达式*](./04_Expressions.md#expression) > @@ -142,7 +142,7 @@ repeat-while-statement {#repeat-while-statement} > 分支语句语法 > > -branch-statement {#branch-statement} +###### {#branch-statement} > *分支语句* → [*if 语句*](#if-statement) > > *分支语句* → [*guard 语句*](#guard-statement) @@ -190,10 +190,10 @@ if condition 1 { > if 语句语法 > > -if-statement {#if-statement} +###### {#if-statement} > *if 语句* → **if** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block) [*else 子句*](#else-clause)可选 > -else-clause {#else-clause} +###### {#else-clause} > *else 子句* → **else** [*代码块*](./06_Declarations.md#code-block) | **else** [*if 语句*](#if-statement) > @@ -224,7 +224,7 @@ guard condition else { > guard 语句语法 > > -guard-statement {#guard-statement} +###### {#guard-statement} > *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05_Declarations.md#code-block) > @@ -296,46 +296,46 @@ case .suppressed: > switch 语句语法 > > -switch-statement {#switch-statement} +###### {#switch-statement} > *switch 语句* → **switch** [*表达式*](./04_Expressions.md#expression) **{** [*switch-case 列表*](#switch-cases)可选 **}** > -switch-cases {#switch-cases} +###### {#switch-cases} > *switch case 列表* → [*switch-case*](#switch-case) [*switch-case 列表*](#switch-cases)可选 > -switch-case {#switch-case} +###### {#switch-case} > *switch case* → [*case 标签*](#case-label) [*多条语句*](#statements) | [*default 标签*](#default-label) [*多条语句*](#statements) | [*conditional-switch-case*](#conditional-switch-case-label) > -case-label {#case-label} +###### {#case-label} > *case 标签* → [*属性*](#switch-case-attributes-label)可选 **case** [*case 项列表*](#case-item-list) **:** > -case-item-list {#case-item-list} +###### {#case-item-list} > *case 项列表* → [*模式*](./08_Patterns.md#pattern) [*where 子句*](#where-clause)可选 | [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)可选 **,** [*case 项列表*](#case-item-list) > -default-label {#default-label} +###### {#default-label} > *default 标签* → [*属性*](#switch-case-attributes-label)可选 **default** **:** > > -where-clause {#where-clause} +###### {#where-clause} > *where-clause* → **where** [*where 表达式*](#where-expression) > -where-expression {#where-expression} +###### {#where-expression} > *where-expression* → [*表达式*](./04_Expressions.md#expression) > > -grammar_conditional-switch-case {#grammar_conditional-switch-case} +###### {#grammar_conditional-switch-case} > *conditional-switch-case* → [*switch-if-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label) 可选 [*switch-else-directive-clause*](#switch-case-attributes-label) 可选 [*endif-directive*](#switch-case-attributes-label) > -grammar_switch-if-directive-clause {#grammar_switch-if-directive-clause} +###### {#grammar_switch-if-directive-clause} > *switch-if-directive 语句* → [*if-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) 可选 > -grammar_switch-elseif-directive-clauses {#grammar_switch-elseif-directive-clauses} +###### {#grammar_switch-elseif-directive-clauses} > *switch-elseif-directive 语句(复数)* → [*elseif-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label)可选 > -grammar_switch-elseif-directive-clause {#grammar_switch-elseif-directive-clause} +###### {#grammar_switch-elseif-directive-clause} > *switch-elseif-directive 语句* → [*elseif-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label)可选 > -grammar_switch-else-directive-clause {#grammar_switch-else-directive-clause} +###### {#grammar_switch-else-directive-clause} > *switch-else-directive 语句* → [*else-directive*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) 可选 > @@ -349,7 +349,7 @@ grammar_switch-else-directive-clause {#grammar_switch-else-directive-clause} > 带标签的语句语法 > > -labeled-statement {#labeled-statement} +###### {#labeled-statement} > *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#grammar_loop-statement) > > *带标签的语句* → [*语句标签*](#statement-label) [*if 语句*](#if-statement) @@ -358,10 +358,10 @@ labeled-statement {#labeled-statement} > > > *带标签的语句* → [*语句标签*](#statement-label) [*do 语句*](#sdo-statement) > -statement-label {#statement-label} +###### {#statement-label} > *语句标签* → [*标签名称*](#label-name) **:** > -label-name {#label-name} +###### {#label-name} > *标签名称* → [*标识符*](./02_Lexical_Structure.md#identifier) > @@ -371,7 +371,7 @@ label-name {#label-name} > 控制转移语句语法 > > -control-transfer-statement {#control-transfer-statement} +###### {#control-transfer-statement} > *控制转移语句* → [*break 语句*](#break-statement) > > *控制转移语句* → [*continue 语句*](#continue-statement) @@ -402,7 +402,7 @@ control-transfer-statement {#control-transfer-statement} > break 语句语法 > > -break-statement {#break-statement} +###### {#break-statement} > *break 语句* → **break** [*标签名称*](#label-name)可选 > @@ -427,7 +427,7 @@ break-statement {#break-statement} > continue 语句语法 > > -continue-statement {#continue-statement} +###### {#continue-statement} > *continue 语句* → **continue** [*标签名称*](#label-name)可选 > @@ -441,7 +441,7 @@ continue-statement {#continue-statement} > fallthrough 语句语法 > > -fallthrough-statement {#fallthrough-statement} +###### {#fallthrough-statement} > *fallthrough 语句* → **fallthrough** > @@ -468,7 +468,7 @@ fallthrough-statement {#fallthrough-statement} > return 语句语法 > > -return-statement {#return-statement} +###### {#return-statement} > *return 语句* → **return** [*表达式*](./04_Expressions.md#expression)可选 > @@ -489,7 +489,7 @@ return-statement {#return-statement} > throw 语句语法 > > -throw-statement {#throw-statement} +###### {#throw-statement} > *throw 语句* → **throw** [*表达式*](./04_Expressions.md#expression) > @@ -525,7 +525,7 @@ f() > defer 语句语法 > > -defer-statement {#defer-statement} +###### {#defer-statement} > *延迟语句* → **defer** [*代码块*](./06_Declarations.md#code-block) > @@ -556,13 +556,13 @@ do { > do 语句语法 > > -do-statement {#do-statement} +###### {#do-statement} > *do 语句* → **do** [*代码块*](./06_Declarations.md#code-block) [*多条 catch 子句*](#catch-clauses)可选 > -catch-clauses {#catch-clauses} +###### {#catch-clauses} > *多条 catch 子句* → [*catch 子句*](#catch-clause) [*多条 catch 子句*](#catch-clauses)可选 > -catch-clause {#catch-clause} +###### {#catch-clause} > *catch 子句* → **catch** [*模式*](./08_Patterns.md#pattern)可选 [*where 子句*](#where-clause)可选 [*代码块*](05_Declarations.md#code-block) > @@ -572,7 +572,7 @@ catch-clause {#catch-clause} > 编译器控制语句语法 > > -compiler-control-statement {#compiler-control-statement} +###### {#compiler-control-statement} > *编译器控制语句* → [*条件编译语句*](#grammar_conditional-compilation-block) > > *编译器控制语句* → [*线路控制语句*](#line-control-statement) @@ -651,23 +651,23 @@ statements to compile if both compilation conditions are false > 即使没有被编译,编译配置中的语句仍然会被解析。然而,唯一的例外是编译配置语句中包含语言版本检测函数:仅当 `Swift` 编译器版本和语言版本检测函数中指定的版本号匹配时,语句才会被解析。这种设定能确保旧的编译器不会尝试去解析新 Swift 版本的语法。 > -build-config-statement {#build-config-statement} +###### {#build-config-statement} > 条件编译代码块语法 > > -grammar_conditional-compilation-block {#grammar_conditional-compilation-block} +###### {#grammar_conditional-compilation-block} > *条件编译代码块* → [*if-directive 语句*](#grammar_if-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses)可选 [*else-directive 语句*](#grammar_else-directive-clause)可选 [*endif-directive*](#grammar_endif-directive) > -grammar_if-directive-clause {#grammar_if-directive-clause} +###### {#grammar_if-directive-clause} > *if-directive 语句* → [*if-directive*](#grammar_if-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)可选 > -grammar_elseif-directive-clauses {#grammar_elseif-directive-clauses} +###### {#grammar_elseif-directive-clauses} > *elseif-directive 语句(复数)* → [*elseif-directive 语句*](#grammar_elseif-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses) > -grammar_elseif-directive-clauses {#grammar_elseif-directive-clauses} +###### {#grammar_elseif-directive-clauses} > *elseif-directive 语句* → [*elseif-directive*](#grammar_elseif-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)可选 > -grammar_else-directive-clause {#grammar_else-directive-clause} +###### {#grammar_else-directive-clause} > *else-directive 语句* → [*else-directive*](#grammar_else-directive) [*语句(复数)*](#statements)可选 > @@ -681,7 +681,7 @@ grammar_else-directive-clause {#grammar_else-directive-clause} > *endif-directive* → **#endif** > -compilation-condition {#compilation-condition} +###### {#compilation-condition} > *编译条件* → [*平台条件*](#grammar_platform-condition) > > *编译条件* → [*标识符*](./02_Lexical_Structure.md#identifier) @@ -697,41 +697,41 @@ compilation-condition {#compilation-condition} > *编译条件* → [*编译条件*](#compilation-condition) **||** [*编译条件*](#compilation-condition) > -grammar_platform-condition {#grammar_platform-condition} -grammar_platform-condition-os {#grammar_platform-condition-os} +###### {#grammar_platform-condition} +###### {#grammar_platform-condition-os} > *平台条件* → **os ( [*操作系统*](#operating-system) )** > -grammar_platform-condition-arch {#grammar_platform-condition-arch} +###### {#grammar_platform-condition-arch} > *平台条件* → **arch ( [*架构*](#architecture) )** > -grammar_platform-condition-swift {#grammar_platform-condition-swift} +###### {#grammar_platform-condition-swift} > *平台条件* → **swift ( >= [*swift 版本*](#swift-version) )** | **swift ( < [*swift 版本*](#swift-version) )** > -grammar_platform-condition-compiler {#grammar_platform-condition-compiler} +###### {#grammar_platform-condition-compiler} > *平台条件* → **compiler ( >= [*swift 版本*](#swift-version) )** | **compiler ( < [*swift 版本*](#swift-version) )** > -grammar_platform-condition-canImport {#grammar_platform-condition-canImport} +###### {#grammar_platform-condition-canImport} > *平台条件* → **canImport ( [*模块名*](#grammar_module-name) )** > -grammar_platform-condition-targetEnvironment {#grammar_platform-condition-targetEnvironment} +###### {#grammar_platform-condition-targetEnvironment} > *平台条件* → **targetEnvironment ( [*环境*](#grammar_environment) )** > -operating-system {#operating-system} +###### {#operating-system} > *操作系统* → **macOS** | **iOS** | **watchOS** | **tvOS** > -architecture {#architecture} +###### {#architecture} > *架构* → **i386** | **x86_64** | **arm** | **arm64** > -swift-version {#swift-version} +###### {#swift-version} > *swift 版本* → [*十进制数字*](./02_Lexical_Structure.md#decimal-digit) ­**.** ­[*swift 版本延续*](#grammar_swift-version-continuation) 可选 > -grammar_swift-version-continuation {#grammar_swift-version-continuation} +###### {#grammar_swift-version-continuation} > *swift 版本延续* → **.** [*十进制数字*](./02_Lexical_Structure.md#decimal-digit) [*swift 版本延续*](#grammar_swift-version-continuation) 可选 > -grammar_module-name {#grammar_module-name} +###### {#grammar_module-name} > *模块名* → [*identifier*](./02_Lexical_Structure.md#identifier) > -grammar_environment {#grammar_environment} +###### {#grammar_environment} > *环境* → **模拟器** > @@ -750,7 +750,7 @@ grammar_environment {#grammar_environment} 第二种的行控制语句,`#sourceLocation()`,会将源代码的定位信息重置回默认的行号和文件名。 -line-control-statement {#line-control-statement} +###### {#line-control-statement} > 行控制语句语法 > > @@ -758,10 +758,10 @@ line-control-statement {#line-control-statement} > > *行控制语句* → **#sourceLocation()** > -line-number {#line-number} +###### {#line-number} > *行号* → 大于 0 的十进制整数 > -file-name {#file-name} +###### {#file-name} > *文件名* → [*静态字符串字面量*](./02_Lexical_Structure.md#static-string-literal) > @@ -779,12 +779,12 @@ file-name {#file-name} > 编译时诊断语句语法 > > -grammar_compile-time-diagnostic-statement {#grammar_compile-time-diagnostic-statement} +###### {#grammar_compile-time-diagnostic-statement} > *诊断语句* → **#error** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)** > > *诊断语句* → **#warning** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)** > -grammar_diagnostic-message {#grammar_diagnostic-message} +###### {#grammar_diagnostic-message} > *诊断语句* → [*静态字符串字面量*](./02_Lexical_Structure.md#static-string-literal) > @@ -810,19 +810,19 @@ if #available(platform name version, ..., *) { > 可用性条件语法 > > -availability-condition {#availability-condition} +###### {#availability-condition} > *可用性条件* → **#available** **(** [*可用性参数列表*](#availability-arguments) **)** > -availability-arguments {#availability-arguments} +###### {#availability-arguments} > *可用性参数列表* → [*可用性参数*](#availability-argument) | [*可用性参数*](#availability-argument) **,** [*可用性参数列表*](#availability-arguments) > -availability-argument {#availability-argument} +###### {#availability-argument} > *可用性参数* → [平台名称](#platform-name) [平台版本](#platform-version) > > *可用性条件* → __*__ > > -platform-name {#platform-name} +###### {#platform-name} > *平台名称* → **iOS** | **iOSApplicationExtension** > > *平台名称* → **OSX** | **macOSApplicationExtension** @@ -831,7 +831,7 @@ platform-name {#platform-name} > > *平台名称* → **tvOS** > -platform-version {#platform-version} +###### {#platform-version} > *平台版本* → [十进制数字](./02_Lexical_Structure.md#decimal-digits) > > *平台版本* → [十进制数字](./02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](./02_Lexical_Structure.md#decimal-digits) diff --git a/source/chapter3/06_Declarations.md b/source/chapter3/06_Declarations.md index 0950d826..2189cb36 100755 --- a/source/chapter3/06_Declarations.md +++ b/source/chapter3/06_Declarations.md @@ -5,7 +5,7 @@ > 声明语法 > -> declaration {#declaration} +> ###### declaration {#declaration} > > *声明* → [*导入声明*](#import-declaration) > @@ -35,7 +35,7 @@ > > *声明* → [*运算符声明*](#operator-declaration) > -> declarations {#declarations} +> ###### declarations {#declarations} > > *多条声明* → [*声明*](#declaration) [*多条声明*](#declarations)可选 > @@ -61,7 +61,7 @@ Swift 的源文件中的顶级代码(top-level code)由零个或多个语句 > 代码块语法 > -> code-block {#code-block} +> ###### code-block {#code-block} > > *代码块* → **{** [*多条语句*](./05_Statements.md#statements)可选 **}** > @@ -80,22 +80,22 @@ import 导入类型 模块.符号名 import 模块.子模块 ``` -grammer_of_an_import_declaration {#grammer_of_an_import_declaration} +###### {#grammer_of_an_import_declaration} > 导入声明语法 > -> import-declaration {#import-declaration} +> ###### import-declaration {#import-declaration} > > *导入声明* → [*特性列表*](./07_Attributes.md#attributes)可选 **import** [*导入类型*](#import-kind)可选 [*导入路径*](#import-path) > -> import-kind {#import-kind} +> ###### import-kind {#import-kind} > > *导入类型* → **typealias** | **struct** | **class** | **enum** | **protocol** | **let** | **var** | **func** > -> import-path {#import-path} +> ###### import-path {#import-path} > > *导入路径* → [*导入路径标识符*](#import-path-identifier) | [*导入路径标识符*](#import-path-identifier) **.** [*导入路径*](#import-path) > -> import-path-identifier {#import-path-identifier} +> ###### import-path-identifier {#import-path-identifier} > > *导入路径标识符* → [*标识符*](./02_Lexical_Structure.md#identifier) | [*运算符*](./02_Lexical_Structure.md#operator) > @@ -132,22 +132,22 @@ print("The second number is \(secondNumber).") 如果还想获得更多关于常量的信息或者想在使用中获得帮助,请参阅 [常量和变量](../chapter2/01_The_Basics.md#constants_and_variables) 和 [存储属性](../chapter2/10_Properties.md#stored_properties)。 -grammer_of_a_constant_declaration {#grammer_of_a_constant_declaration} +###### {#grammer_of_a_constant_declaration} > 常量声明语法 > -> constant-declaration {#constant-declaration} +> ###### constant-declaration {#constant-declaration} > > *常量声明* → [*特性列表*](./07_Attributes.md#attributes)可选 [*声明修饰符列表*](#declaration-modifiers)可选 **let** [*模式构造器列表*](pattern-initializer-list) > -> pattern-initializer-list {#pattern-initializer-list} +> ###### pattern-initializer-list {#pattern-initializer-list} > > *模式构造器列表* → [*模式构造器*](#pattern-initializer) | [*模式构造器*](#pattern-initializer) **,** [*模式构造器列表*](#pattern-initializer-list) > -> pattern-initializer {#pattern-initializer} +> ###### pattern-initializer {#pattern-initializer} > > *模式构造器* → [*模式*](./08_Patterns.md#pattern) [*构造器*](#initializer)可选 > -> initializer {#initializer} +> ###### initializer {#initializer} > > *构造器* → **=** [*表达式*](./04_Expressions.md#expression) > @@ -242,10 +242,10 @@ var 变量名称: 类型 = 表达式 { > 在一个类声明中,使用关键字 `static` 与同时使用 `class` 和 `final` 去标记一个声明的效果相同。 > -grammer_of_a_variable_declaration {#grammer_of_a_variable_declaration} +###### {#grammer_of_a_variable_declaration} > 变量声明语法 > -variable-declaration {#variable-declaration} +###### {#variable-declaration} > *变量声明* → [*变量声明头*](#variable-declaration-head) [*模式构造器列表*](#pattern-initializer-list) > > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.md#type-annotation) [*代码块*](#code-block) @@ -259,58 +259,58 @@ variable-declaration {#variable-declaration} > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.md#type-annotation) [*构造器*](#initializer)可选 [*willSet-didSet 代码块*](#willSet-didSet-block) > -variable-declaration-head {#variable-declaration-head} +###### {#variable-declaration-head} > *变量声明头* → [*特性列表*](./07_Attributes.md#attributes)可选 [*声明修饰符列表*](#declaration-modifiers)可选 **var** > -> variable-name {#variable-name} +> ###### variable-name {#variable-name} > > *变量名称* → [*标识符*](./02_Lexical_Structure.md#identifier) > -getter-setter-block {#getter-setter-block} +###### {#getter-setter-block} > *getter-setter 代码块* → [*代码块*](#code-block) > > *getter-setter 代码块* → **{** [*getter 子句*](#getter-clause) [*setter 子句*](#setter-clause)可选 **}** > > *getter-setter 代码块* → **{** [*setter 子句*](#setter-clause) [*getter 子句*](#getter-clause) **}** > -> getter-clause {#getter-clause} +> ###### getter-clause {#getter-clause} > > *getter 子句* → [*特性列表*](./07_Attributes.md#attributes)可选 **get** [*代码块*](#code-block) > -> setter-clause {#setter-clause} +> ###### setter-clause {#setter-clause} > > *setter 子句* → [*特性列表*](./07_Attributes.md#attributes)可选 **set** [*setter 名称*](#setter-name)可选 [*代码块*](#code-block) > -> setter-name {#setter-name} +> ###### setter-name {#setter-name} > > *setter 名称* → **(** [*标识符*](./02_Lexical_Structure.md#identifier) **)** > -getter-setter-keyword-block {#getter-setter-keyword-block} +###### {#getter-setter-keyword-block} > *getter-setter 关键字代码块* → **{** [*getter 关键字子句*](#getter-keyword-clause) [*setter 关键字子句*](#setter-keyword-clause)可选 **}** > > *getter-setter 关键字代码块* → **{** [*setter 关键字子句*](#setter-keyword-clause) [*getter 关键字子句*](#getter-keyword-clause) **}** > -> getter-keyword-clause {#getter-keyword-clause} +> ###### getter-keyword-clause {#getter-keyword-clause} > > *getter 关键字子句* → [*特性列表*](./07_Attributes.md#attributes)可选 **get** > -> setter-keyword-clause {#setter-keyword-clause} +> ###### setter-keyword-clause {#setter-keyword-clause} > > *setter 关键字子句* → [*特性列表*](./07_Attributes.md#attributes)可选 **set** > -willSet-didSet-block {#willSet-didSet-block} +###### {#willSet-didSet-block} > *willSet-didSet 代码块* → **{** [*willSet 子句*](#willSet-clause) [*didSet 子句*](#didSet-clause)可选 **}** > > *willSet-didSet 代码块* → **{** [*didSet 子句*](#didSet-clause) [*willSet 子句*](#willSet-clause)可选 **}** > -> willSet-clause {#willSet-clause} +> ###### willSet-clause {#willSet-clause} > > *willSet 子句* → [*特性列表*](./07_Attributes.md#attributes)可选 **willSet** [*setter 名称*](#setter-name)可选 [*代码块*](#code-block) > -> didSet-clause {#didSet-clause} +> ###### didSet-clause {#didSet-clause} > > *didSet 子句* → [*特性列表*](./07_Attributes.md#attributes)可选 **didSet** [*setter 名称*](#setter-name)可选 [*代码块*](#code-block) > @@ -370,19 +370,19 @@ func sum(_ sequence: T) -> Int where T.Element == Int { 另请参阅 [协议关联类型声明](#protocol_associated_type_declaration)。 -grammer_of_a_type_alias_declaration {#grammer_of_a_type_alias_declaration} +###### {#grammer_of_a_type_alias_declaration} > 类型别名声明语法 > > -> typealias-declaration {#typealias-declaration} +> ###### typealias-declaration {#typealias-declaration} > > *类型别名声明* → [*特性列表*](./07_Attributes.md#attributes)可选 [*访问级别修饰符*](#access-level-modifier)可选 **typealias** [*类型别名名称*](#typealias-name) [*类型别子句*](#typealias-clause) [*类型别名赋值*](#typealias-assignment) > -> typealias-name {#typealias-name} +> ###### typealias-name {#typealias-name} > > *类型别名名称* → [*标识符*](./02_Lexical_Structure.md#identifier) > -> typealias-assignment {#typealias-assignment} +> ###### typealias-assignment {#typealias-assignment} > > *类型别名赋值* → **=** [*类型*](./03_Types.md#type) > @@ -570,46 +570,46 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它 你可以重写一个不归方法,但是新的方法必须保持原有的返回类型和没有返回的行为。 -grammer_of_a_function_declaration {#grammer_of_a_function_declaration} +###### {#grammer_of_a_function_declaration} > 函数声明语法 > -function-declaration {#function-declaration} +###### {#function-declaration} > *函数声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*函数签名*](#function-signature) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause) [*函数体*](#function-body)可选 > -function-head {#function-head} +###### {#function-head} > *函数头* → [*特性列表*](./07_Attributes.md#attributes)可选 [*声明修饰符列表*](#declaration-modifiers)可选 **func** > -> function-name {#function-name} +> ###### function-name {#function-name} > > *函数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | [*运算符*](./02_Lexical_Structure.md#operator) > > -> function-signature {#function-signature} +> ###### function-signature {#function-signature} > > > *函数签名* → [*参数子句列表*](#parameter-clauses) **throws**可选 [*函数结果*](#function-result)可选 > > *函数签名* → [*参数子句列表*](#parameter-clauses) **rethrows** [*函数结果*](#function-result)可选 > -> function-result {#function-result} +> ###### function-result {#function-result} > > *函数结果* → **->** [*特性列表*](./07_Attributes.md#attributes)可选 [*类型*](./03_Types.md#type) > -> function-body {#function-body} +> ###### function-body {#function-body} > > *函数体* → [*代码块*](#code-block) > -> parameter-clause {#parameter-clause} +> ###### parameter-clause {#parameter-clause} > > *参数子句* → **(** **)** | **(** [*参数列表*](#parameter-list) **)** > -> parameter-list {#parameter-list} +> ###### parameter-list {#parameter-list} > > *参数列表* → [*参数*](#parameter) | [*参数*](#parameter) **,** [*参数列表*](#parameter-list) > -> parameter {#parameter} +> ###### parameter {#parameter} > > *参数* → [*外部参数名*](#external-parameter-name)可选 [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.md#type-annotation) [*默认参数子句*](#default-argument-clause)可选 > @@ -617,15 +617,15 @@ function-head {#function-head} > > *参数* → [*外部参数名*](#external-parameter-name)可选 [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.md#type-annotation) **...** > -> external-parameter-name {#external-parameter-name} +> ###### external-parameter-name {#external-parameter-name} > > *外部参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | **_** > -> local-parameter-name {#local-parameter-name} +> ###### local-parameter-name {#local-parameter-name} > > *内部参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | **_** > -> default-argument-clause {#default-argument-clause} +> ###### default-argument-clause {#default-argument-clause} > > *默认参数子句* → **=** [*表达式*](./04_Expressions.md#expression) > @@ -731,11 +731,11 @@ enum GamePlayMode: String { 可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../chapter2/08_Enumerations.md#matching_enumeration_values_with_a_switch_statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](./08_Patterns.md#enumeration_case_pattern) 所述。 -grammer_of_an_enumeration_declaration {#grammer_of_an_enumeration_declaration} +###### {#grammer_of_an_enumeration_declaration} > 枚举声明语法 > > -> enum-declaration {#enum-declaration} +> ###### enum-declaration {#enum-declaration} > > *枚举声明* → [*特性列表*](./07_Attributes.md#attributes)可选 [*访问级别修饰符*](#access-level-modifier)可选 [*联合风格枚举*](#union-style-enum) > @@ -744,65 +744,65 @@ grammer_of_an_enumeration_declaration {#grammer_of_an_enumeration_declaration} > > *联合风格枚举* → **indirect**可选 **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [类型继承子句](./03_Types.md#type-inheritance-clause)可选 **{** [*多个联合风格枚举成员*](#union-style-enum-members)可选 **}** > -> union-style-enum-members {#union-style-enum-members} +> ###### union-style-enum-members {#union-style-enum-members} > > *多个联合风格枚举成员* → [*联合风格枚举成员*](#union-style-enum-member) [*多个联合风格枚举成员*](#union-style-enum-members)可选 > -> union-style-enum-member {#union-style-enum-member} +> ###### union-style-enum-member {#union-style-enum-member} > > *联合风格枚举成员* → [*声明*](#declaration) | [*联合风格枚举用例子句*](#union-style-enum-case-clause) | [*编译控制流语句*](05_Statements.md#compiler-control-statement) > -> union-style-enum-case-clause {#union-style-enum-case-clause} +> ###### union-style-enum-case-clause {#union-style-enum-case-clause} > > *联合风格枚举用例子句* → [*特性列表*](./07_Attributes.md#attributes)可选 **indirect**可选 **case** [*联合风格枚举用例列表*](#union-style-enum-case-list) > -> union-style-enum-case-list {#union-style-enum-case-list} +> ###### union-style-enum-case-list {#union-style-enum-case-list} > > *联合风格枚举用例列表* → [*联合风格枚举用例*](#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 {#union-style-enum-case} > > *联合风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*元组类型*](03_Types.md#tuple-type)可选 > -> enum-name {#enum-name} +> ###### enum-name {#enum-name} > > *枚举名称* → [*标识符*](./02_Lexical_Structure.md#identifier) > -> enum-case-name {#enum-case-name} +> ###### enum-case-name {#enum-case-name} > > *枚举用例名称* → [*标识符*](./02_Lexical_Structure.md#identifier) > > -> raw-value-style-enum {#raw-value-style-enum} +> ###### raw-value-style-enum {#raw-value-style-enum} > > > *原始值风格枚举* → **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*类型继承子句*](./03_Types.md#type-inheritance-clause) [*泛型 where 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause) **{** [*多个原始值风格枚举成员*](#raw-value-style-enum-members) **}** > -> raw-value-style-enum-members {#raw-value-style-enum-members} +> ###### raw-value-style-enum-members {#raw-value-style-enum-members} > > *多个原始值风格枚举成员* → [*原始值风格枚举成员*](#raw-value-style-enum-member) [*多个原始值风格枚举成员*](#raw-value-style-enum-members)可选 > -> raw-value-style-enum-member {#raw-value-style-enum-member} +> ###### raw-value-style-enum-member {#raw-value-style-enum-member} > > *原始值风格枚举成员* → [*声明*](#declaration) | [*原始值风格枚举用例子句*](#raw-value-style-enum-case-clause) | [*编译控制流语句*](05_Statements.md#compiler-control-statement) > -> raw-value-style-enum-case-clause {#raw-value-style-enum-case-clause} +> ###### raw-value-style-enum-case-clause {#raw-value-style-enum-case-clause} > > *原始值风格枚举用例子句* → [*特性列表*](./07_Attributes.md#attributes)可选 **case** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list) > -> raw-value-style-enum-case-list {#raw-value-style-enum-case-list} +> ###### raw-value-style-enum-case-list {#raw-value-style-enum-case-list} > > *原始值风格枚举用例列表* → [*原始值风格枚举用例*](#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 {#raw-value-style-enum-case} > > *原始值风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*原始值赋值*](#raw-value-assignment)可选 > -> raw-value-assignment {#raw-value-assignment} +> ###### raw-value-assignment {#raw-value-assignment} > > *原始值赋值* → **=** [*原始值字面量*](#raw-value-literal) > -> raw-value-literal {#raw-value-literal} +> ###### raw-value-literal {#raw-value-literal} > > *原始值字面量* → [数字型字面量](./02_Lexical_Structure.md#numeric-literal) | [字符串型字面量](./02_Lexical_Structure.md#static-string-literal) | [布尔型字面量](./02_Lexical_Structure.md#boolean-literal) > @@ -837,28 +837,28 @@ struct 结构体名称: 采纳的协议 { 可以使用扩展声明来扩展结构体类型的行为,请参阅 [扩展声明](#extension_declaration)。 -grammer_of_a_structure_declaration {#grammer_of_a_structure_declaration} +###### {#grammer_of_a_structure_declaration} > 结构体声明语法 > > -> struct-declaration {#struct-declaration} +> ###### struct-declaration {#struct-declaration} > > *结构体声明* → [*特性列表*](./07_Attributes.md#attributes)可选 [*访问级别修饰符*](#access-level-modifier) 可选 **struct** [*结构体名称*](#struct-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*类型继承子句*](./03_Types.md#type-inheritance-clause)可选 [*泛型 where 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*结构体主体*](#struct-body) > -> struct-name {#struct-name} +> ###### struct-name {#struct-name} > > *结构体名称* → [*标识符*](./02_Lexical_Structure.md#identifier) > -> struct-body {#struct-body} +> ###### struct-body {#struct-body} > > *结构体主体* → **{** [*多条声明*](#declarations)可选 **}** > -> struct-name {#struct-name} +> ###### struct-name {#struct-name} > > > *结构体多个成员* → [*结构体成员*](#struct-member) [*结构体多个成员*](#struct-members)可选 > -> struct-member {#struct-member} +> ###### struct-member {#struct-member} > > *结构体成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement) > @@ -896,28 +896,28 @@ class 类名: 超类, 采纳的协议 { 可以使用扩展声明来扩展类的行为,请参阅 [扩展声明](#extension_declaration)。 -grammer_of_a_class_declaration {#grammer_of_a_class_declaration} +###### {#grammer_of_a_class_declaration} > 类声明语法 > > -> class-declaration {#class-declaration} +> ###### class-declaration {#class-declaration} > > *类声明* → [*特性列表*](./07_Attributes.md#attributes)可选 [访问级别修饰符](#access-level-modifier)可选 **final**可选 **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*类型继承子句*](./03_Types.md#type-inheritance-clause)可选 [*泛型 where 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*类主体*](#class-body) > > *类声明* → [*特性列表*](./07_Attributes.md#attributes)可选 **final** [访问级别修饰符](#access-level-modifier)可选 **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*类型继承子句*](./03_Types.md#type-inheritance-clause)可选 [*泛型 where 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*类主体*](#class-body) > -> class-name {#class-name} +> ###### class-name {#class-name} > > *类名* → [*标识符*](./02_Lexical_Structure.md#identifier) > -> class-body {#class-body} +> ###### class-body {#class-body} > > *类主体* → **{** [*多条声明*](#declarations)可选 **}** > > > *类多个成员* → [*类成员*](#class-member) [*类多个成员*](#class-members)可选 > -> class-member {#class-member} +> ###### class-member {#class-member} > > *类成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement) > @@ -963,31 +963,31 @@ protocol SomeProtocol: AnyObject { 可以使用协议来声明作为代理的类或者结构体应该实现的方法,正如 [委托(代理)模式](../chapter2/21_Protocols.md#delegation) 中所述。 -grammer_of_a_protocol_declaration {#grammer_of_a_protocol_declaration} +###### {#grammer_of_a_protocol_declaration} > 协议声明语法 > > -> protocol-declaration {#protocol-declaration} +> ###### protocol-declaration {#protocol-declaration} > > *协议声明* → [*特性列表*](./07_Attributes.md#attributes)可选 [*访问级别修饰符*](#access-level-modifier)可选 **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03_Types.md#type-inheritance-clause)可选 [*泛型 where 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*协议主体*](#protocol-body) > -> protocol-name {#protocol-name} +> ###### protocol-name {#protocol-name} > > *协议名称* → [*标识符*](./02_Lexical_Structure.md#identifier) > -> protocol-body {#protocol-body} +> ###### protocol-body {#protocol-body} > > *协议主体* → **{** [*协议成员声明列表*](#protocol-member-declarations)可选 **}** > > > *协议多个成员* → [*协议成员*](#protocol-member) [*协议多个成员*](#protocol-members)可选 > -> protocol-member {#protocol-member} +> ###### protocol-member {#protocol-member} > > *协议成员* → [*协议成员声明*](#protocol-member-declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement) > > -> protocol-member-declaration {#protocol-member-declaration} +> ###### protocol-member-declaration {#protocol-member-declaration} > > *协议成员声明* → [*协议属性声明*](#protocol-property-declaration) > @@ -999,7 +999,7 @@ grammer_of_a_protocol_declaration {#grammer_of_a_protocol_declaration} > > *协议成员声明* → [*协议关联类型声明*](#protocol-associated-type-declaration) > -> protocol-member-declarations {#protocol-member-declarations} +> ###### protocol-member-declarations {#protocol-member-declarations} > > *协议成员声明列表* → [*协议成员声明*](#protocol-member-declaration) [*协议成员声明列表*](#protocol-member-declarations)可选 > @@ -1017,10 +1017,10 @@ var 属性名: 类型 { get set } 另请参阅 [变量声明](#variable_declaration)。 -grammer_of_an_import_declaration {#grammer_of_an_import_declaration} +###### {#grammer_of_an_import_declaration} > 协议属性声明语法 > -> protocol-property-declaration {#protocol-property-declaration} +> ###### protocol-property-declaration {#protocol-property-declaration} > > *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.md#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > @@ -1032,10 +1032,10 @@ grammer_of_an_import_declaration {#grammer_of_an_import_declaration} 另请参阅 [函数声明](#function_declaration)。 -grammer_of_a_protocol_declaration {#grammer_of_a_protocol_declaration} +###### {#grammer_of_a_protocol_declaration} > 协议方法声明语法 > -> protocol-method-declaration {#protocol-method-declaration} +> ###### protocol-method-declaration {#protocol-method-declaration} > > *协议方法声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*函数签名*](#function-signature) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 > @@ -1050,10 +1050,10 @@ grammer_of_a_protocol_declaration {#grammer_of_a_protocol_declaration} 另请参阅 [构造器声明](#initializer_declaration)。 -grammer_of_a_protocol_initializer_declaration {#grammer_of_a_protocol_initializer_declaration} +###### {#grammer_of_a_protocol_initializer_declaration} > 协议构造器声明语法 > -> protocol-initializer-declaration {#protocol-initializer-declaration} +> ###### protocol-initializer-declaration {#protocol-initializer-declaration} > > *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*参数子句*](#parameter-clause) **throws**可选 [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 > @@ -1072,10 +1072,10 @@ subscript (参数列表) -> 返回类型 { get set } 另请参阅 [下标声明](#subscript_declaration)。 -grammer_of_a_protocol_subscript_declaration {#grammer_of_a_protocol_subscript_declaration} +###### {#grammer_of_a_protocol_subscript_declaration} > 协议下标声明语法 > -> protocol-subscript-declaration {#protocol-subscript-declaration} +> ###### protocol-subscript-declaration {#protocol-subscript-declaration} > > *协议下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > @@ -1103,10 +1103,10 @@ protocol SubProtocolB: SomeProtocol where SomeType: Equatable { } 另请参阅 [类型别名声明](#type_alias_declaration)。 -grammer_of_a_protocol_associated_type_declaration {#grammer_of_a_protocol_associated_type_declaration} +###### {#grammer_of_a_protocol_associated_type_declaration} > 协议关联类型声明语法 > -> protocol-associated-type-declaration {#protocol-associated-type-declaration} +> ###### protocol-associated-type-declaration {#protocol-associated-type-declaration} > > *协议关联类型声明* → [*特性列表*](./07_Attributes.md#attributes)可选 [*访问级别修饰符*](#access-level-modifier)可选 **associatedtype** [*类型别名头*](#typealias-head) [*类型继承子句*](03_Types.md#type-inheritance-clause)可选 [*类型别名赋值*](#typealias-assignment)可选 [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 > @@ -1194,16 +1194,16 @@ if let actualInstance = SomeStruct(input: "Hello") { 更多关于可失败构造器的信息和例子,请参阅 [可失败构造器](../chapter2/14_Initialization.md#failable_initializers)。 -grammer_of_an_initializer_declaration {#grammer_of_an_initializer_declaration} +###### {#grammer_of_an_initializer_declaration} > 构造器声明语法 > -> initializer-declaration {#initializer-declaration} +> ###### initializer-declaration {#initializer-declaration} > > *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*参数子句*](#parameter-clause) **throws**可选 [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*构造器主体*](#initializer-body) > > *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*参数子句*](#parameter-clause) **rethrows**可选 [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*构造器主体*](#initializer-body) > -> initializer-head {#initializer-head} +> ###### initializer-head {#initializer-head} > > *构造器头* → [*特性列表*](./07_Attributes.md#attributes)可选 [*声明修饰符列表*](#declaration-modifiers)可选 **init** > @@ -1211,7 +1211,7 @@ grammer_of_an_initializer_declaration {#grammer_of_an_initializer_declaration} > > *构造器头* → [*特性列表*](./07_Attributes.md#attributes)可选 [*声明修饰符列表*](#declaration-modifiers)可选 **init** **!** > -> initializer-body {#initializer-body} +> ###### initializer-body {#initializer-body} > > *构造器主体* → [*代码块*](#code-block) > @@ -1233,10 +1233,10 @@ deinit { 关于如何在类声明中使用析构器的例子,请参阅 [析构过程](../chapter2/15_Deinitialization.md)。 -grammer_of_a_deinitializer_declaration {#grammer_of_a_deinitializer_declaration} +###### {#grammer_of_a_deinitializer_declaration} > 析构器声明语法 > -> deinitializer-declaration {#deinitializer-declaration} +> ###### deinitializer-declaration {#deinitializer-declaration} > > *析构器声明* → [*特性列表*](./07_Attributes.md#attributes)可选 **deinit** [*代码块*](#code-block) > @@ -1416,15 +1416,15 @@ extension Array: Loggable where Element: MarkedLoggable { } // 报错: redundant conformance of 'Array' to protocol 'Loggable' ``` -grammer_of_an_extension_declaration {#grammer_of_an_extension_declaration} +###### {#grammer_of_an_extension_declaration} > 扩展声明语法 > > -> extension-declaration {#extension-declaration} +> ###### extension-declaration {#extension-declaration} > > *扩展声明* → [特性](./07_Attributes.md#type_attributes)可选 [访问级别修饰符](#access-level-modifier)可选 **extension** [*类型标识符*](03_Types.md#type-identifier) [*类型-继承-子句*](./03_Types.md#type-inheritance-clause)可选 [*泛型 where 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*扩展主体*](#extension-body) > -> extension-body {#extension-body} +> ###### extension-body {#extension-body} > > *扩展主体* → **{** [*多条声明*](#declarations)可选 **}** > @@ -1464,11 +1464,11 @@ subscript (参数列表) -> 返回类型 { 更多关于下标的信息和例子,请参阅 [下标](../chapter2/12_Subscripts.md)。 -grammer_of_a_subscript_declaration {#grammer_of_a_subscript_declaration} +###### {#grammer_of_a_subscript_declaration} > 下标声明语法 > > -> subscript-declaration {#subscript-declaration} +> ###### subscript-declaration {#subscript-declaration} > > *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*代码块*](#code-block) > @@ -1476,11 +1476,11 @@ grammer_of_a_subscript_declaration {#grammer_of_a_subscript_declaration} > > *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)可选 [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > -> subscript-head {#subscript-head} +> ###### subscript-head {#subscript-head} > > *下标头* → [*特性列表*](./07_Attributes.md#attributes)可选 [*声明修饰符列表*](#declaration-modifiers)可选 **subscript** [*泛型参数子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)可选 [*参数子句*](#parameter-clause) > -> subscript-result {#subscript-result} +> ###### subscript-result {#subscript-result} > > *下标结果* → **->** [*特性列表*](./07_Attributes.md#attributes)可选 [*类型*](./03_Types.md#type) > @@ -1524,24 +1524,24 @@ postfix operator 运算符名称 {} 声明了一个新的运算符以后,需要实现一个跟这个运算符同名的函数来实现这个运算符。如果是实现一个前缀或者后缀运算符,也必须使用相符的 `prefix` 或者 `postfix` 声明修饰符标记函数声明。如果是实现中缀运算符,则不需要使用 `infix` 声明修饰符标记函数声明。关于如何实现一个新的运算符的例子,请参阅 [自定义运算符](../chapter2/26_Advanced_Operators.md#custom_operators)。 -grammer_of_an_operator_declaration {#grammer_of_an_operator_declaration} +###### {#grammer_of_an_operator_declaration} > 运算符声明语法 > -operator-declaration {#operator-declaration} +###### {#operator-declaration} > *运算符声明* → [*前缀运算符声明*](#prefix-operator-declaration) | [*后缀运算符声明*](#postfix-operator-declaration) | [*中缀运算符声明*](#infix-operator-declaration) > -prefix-operator-declaration {#prefix-operator-declaration} +###### {#prefix-operator-declaration} > *前缀运算符声明* → **prefix** **运算符** [*运算符*](./02_Lexical_Structure.md#operator) **{** **}** > -> postfix-operator-declaration {#postfix-operator-declaration} +> ###### postfix-operator-declaration {#postfix-operator-declaration} > > *后缀运算符声明* → **postfix** **运算符** [*运算符*](./02_Lexical_Structure.html#operator) **{** **}** > -> infix-operator-declaration {#infix-operator-declaration} +> ###### infix-operator-declaration {#infix-operator-declaration} > > *中缀运算符声明* → **infix** **运算符** [*运算符*](./02_Lexical_Structure.md#operator) **{** [*中缀运算符属性*](#infix-operator-attributes)可选 **}** > -infix-operator-group {#infix-operator-group} +###### {#infix-operator-group} > *中缀运算符组* → [*优先级组名称*](#precedence-group-name) > @@ -1571,43 +1571,43 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用, 优先级组的赋值性表示在包含可选链操作时的运算符优先级。当设为 true 时,与优先级组对应的运算符在可选链操作中使用和标准库中赋值运算符同样的分组规则,当设为 false 或者不设置,该优先级组的运算符与不赋值的运算符遵循同样的可选链规则。 -grammer_of_a_precedence_group_declaration {#grammer_of_a_precedence_group_declaration} +###### {#grammer_of_a_precedence_group_declaration} > 优先级组声明语法 > -precedence-group-declaration {#precedence-group-declaration} +###### {#precedence-group-declaration} > *优先级组声明* → **precedence**[*优先级组名称*](#precedence-group-name){[*多优先级组属性*](#precedence-group-attributes)可选 } > -precedence-group-attributes {#precedence-group-attributes} +###### {#precedence-group-attributes} > *优先级组属性* → [*优先级组属性*](#precedence-group-attribute)[*多优先级组属性*](#precedence-group-attributes)可选 **{** **}** > -precedence-group-attribute {#precedence-group-attribute} +###### {#precedence-group-attribute} > *优先级组属性* → [*优先级组关系*](#precedence-group-relation) > > *优先级组属性* → [*优先级组赋值性*](#precedence-group-assignment) > > *优先级组属性* → [*优先级组相关性*](#precedence-group-associativity) > -> precedence-group-relation {#precedence-group-relation} +> ###### precedence-group-relation {#precedence-group-relation} > > *优先级组关系* → **higherThan:**[*多优先级组名称*](#precedence-group-names) > > *优先级组关系* → **lowerThan:**[*多优先级组名称*](#precedence-group-names) > -> precedence-group-assignment {#precedence-group-assignment} +> ###### precedence-group-assignment {#precedence-group-assignment} > > *优先级组赋值* → **assignment:**[*布尔字面值*](./02_Lexical_Structure.md#boolean-literal) > -precedence-group-associativity {#precedence-group-associativity} +###### {#precedence-group-associativity} > *优先级组结合性* → **associativity:left** > > *优先级组结合性* → **associativity:right** > > *优先级组结合性* → **associativity:none** > -precedence-group-names {#precedence-group-names} +###### {#precedence-group-names} > *多优先级组名称* → [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) > -precedence-group-name {#precedence-group-name} +###### {#precedence-group-name} > *优先级组名称* →[*标识符*](./02_Lexical_Structure.md#identifier) > @@ -1671,19 +1671,19 @@ Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private` 以上访问级别修饰符都可以选择带上一个参数,该参数由一对圆括号和其中的 `set` 关键字组成(例如,`private(set)`)。使用这种形式的访问级别修饰符来限制某个属性或下标的 setter 的访问级别低于其本身的访问级别,正如 [Getter 和 Setter](../chapter2/25_Access_Control.md#getters_and_setters) 中所讨论的。 -grammer_of_a_declaration_modifier {#grammer_of_a_declaration_modifier} +###### {#grammer_of_a_declaration_modifier} > 声明修饰符的语法 > -declaration-modifier {#declaration-modifier} +###### {#declaration-modifier} > *声明修饰符* → **class** | **convenience**| **dynamic** | **final** | **infix** | **lazy** | **mutating** | **nonmutating** | **optional** | **override** | **postfix** | **prefix** | **required** | **static** | **unowned** | **unowned ( safe )** | **unowned ( unsafe )** | **weak** > > 声明修饰符 → [*访问级别修饰符*](#access-level-modifier) > -> declaration-modifiers {#declaration-modifiers} +> ###### declaration-modifiers {#declaration-modifiers} > > *声明修饰符列表* → [*声明修饰符*](#declaration-modifier) [*声明修饰符列表*](#declaration-modifiers)可选 > -access-level-modifier {#access-level-modifier} +###### {#access-level-modifier} > 访问级别修饰符 → **internal** | **internal ( set )** > > 访问级别修饰符 → **private** | **private ( set )** diff --git a/source/chapter3/07_Attributes.md b/source/chapter3/07_Attributes.md index cabde055..07b03f18 100755 --- a/source/chapter3/07_Attributes.md +++ b/source/chapter3/07_Attributes.md @@ -358,28 +358,28 @@ convention 特性总是与下面的参数之一一起出现。 > 特性语法 > > -> attribute {#attribute} +> ###### attribute {#attribute} > > *特性*→ [特性名](#attribute_name) [特性参数子句](#atribute_argument_clause)可选 > -> attribute_name {#attribute_name} +> ###### attribute_name {#attribute_name} > > *特性名* → [标识符](./02_Lexical_Structure.md#identifier) > -> atribute_argument_clause {#atribute_argument_clause} +> ###### atribute_argument_clause {#atribute_argument_clause} > > *特性参数子句* → **(** [均衡令牌列表](#balanced_tokens)可选 **)** > -> attributes {#attributes} +> ###### attributes {#attributes} > > *特性列表* → [特性](#attribute) [特性列表](#attributes)可选 > > -> balanced_tokens {#balanced_tokens} +> ###### balanced_tokens {#balanced_tokens} > > *均衡令牌列表* → [均衡令牌](#balanced_token) [均衡令牌列表](#balanced_tokens)可选 > -> balanced_token {#balanced_token} +> ###### balanced_token {#balanced_token} > > *均衡令牌* → **(** [均衡令牌列表](#balanced_tokens)可选 **)** > diff --git a/source/chapter3/08_Patterns.md b/source/chapter3/08_Patterns.md index 64e5512a..e44017b1 100755 --- a/source/chapter3/08_Patterns.md +++ b/source/chapter3/08_Patterns.md @@ -10,7 +10,7 @@ Swift 中的模式分为两类:一种能成功匹配任何类型的值,另 > 模式语法 > -pattern {#pattern} +###### {#pattern} > *模式* → [*通配符模式*](#wildcard_pattern) [*类型标注*](03_Types.md#type-annotation)可选 > > *模式* → [*标识符模式*](#identifier_pattern) [*类型标注*](03_Types.md#type-annotation)可选 @@ -40,7 +40,7 @@ for _ in 1...3 { > 通配符模式语法 > -wildcard-pattern {#wildcard-pattern} +###### {#wildcard-pattern} > *通配符模式* → **_** > @@ -57,7 +57,7 @@ let someValue = 42 > 标识符模式语法 > -identifier-pattern {#identifier-pattern} +###### {#identifier-pattern} > *标识符模式* → [*标识符*](./02_Lexical_Structure.md#identifier) > @@ -80,7 +80,7 @@ case let (x, y): > 值绑定模式语法 > -value-binding-pattern {#value-binding-pattern} +###### {#value-binding-pattern} > *值绑定模式* → **var** [*模式*](#pattern) | **let** [*模式*](#pattern) > @@ -109,13 +109,13 @@ let (a): Int = 2 // a: Int = 2 > 元组模式语法 > -tuple-pattern {#tuple-pattern} +###### {#tuple-pattern} > *元组模式* → **(** [*元组模式元素列表*](#tuple-pattern-element-list)可选 **)** > -tuple-pattern-element-list {#tuple-pattern-element-list} +###### {#tuple-pattern-element-list} > *元组模式元素列表* → [*元组模式元素*](#tuple-pattern-element) | [*元组模式元素*](#tuple-pattern-element) **,** [*元组模式元素列表*](#tuple-pattern-element-list) > -tuple-pattern-element {#tuple-pattern-element} +###### {#tuple-pattern-element} > *元组模式元素* → [*模式*](#pattern) > @@ -126,7 +126,7 @@ tuple-pattern-element {#tuple-pattern-element} > 枚举用例模式语法 > -enum-case-pattern {#enum-case-pattern} +###### {#enum-case-pattern} > *枚举用例模式* → [*类型标识*](./03_Types.md#type-identifier)可选 **.** [*枚举用例名*](./06_Declarations.md#enum-case-name) [*元组模式*](#tuple-pattern)可选 > @@ -163,7 +163,7 @@ for case let number? in arrayOfOptinalInts { > 可选模式语法 > -optional-pattern {#optional-pattern} +###### {#optional-pattern} > *可选模式* → [*标识符模式*](./03_Types.md#type-identifier) **?** > @@ -183,13 +183,13 @@ optional-pattern {#optional-pattern} > 类型转换模式语法 > -type-casting-pattern {#type-casting-pattern} +###### {#type-casting-pattern} > *类型转换模式* → [*is 模式*](#is-pattern) | [*as 模式*](#as-pattern) > -is-pattern {#is-pattern} +###### {#is-pattern} > *is 模式* → **is** [*类型*](./03_Types.md#type) > -as-pattern {#as-pattern} +###### {#as-pattern} > *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.md#type) > @@ -231,6 +231,6 @@ default: > 表达式模式语法 > -expression-pattern {#expression-pattern} +###### {#expression-pattern} > *表达式模式* → [*表达式*](./04_Expressions.md#expression) > diff --git a/source/chapter3/09_Generic_Parameters_and_Arguments.md b/source/chapter3/09_Generic_Parameters_and_Arguments.md index 374157ce..70a72307 100755 --- a/source/chapter3/09_Generic_Parameters_and_Arguments.md +++ b/source/chapter3/09_Generic_Parameters_and_Arguments.md @@ -56,36 +56,36 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型 > 泛型形参子句语法 > -generic-parameter-clause {#generic-parameter-clause} +###### {#generic-parameter-clause} > *泛型形参子句* → **<** [*泛型形参列表*](#generic-parameter-list) [*约束子句*](#requirement-clause)可选 **>** > -generic-parameter-list {#generic-parameter-list} +###### {#generic-parameter-list} > *泛型形参列表* → [*泛形形参*](#generic-parameter) | [*泛形形参*](#generic-parameter) **,** [*泛型形参列表*](#generic-parameter-list) > -generic-parameter {#generic-parameter} +###### {#generic-parameter} > *泛形形参* → [*类型名称*](./03_Types.md#type-name) > > *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*类型标识符*](./03_Types.md#type-identifier) > > *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*协议合成类型*](./03_Types.md#protocol-composition-type) > -> requirement-clause {#requirement-clause} +> ###### requirement-clause {#requirement-clause} > > *约束子句* → **where** [*约束列表*](#requirement-list) > -requirement-list {#requirement-list} +###### {#requirement-list} > *约束列表* → [*约束*](#requirement) | [*约束*](#requirement) **,** [*约束列表*](#requirement-list) > -requirement {#requirement} +###### {#requirement} > *约束* → [*一致性约束*](#conformance-requirement) | [*同类型约束*](#same-type-requirement) > -> conformance-requirement {#conformance-requirement} +> ###### conformance-requirement {#conformance-requirement} > > *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*类型标识符*](./03_Types.md#type-identifier) > > *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*协议合成类型*](./03_Types.md#protocol-composition-type) > -same-type-requirement {#same-type-requirement} +###### {#same-type-requirement} > *同类型约束* → [*类型标识符*](./03_Types.md#type-identifier) **==** [*类型*](./03_Types.md#type) > @@ -116,12 +116,12 @@ let arrayOfArrays: Array> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] > 泛型实参子句语法 > -generic-argument-clause {#generic-argument-clause} +###### {#generic-argument-clause} > *泛型实参子句* → **<** [*泛型实参列表*](#generic-argument-list) **>** > -generic-argument-list {#generic-argument-list} +###### {#generic-argument-list} > *泛型实参列表* → [*泛型实参*](#generic-argument) | [*泛型实参*](#generic-argument) **,** [*泛型实参列表*](#generic-argument-list) > -generic-argument {#generic-argument} +###### {#generic-argument} > *泛型实参* → [*类型*](./03_Types.md#type) >