partly fix chapter 3 relative links
This commit is contained in:
@ -20,14 +20,14 @@ 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.md#getter-clause) [*setter 子句*](./05_Declarations.md#setter-clause)<sub>可选</sub> } | { [*setter 子句*](./05_Declarations.md#setter-clause) [*getter 子句*](./05_Declarations.md#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.md#getter-clause) [*setter 子句*](./05_Declarations.md#setter-clause)<sub>可选</sub> }
|
||||||
>
|
>
|
||||||
> getter-setter 方法块 → { [*setter 子句*](05_Declarations.html#setter-clause) [*getter 子句*](05_Declarations.html#getter-clause) }
|
> getter-setter 方法块 → { [*setter 子句*](./05_Declarations.md#setter-clause) [*getter 子句*](./05_Declarations.md#getter-clause) }
|
||||||
>
|
>
|
||||||
|
|||||||
@ -52,7 +52,7 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
|||||||
> *多行注释内容项* → 任何 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.md#//apple_ref/doc/uid/TP40016497) 所述。
|
||||||
|
|
||||||
<a id="identifiers"></a>
|
<a id="identifiers"></a>
|
||||||
## 标识符
|
## 标识符
|
||||||
@ -188,7 +188,7 @@ true // 布尔值字面量
|
|||||||
|
|
||||||
整型字面面可以使用下划线(`_`)来增加数字的可读性,下划线会被系统忽略,因此不会影响字面量的值。同样地,也可以在数字前加 `0`,这同样也会被系统所忽略,并不会影响字面量的值。
|
整型字面面可以使用下划线(`_`)来增加数字的可读性,下划线会被系统忽略,因此不会影响字面量的值。同样地,也可以在数字前加 `0`,这同样也会被系统所忽略,并不会影响字面量的值。
|
||||||
|
|
||||||
除非特别指定,整数字面量的默认推导类型为 Swift 标准库类型中的 `Int`。Swift 标准库还定义了其他不同长度以及是否带符号的整数类型,请参考 [整数](../chapter2/01_The_Basics.html#integers)。
|
除非特别指定,整数字面量的默认推导类型为 Swift 标准库类型中的 `Int`。Swift 标准库还定义了其他不同长度以及是否带符号的整数类型,请参考 [整数](../chapter2/01_The_Basics.md#integers)。
|
||||||
|
|
||||||
> 整数字面量语法
|
> 整数字面量语法
|
||||||
>
|
>
|
||||||
@ -374,7 +374,7 @@ true // 布尔值字面量
|
|||||||
let x = 3; "1 2 \(x)"
|
let x = 3; "1 2 \(x)"
|
||||||
```
|
```
|
||||||
|
|
||||||
字符串字面量的默认推导类型为 `String`。更多有关 `String` 类型的信息请参考 [字符串和字符](../chapter2/03_Strings_and_Characters.html) 以及 [*字符串结构参考*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_String_Structure/index.html#//apple_ref/doc/uid/TP40015181)。
|
字符串字面量的默认推导类型为 `String`。更多有关 `String` 类型的信息请参考 [字符串和字符](../chapter2/03_Strings_and_Characters.md) 以及 [*字符串结构参考*](./https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_String_Structure/index.md#//apple_ref/doc/uid/TP40015181)。
|
||||||
|
|
||||||
用 `+` 操作符连接的字符型字面量是在编译时进行连接的。比如下面的 `textA` 和 `textB` 是完全一样的,`textA` 没有任何运行时的连接操作。
|
用 `+` 操作符连接的字符型字面量是在编译时进行连接的。比如下面的 `textA` 和 `textB` 是完全一样的,`textA` 没有任何运行时的连接操作。
|
||||||
|
|
||||||
@ -428,7 +428,7 @@ let textB = "Hello world"
|
|||||||
<a id="operators"></a>
|
<a id="operators"></a>
|
||||||
## 运算符
|
## 运算符
|
||||||
|
|
||||||
Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基础运算符](../chapter2/02_Basic_Operators.html) 和 [高级运算符](../chapter2/25_Advanced_Operators.html) 中进行了阐述。这一小节将描述哪些字符能用于自定义运算符。
|
Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基础运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/25_Advanced_Operators.md) 中进行了阐述。这一小节将描述哪些字符能用于自定义运算符。
|
||||||
|
|
||||||
自定义运算符可以由以下其中之一的 ASCII 字符 `/`、`=`、`-`、`+`、`!`、`*`、`%`、`<`、`>`、`&`、`|`、`^`、`?` 以及 `~`,或者后面语法中规定的任一个 Unicode 字符(其中包含了*数学运算符*、*零散符号(Miscellaneous Symbols)* 以及印刷符号(Dingbats)之类的 Unicode 块)开始。在第一个字符之后,允许使用组合型 Unicode 字符。
|
自定义运算符可以由以下其中之一的 ASCII 字符 `/`、`=`、`-`、`+`、`!`、`*`、`%`、`<`、`>`、`&`、`|`、`^`、`?` 以及 `~`,或者后面语法中规定的任一个 Unicode 字符(其中包含了*数学运算符*、*零散符号(Miscellaneous Symbols)* 以及印刷符号(Dingbats)之类的 Unicode 块)开始。在第一个字符之后,允许使用组合型 Unicode 字符。
|
||||||
|
|
||||||
@ -454,7 +454,7 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
|
|||||||
|
|
||||||
在某些特定的设计中 ,以 `<` 或 `>` 开头的运算符会被分离成两个或多个符号,剩余部分可能会以同样的方式被再次分离。因此,在 `Dictionary<String, Array<Int>>` 中没有必要添加空白来消除闭合字符 `>` 的歧义。在这个例子中, 闭合字符 `>` 不会被视为单独的符号,因而不会被错误解析为 `>>` 运算符。
|
在某些特定的设计中 ,以 `<` 或 `>` 开头的运算符会被分离成两个或多个符号,剩余部分可能会以同样的方式被再次分离。因此,在 `Dictionary<String, Array<Int>>` 中没有必要添加空白来消除闭合字符 `>` 的歧义。在这个例子中, 闭合字符 `>` 不会被视为单独的符号,因而不会被错误解析为 `>>` 运算符。
|
||||||
|
|
||||||
要学习如何自定义运算符,请参考 [自定义运算符](../chapter2/25_Advanced_Operators.html#custom_operators) 和 [运算符声明](05_Declarations.html#operator_declaration)。要学习如何重载运算符,请参考 [运算符函数](../chapter2/25_Advanced_Operators.html#operator_functions)。
|
要学习如何自定义运算符,请参考 [自定义运算符](../chapter2/25_Advanced_Operators.md#custom_operators) 和 [运算符声明](./05_Declarations.md#operator_declaration)。要学习如何重载运算符,请参考 [运算符函数](../chapter2/25_Advanced_Operators.md#operator_functions)。
|
||||||
|
|
||||||
> 运算符语法
|
> 运算符语法
|
||||||
>
|
>
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
Swift 语言存在两种类型:命名型类型和复合型类型。命名型类型是指定义时可以给定名字的类型。命名型类型包括类、结构体、枚举和协议。比如,一个用户定义的类 `MyClass` 的实例拥有类型 `MyClass`。除了用户定义的命名型类型,Swift 标准库也定义了很多常用的命名型类型,包括那些表示数组、字典和可选值的类型。
|
Swift 语言存在两种类型:命名型类型和复合型类型。命名型类型是指定义时可以给定名字的类型。命名型类型包括类、结构体、枚举和协议。比如,一个用户定义的类 `MyClass` 的实例拥有类型 `MyClass`。除了用户定义的命名型类型,Swift 标准库也定义了很多常用的命名型类型,包括那些表示数组、字典和可选值的类型。
|
||||||
|
|
||||||
那些通常被其它语言认为是基本或原始的数据型类型,比如表示数字、字符和字符串的类型,实际上就是命名型类型,这些类型在 Swift 标准库中是使用结构体来定义和实现的。因为它们是命名型类型,因此你可以按照 [扩展](../chapter2/21_Extensions.html) 和 [扩展声明](05_Declarations.html#extension_declaration) 中讨论的那样,声明一个扩展来增加它们的行为以满足你程序的需求。
|
那些通常被其它语言认为是基本或原始的数据型类型,比如表示数字、字符和字符串的类型,实际上就是命名型类型,这些类型在 Swift 标准库中是使用结构体来定义和实现的。因为它们是命名型类型,因此你可以按照 [扩展](../chapter2/21_Extensions.md) 和 [扩展声明](./05_Declarations.md#extension_declaration) 中讨论的那样,声明一个扩展来增加它们的行为以满足你程序的需求。
|
||||||
|
|
||||||
复合型类型是没有名字的类型,它由 Swift 本身定义。Swift 存在两种复合型类型:函数类型和元组类型。一个复合型类型可以包含命名型类型和其它复合型类型。例如,元组类型 `(Int, (Int, Int))` 包含两个元素:第一个是命名型类型 `Int`,第二个是另一个复合型类型 `(Int, Int)`。
|
复合型类型是没有名字的类型,它由 Swift 本身定义。Swift 存在两种复合型类型:函数类型和元组类型。一个复合型类型可以包含命名型类型和其它复合型类型。例如,元组类型 `(Int, (Int, Int))` 包含两个元素:第一个是命名型类型 `Int`,第二个是另一个复合型类型 `(Int, Int)`。
|
||||||
|
|
||||||
@ -70,7 +70,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.md#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="type_identifier"></a>
|
<a name="type_identifier"></a>
|
||||||
@ -96,10 +96,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.md#generic_argument_clause)<sub>可选</sub> | [*类型名称*](./#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.md#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.md#identifier)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="tuple_type"></a>
|
<a name="tuple_type"></a>
|
||||||
@ -107,7 +107,7 @@ var someValue: ExampleModule.MyType
|
|||||||
|
|
||||||
元组类型是使用括号括起来的零个或多个类型,类型间用逗号隔开。
|
元组类型是使用括号括起来的零个或多个类型,类型间用逗号隔开。
|
||||||
|
|
||||||
你可以使用元组类型作为一个函数的返回类型,这样就可以使函数返回多个值。你也可以命名元组类型中的元素,然后用这些名字来引用每个元素的值。元素的名字由一个标识符紧跟一个冒号 `(:)` 组成。[函数和多返回值](../chapter2/06_Functions.html#functions_with_multiple_return_values) 章节里有一个展示上述特性的例子。
|
你可以使用元组类型作为一个函数的返回类型,这样就可以使函数返回多个值。你也可以命名元组类型中的元素,然后用这些名字来引用每个元素的值。元素的名字由一个标识符紧跟一个冒号 `(:)` 组成。[函数和多返回值](../chapter2/06_Functions.md#functions_with_multiple_return_values) 章节里有一个展示上述特性的例子。
|
||||||
|
|
||||||
当一个元组类型的元素有名字的时候,这个名字就是类型的一部分。
|
当一个元组类型的元素有名字的时候,这个名字就是类型的一部分。
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配
|
|||||||
> *元组类型元素* → [*元素名*](#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.md#identifier)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="function_type"></a>
|
<a name="function_type"></a>
|
||||||
@ -145,12 +145,12 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配
|
|||||||
|
|
||||||
参数类型是由逗号间隔的类型列表。由于参数类型和返回值类型可以是元组类型,所以函数类型支持多参数与多返回值的函数与方法。
|
参数类型是由逗号间隔的类型列表。由于参数类型和返回值类型可以是元组类型,所以函数类型支持多参数与多返回值的函数与方法。
|
||||||
|
|
||||||
你可以对函数参数 `() -> T`(其中 T 是任何类型)使用 `autoclosure` 特性。这会自动将参数表达式转化为闭包,表达式的结果即闭包返回值。这从语法结构上提供了一种便捷:延迟对表达式的求值,直到其值在函数体中被调用。以自动闭包做为参数的函数类型的例子详见 [自动闭包](../chapter2/07_Closures.html#autoclosures) 。
|
你可以对函数参数 `() -> T`(其中 T 是任何类型)使用 `autoclosure` 特性。这会自动将参数表达式转化为闭包,表达式的结果即闭包返回值。这从语法结构上提供了一种便捷:延迟对表达式的求值,直到其值在函数体中被调用。以自动闭包做为参数的函数类型的例子详见 [自动闭包](../chapter2/07_Closures.md#autoclosures) 。
|
||||||
>
|
>
|
||||||
|
|
||||||
函数类型可以拥有一个可变长参数作为参数类型中的最后一个参数。从语法角度上讲,可变长参数由一个基础类型名字紧随三个点(`...`)组成,如 `Int...`。可变长参数被认为是一个包含了基础类型元素的数组。即 `Int...` 就是 `[Int]`。关于使用可变长参数的例子,请参阅 [可变参数](../chapter2/06_Functions.html#variadic_parameters)。
|
函数类型可以拥有一个可变长参数作为参数类型中的最后一个参数。从语法角度上讲,可变长参数由一个基础类型名字紧随三个点(`...`)组成,如 `Int...`。可变长参数被认为是一个包含了基础类型元素的数组。即 `Int...` 就是 `[Int]`。关于使用可变长参数的例子,请参阅 [可变参数](../chapter2/06_Functions.md#variadic_parameters)。
|
||||||
|
|
||||||
为了指定一个 `in-out` 参数,可以在参数类型前加 `inout` 前缀。但是你不可以对可变长参数或返回值类型使用 `inout`。关于这种参数的详细讲解请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。
|
为了指定一个 `in-out` 参数,可以在参数类型前加 `inout` 前缀。但是你不可以对可变长参数或返回值类型使用 `inout`。关于这种参数的详细讲解请参阅 [输入输出参数](../chapter2/06_Functions.md#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` 与一个空元组的变量的函数类型 `(()) -> ()` 是一样的。但这些类型和无变量的函数类型 `() -> ()` 是不一样的。
|
||||||
>
|
>
|
||||||
@ -188,7 +188,7 @@ 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.md#throwing_functions_and_methods) 和 [重抛函数与方法](./05_Declarations.md#rethrowing_functions_and_methods)。
|
||||||
|
|
||||||
<a name="Restrictions for Nonescaping Closures"></a>
|
<a name="Restrictions for Nonescaping Closures"></a>
|
||||||
### 对非逃逸闭包的限制
|
### 对非逃逸闭包的限制
|
||||||
@ -214,15 +214,15 @@ func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) {
|
|||||||
|
|
||||||
上述例子里的被标记为“错误”的四个函数调用会产生编译错误。因为第一个和第二个参数是非逃逸函数,它们不能够被当作变量被传递到另一个非闭包函数参数。与此相反, 标记“正确”的两个函数不回产生编译错误。这些函数调用不会违反限制, 因为 `外部(external)` 不是 `takesTwoFunctions(first:second:)` 里的一个参数。
|
上述例子里的被标记为“错误”的四个函数调用会产生编译错误。因为第一个和第二个参数是非逃逸函数,它们不能够被当作变量被传递到另一个非闭包函数参数。与此相反, 标记“正确”的两个函数不回产生编译错误。这些函数调用不会违反限制, 因为 `外部(external)` 不是 `takesTwoFunctions(first:second:)` 里的一个参数。
|
||||||
|
|
||||||
如果你需要避免这个限制, 标记其中之一的参数为逃逸, 或者使用 `withoutActuallyEscaping(_:do:)` 函数临时地转换非逃逸函数的其中一个参数为逃逸函数。关于避免内存访问冲突,可以参阅[内存安全](../chapter2/24_Memory_Safety.html)。
|
如果你需要避免这个限制, 标记其中之一的参数为逃逸, 或者使用 `withoutActuallyEscaping(_:do:)` 函数临时地转换非逃逸函数的其中一个参数为逃逸函数。关于避免内存访问冲突,可以参阅[内存安全](../chapter2/24_Memory_Safety.md)。
|
||||||
|
|
||||||
|
|
||||||
> 函数类型语法
|
> 函数类型语法
|
||||||
>
|
>
|
||||||
<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.md#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.md#attributes)<sub>可选</sub> [*函数类型子句*](#function-type-argument-clause) **rethrows** **->** [*类型*](#type)
|
||||||
>
|
>
|
||||||
<a name="function-type-argument-clause"></a>
|
<a name="function-type-argument-clause"></a>
|
||||||
> *函数类型子句* → ()
|
> *函数类型子句* → ()
|
||||||
@ -233,10 +233,10 @@ func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) {
|
|||||||
> *函数类型参数列表* → [*函数类型参数*](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.md#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.md#identifier)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="array_type"></a>
|
<a name="array_type"></a>
|
||||||
@ -265,7 +265,7 @@ var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
|
|||||||
|
|
||||||
访问一个多维数组的元素时,最左边的下标指向最外层数组的相应位置元素。接下来往右的下标指向第一层嵌入的相应位置元素,依次类推。这就意味着,在上面的例子中,`array3D[0]` 是 `[[1, 2], [3, 4]]`,`array3D[0][1]` 是 `[3, 4]`,`array3D[0][1][1]` 则是 `4`。
|
访问一个多维数组的元素时,最左边的下标指向最外层数组的相应位置元素。接下来往右的下标指向第一层嵌入的相应位置元素,依次类推。这就意味着,在上面的例子中,`array3D[0]` 是 `[[1, 2], [3, 4]]`,`array3D[0][1]` 是 `[3, 4]`,`array3D[0][1][1]` 则是 `4`。
|
||||||
|
|
||||||
关于 Swift 标准库中 `Array` 类型的详细讨论,请参阅 [数组](../chapter2/04_Collection_Types.html#arrays)。
|
关于 Swift 标准库中 `Array` 类型的详细讨论,请参阅 [数组](../chapter2/04_Collection_Types.md#arrays)。
|
||||||
|
|
||||||
> 数组类型语法
|
> 数组类型语法
|
||||||
>
|
>
|
||||||
@ -295,7 +295,7 @@ let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39]
|
|||||||
|
|
||||||
字典中键的类型必须符合 Swift 标准库中的 `Hashable` 协议。
|
字典中键的类型必须符合 Swift 标准库中的 `Hashable` 协议。
|
||||||
|
|
||||||
关于 Swift 标准库中 `Dictionary` 类型的详细讨论,请参阅 [字典](../chapter2/04_Collection_Types.html#dictionaries)。
|
关于 Swift 标准库中 `Dictionary` 类型的详细讨论,请参阅 [字典](../chapter2/04_Collection_Types.md#dictionaries)。
|
||||||
|
|
||||||
> 字典类型语法
|
> 字典类型语法
|
||||||
>
|
>
|
||||||
@ -328,7 +328,7 @@ optionalInteger! // 42
|
|||||||
|
|
||||||
你也可以使用可选链式调用和可选绑定来选择性地在可选表达式上执行操作。如果值为 `nil`,不会执行任何操作,因此也就没有运行错误产生。
|
你也可以使用可选链式调用和可选绑定来选择性地在可选表达式上执行操作。如果值为 `nil`,不会执行任何操作,因此也就没有运行错误产生。
|
||||||
|
|
||||||
更多细节以及更多如何使用可选类型的例子,请参阅 [可选类型](../chapter2/01_The_Basics.html#optionals)。
|
更多细节以及更多如何使用可选类型的例子,请参阅 [可选类型](../chapter2/01_The_Basics.md#optionals)。
|
||||||
|
|
||||||
> 可选类型语法
|
> 可选类型语法
|
||||||
>
|
>
|
||||||
@ -364,7 +364,7 @@ let implicitlyUnwrappedArray: [Int]! // 正确
|
|||||||
|
|
||||||
可以使用可选链式调用来在隐式解析可选表达式上选择性地执行操作。如果值为 `nil`,就不会执行任何操作,因此也不会产生运行错误。
|
可以使用可选链式调用来在隐式解析可选表达式上选择性地执行操作。如果值为 `nil`,就不会执行任何操作,因此也不会产生运行错误。
|
||||||
|
|
||||||
关于隐式解析可选类型的更多细节,请参阅 [隐式解析可选类型](../chapter2/01_The_Basics.html#implicityly_unwrapped_optionals)。
|
关于隐式解析可选类型的更多细节,请参阅 [隐式解析可选类型](../chapter2/01_The_Basics.md#implicityly_unwrapped_optionals)。
|
||||||
|
|
||||||
> 隐式解析可选类型语法
|
> 隐式解析可选类型语法
|
||||||
>
|
>
|
||||||
@ -454,11 +454,11 @@ let anotherInstance = metatype.init(string: "some string")
|
|||||||
|
|
||||||
类型继承子句被用来指定一个命名型类型继承自哪个类、采纳哪些协议。类型继承子句也用来指定一个类类型专属协议。类型继承子句开始于冒号 `:`,其后是所需要的类、类型标识符列表或两者都有。
|
类型继承子句被用来指定一个命名型类型继承自哪个类、采纳哪些协议。类型继承子句也用来指定一个类类型专属协议。类型继承子句开始于冒号 `:`,其后是所需要的类、类型标识符列表或两者都有。
|
||||||
|
|
||||||
类可以继承单个超类,采纳任意数量的协议。当定义一个类时,超类的名字必须出现在类型标识符列表首位,然后跟上该类需要采纳的任意数量的协议。如果一个类不是从其它类继承而来,那么列表可以以协议开头。关于类继承更多的讨论和例子,请参阅 [继承](../chapter2/13_Inheritance.html)。
|
类可以继承单个超类,采纳任意数量的协议。当定义一个类时,超类的名字必须出现在类型标识符列表首位,然后跟上该类需要采纳的任意数量的协议。如果一个类不是从其它类继承而来,那么列表可以以协议开头。关于类继承更多的讨论和例子,请参阅 [继承](../chapter2/13_Inheritance.md)。
|
||||||
|
|
||||||
其它命名型类型可能只继承或采纳一系列协议。协议类型可以继承自任意数量的其他协议。当一个协议类型继承自其它协议时,其它协议中定义的要求会被整合在一起,然后从当前协议继承的任意类型必须符合所有这些条件。正如在 [协议声明](05_Declarations.html#protocol_declaration) 中所讨论的那样,可以把 `class` 关键字放到协议类型的类型继承子句的首位,这样就可以声明一个类类型专属协议。
|
其它命名型类型可能只继承或采纳一系列协议。协议类型可以继承自任意数量的其他协议。当一个协议类型继承自其它协议时,其它协议中定义的要求会被整合在一起,然后从当前协议继承的任意类型必须符合所有这些条件。正如在 [协议声明](./05_Declarations.md#protocol_declaration) 中所讨论的那样,可以把 `class` 关键字放到协议类型的类型继承子句的首位,这样就可以声明一个类类型专属协议。
|
||||||
|
|
||||||
枚举定义中的类型继承子句可以是一系列协议,或是枚举的原始值类型的命名型类型。在枚举定义中使用类型继承子句来指定原始值类型的例子,请参阅 [原始值](../chapter2/08_Enumerations.html#raw_values)。
|
枚举定义中的类型继承子句可以是一系列协议,或是枚举的原始值类型的命名型类型。在枚举定义中使用类型继承子句来指定原始值类型的例子,请参阅 [原始值](../chapter2/08_Enumerations.md#raw_values)。
|
||||||
|
|
||||||
> 类型继承子句语法
|
> 类型继承子句语法
|
||||||
>
|
>
|
||||||
|
|||||||
@ -20,19 +20,19 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
|||||||
|
|
||||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/25_Advanced_Operators.md)。
|
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/25_Advanced_Operators.md)。
|
||||||
|
|
||||||
关于 Swift 标准库提供的运算符的更多信息,请参阅 [*Swift Standard Library Operators Reference*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_StandardLibrary_Operators/index.html#//apple_ref/doc/uid/TP40016054)。
|
关于 Swift 标准库提供的运算符的更多信息,请参阅 [*Swift Standard Library Operators Reference*](./https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_StandardLibrary_Operators/index.md#//apple_ref/doc/uid/TP40016054)。
|
||||||
|
|
||||||
除了标准库运算符,你也可以对某个变量使用 `&` 运算符,从而将其传递给函数的输入输出参数。更多信息,请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。
|
除了标准库运算符,你也可以对某个变量使用 `&` 运算符,从而将其传递给函数的输入输出参数。更多信息,请参阅 [输入输出参数](../chapter2/06_Functions.md#in_out_parameters)。
|
||||||
|
|
||||||
> 前缀表达式语法
|
> 前缀表达式语法
|
||||||
>
|
>
|
||||||
<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>
|
||||||
@ -67,7 +67,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
|||||||
|
|
||||||
`try` 表达式不能出现在二进制运算符的的右侧,除非二进制运算符是赋值运算符或者 `try` 表达式是被圆括号括起来的。
|
`try` 表达式不能出现在二进制运算符的的右侧,除非二进制运算符是赋值运算符或者 `try` 表达式是被圆括号括起来的。
|
||||||
|
|
||||||
关于 `try`、`try?` 和 `try!` 的更多信息,以及该如何使用的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html)。
|
关于 `try`、`try?` 和 `try!` 的更多信息,以及该如何使用的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.md)。
|
||||||
> Try 表达式语法
|
> Try 表达式语法
|
||||||
>
|
>
|
||||||
<a name="try-operator"></a>
|
<a name="try-operator"></a>
|
||||||
@ -82,9 +82,9 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
|||||||
> `左侧参数` `二元运算符` `右侧参数`
|
> `左侧参数` `二元运算符` `右侧参数`
|
||||||
>
|
>
|
||||||
|
|
||||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.html) 和 [高级运算符](../chapter2/26_Advanced_Operators.html)。
|
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/26_Advanced_Operators.md)。
|
||||||
|
|
||||||
关于 Swift 标准库提供的运算符的更多信息,请参阅 [*Swift Standard Library Operators Reference*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_StandardLibrary_Operators/index.html#//apple_ref/doc/uid/TP40016054)。
|
关于 Swift 标准库提供的运算符的更多信息,请参阅 [*Swift Standard Library Operators Reference*](./https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_StandardLibrary_Operators/index.md#//apple_ref/doc/uid/TP40016054)。
|
||||||
|
|
||||||
> 注意
|
> 注意
|
||||||
>
|
>
|
||||||
@ -94,7 +94,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
|||||||
<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)
|
||||||
>
|
>
|
||||||
@ -180,7 +180,7 @@ f(x as Any)
|
|||||||
// 打印“Function for Any”
|
// 打印“Function for Any”
|
||||||
```
|
```
|
||||||
|
|
||||||
桥接可将 Swift 标准库中的类型(例如 `String`)作为一个与之相关的 Foundation 类型(例如 `NSString`)来使用,而不需要新建一个实例。关于桥接的更多信息,请参阅 [*Using Swift with Cocoa and Objective-C (Swift4.1)*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216) 中的 [Working with Cocoa Data Types](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6)。
|
桥接可将 Swift 标准库中的类型(例如 `String`)作为一个与之相关的 Foundation 类型(例如 `NSString`)来使用,而不需要新建一个实例。关于桥接的更多信息,请参阅 [*Using Swift with Cocoa and Objective-C (Swift4.1)*](./https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.md#//apple_ref/doc/uid/TP40014216) 中的 [Working with Cocoa Data Types](./https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.md#//apple_ref/doc/uid/TP40014216-CH6)。
|
||||||
|
|
||||||
`as?` 运算符有条件地执行类型转换,返回目标类型的可选值。在运行时,如果转换成功,返回的可选值将包含转换后的值,否则返回 `nil`。如果在编译时就能确定转换一定会成功或是失败,则会导致编译报错。
|
`as?` 运算符有条件地执行类型转换,返回目标类型的可选值。在运行时,如果转换成功,返回的可选值将包含转换后的值,否则返回 `nil`。如果在编译时就能确定转换一定会成功或是失败,则会导致编译报错。
|
||||||
|
|
||||||
@ -191,13 +191,13 @@ f(x as Any)
|
|||||||
<a name="type-casting-operator"></a>
|
<a name="type-casting-operator"></a>
|
||||||
> 类型转换运算符语法
|
> 类型转换运算符语法
|
||||||
>
|
>
|
||||||
> *类型转换运算符* → **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>
|
||||||
@ -208,7 +208,7 @@ 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)
|
||||||
>
|
>
|
||||||
@ -287,7 +287,7 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者
|
|||||||
>
|
>
|
||||||
> <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)
|
||||||
>
|
>
|
||||||
@ -375,7 +375,7 @@ struct Point {
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
<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) **]**
|
||||||
@ -406,7 +406,7 @@ struct Point {
|
|||||||
> *父类表达式* → [*父类方法表达式*](#superclass-method-expression) | [*父类下标表达式*](#superclass-subscript-expression) | [*父类构造器表达式*](#superclass-initializer-expression)
|
> *父类表达式* → [*父类方法表达式*](#superclass-method-expression) | [*父类下标表达式*](#superclass-subscript-expression) | [*父类构造器表达式*](#superclass-initializer-expression)
|
||||||
>
|
>
|
||||||
<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) **]**
|
||||||
@ -427,7 +427,7 @@ struct Point {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
闭包的参数声明形式跟函数一样,请参阅 [函数声明](05_Declarations.html#function_declaration)。
|
闭包的参数声明形式跟函数一样,请参阅 [函数声明](./05_Declarations.md#function_declaration)。
|
||||||
|
|
||||||
闭包还有几种特殊的形式,能让闭包使用起来更加简洁:
|
闭包还有几种特殊的形式,能让闭包使用起来更加简洁:
|
||||||
|
|
||||||
@ -519,7 +519,7 @@ myFunction { [unowned self] in print(self.title) } // 无主引用捕获
|
|||||||
myFunction { [weak parent = self.parent] in print(parent!.title) }
|
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.md#closure_expressions)。关于捕获列表的更多信息和例子,请参阅 [解决闭包引起的循环强引用](../chapter2/23_Automatic_Reference_Counting.md#resolving_strong_reference_cycles_for_closures)。
|
||||||
|
|
||||||
> 闭包表达式语法
|
> 闭包表达式语法
|
||||||
>
|
>
|
||||||
@ -532,13 +532,13 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
|
|||||||
> <a name="closure-signature"></a>
|
> <a name="closure-signature"></a>
|
||||||
>
|
>
|
||||||
>
|
>
|
||||||
> 闭包签名* → [*参数子句*](#parameter-clause) [*函数结果*](05_Declarations.html#function-result)<sub>可选</sub> **in**
|
> 闭包签名* → [*参数子句*](./#parameter-clause) [*函数结果*](05_Declarations.md#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**
|
||||||
>
|
>
|
||||||
@ -579,7 +579,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>
|
||||||
@ -769,14 +769,14 @@ print(interestingNumbers[keyPath: \[String: [Int]].["hexagonal"]!.count.bitWidth
|
|||||||
// 打印 "64"
|
// 打印 "64"
|
||||||
```
|
```
|
||||||
|
|
||||||
关于更多如何使用 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.md#//apple_ref/doc/uid/10000107i) 和 [Key-Value 观察编程](./https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.md#//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>
|
||||||
>
|
>
|
||||||
@ -784,7 +784,7 @@ print(interestingNumbers[keyPath: \[String: [Int]].["hexagonal"]!.count.bitWidth
|
|||||||
>
|
>
|
||||||
> <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.md#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>
|
||||||
>
|
>
|
||||||
@ -897,7 +897,7 @@ print(keyPath == c.getSomeKeyPath())
|
|||||||
|
|
||||||
由于 key-path 字符串表达式在编译期才创建,编译期可以检查属性是否存在,以及属性是否暴露给 Objective-C 运行时。
|
由于 key-path 字符串表达式在编译期才创建,编译期可以检查属性是否存在,以及属性是否暴露给 Objective-C 运行时。
|
||||||
|
|
||||||
关于更多如何使用 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.md#//apple_ref/doc/uid/10000107i) 和 [Key-Value 观察编程](./https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.md#//apple_ref/doc/uid/10000177i)
|
||||||
|
|
||||||
> 注意
|
> 注意
|
||||||
>
|
>
|
||||||
@ -918,7 +918,7 @@ print(keyPath == c.getSomeKeyPath())
|
|||||||
|
|
||||||
*后缀表达式*就是在某个表达式的后面运用后缀运算符或其他后缀语法。从语法构成上来看,基本表达式也是后缀表达式。
|
*后缀表达式*就是在某个表达式的后面运用后缀运算符或其他后缀语法。从语法构成上来看,基本表达式也是后缀表达式。
|
||||||
|
|
||||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.html) 和 [高级运算符](../chapter2/26_Advanced_Operators.html)。
|
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/26_Advanced_Operators.md)。
|
||||||
|
|
||||||
关于 Swift 标准库提供的运算符的更多信息,请参阅 [运算符定义](https://developer.apple.com/documentation/swift/operator_declarations)。
|
关于 Swift 标准库提供的运算符的更多信息,请参阅 [运算符定义](https://developer.apple.com/documentation/swift/operator_declarations)。
|
||||||
|
|
||||||
@ -927,7 +927,7 @@ print(keyPath == c.getSomeKeyPath())
|
|||||||
<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)
|
||||||
>
|
>
|
||||||
@ -996,9 +996,9 @@ myData.someMethod {$0 == 13}
|
|||||||
>
|
>
|
||||||
> <a name="function-call-argument"></a>
|
> <a name="function-call-argument"></a>
|
||||||
>
|
>
|
||||||
> *函数调用参数* → [表达式](#expression) | [标识符](02_Lexical_Structure.html#identifier) **:** [*表达式*](#expression)
|
> *函数调用参数* → [表达式](./#expression) | [标识符](02_Lexical_Structure.md#identifier) **:** [*表达式*](#expression)
|
||||||
>
|
>
|
||||||
> *函数调用参数* → [运算符](02_Lexical_Structure.html#operator) | [标识符](02_Lexical_Structure.html#identifier) **:** [*运算符*](02_Lexical_Structure.html#operator)
|
> *函数调用参数* → [运算符](./02_Lexical_Structure.md#operator) | [标识符](./02_Lexical_Structure.md#identifier) **:** [*运算符*](./02_Lexical_Structure.md#operator)
|
||||||
>
|
>
|
||||||
>
|
>
|
||||||
> <a name="trailing-closure"></a>
|
> <a name="trailing-closure"></a>
|
||||||
@ -1082,7 +1082,7 @@ t.0 = t.1
|
|||||||
|
|
||||||
对于模块的成员来说,只能直接访问顶级声明中的成员。
|
对于模块的成员来说,只能直接访问顶级声明中的成员。
|
||||||
|
|
||||||
使用 `dynamicMemberLookup` 属性声明的类型包含可以在运行时查找的成员,具体请参阅 [属性](07_Attributes.html)
|
使用 `dynamicMemberLookup` 属性声明的类型包含可以在运行时查找的成员,具体请参阅 [属性](./07_Attributes.md)
|
||||||
|
|
||||||
为了区分只有参数名有所不同的方法或构造器,在圆括号中写出参数名,参数名后紧跟一个冒号,对于没有参数名的参数,使用下划线代替参数名。而对于重载方法,则需使用类型标注进行区分。例如:
|
为了区分只有参数名有所不同的方法或构造器,在圆括号中写出参数名,参数名后紧跟一个冒号,对于没有参数名的参数,使用下划线代替参数名。而对于重载方法,则需使用类型标注进行区分。例如:
|
||||||
|
|
||||||
@ -1117,17 +1117,17 @@ let x = [10, 3, 20, 15, 4]
|
|||||||
> 显式成员表达式语法
|
> 显式成员表达式语法
|
||||||
>
|
>
|
||||||
<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.md#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.md#identifier) [*泛型实参子句*](./08_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub><br/>
|
||||||
>
|
>
|
||||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.html#identifier) **(** [*参数名称*](#argument-names) **)**
|
> *显式成员表达式* → [*后缀表达式*](./#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.md#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.md#identifier) **:**
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="postfix_self_expression"></a>
|
<a name="postfix_self_expression"></a>
|
||||||
@ -1161,7 +1161,7 @@ let x = [10, 3, 20, 15, 4]
|
|||||||
|
|
||||||
要获取下标表达式的值,可将索引表达式作为下标表达式的参数来调用下标 getter。下标 setter 的调用方式与之一样。
|
要获取下标表达式的值,可将索引表达式作为下标表达式的参数来调用下标 getter。下标 setter 的调用方式与之一样。
|
||||||
|
|
||||||
关于下标的声明,请参阅 [协议下标声明](05_Declarations.html#protocol_subscript_declaration)。
|
关于下标的声明,请参阅 [协议下标声明](./05_Declarations.md#protocol_subscript_declaration)。
|
||||||
|
|
||||||
> 下标表达式语法
|
> 下标表达式语法
|
||||||
>
|
>
|
||||||
|
|||||||
@ -10,9 +10,9 @@
|
|||||||
> 语句语法
|
> 语句语法
|
||||||
>
|
>
|
||||||
<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>
|
||||||
>
|
>
|
||||||
@ -69,7 +69,7 @@ for item in collection {
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
<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>
|
||||||
@ -98,21 +98,21 @@ while condition {
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
<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>
|
||||||
@ -141,7 +141,7 @@ repeat {
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
<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>
|
||||||
@ -205,10 +205,10 @@ if condition 1 {
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
<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>
|
||||||
@ -224,7 +224,7 @@ guard condition else {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
`guard` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件也可以是一条可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.html#optional_binding)。
|
`guard` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件也可以是一条可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.md#optional_binding)。
|
||||||
|
|
||||||
在 `guard` 语句中进行可选绑定的任何常量或者变量,其可用范围从声明开始直到作用域结束。
|
在 `guard` 语句中进行可选绑定的任何常量或者变量,其可用范围从声明开始直到作用域结束。
|
||||||
|
|
||||||
@ -235,13 +235,13 @@ guard condition else {
|
|||||||
* `continue`
|
* `continue`
|
||||||
* `throw`
|
* `throw`
|
||||||
|
|
||||||
关于控制转移语句,请参阅 [控制转移语句](#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.md#code-block)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="switch_statements"></a>
|
<a name="switch_statements"></a>
|
||||||
@ -267,7 +267,7 @@ default:
|
|||||||
|
|
||||||
`switch` 语句会先计算*控制表达式*的值,然后与每一个 `case` 的模式进行匹配。如果匹配成功,程序将会执行对应的 `case` 中的语句。另外,每一个 `case` 的作用域都不能为空,也就是说在每一个 `case` 的冒号(`:`)后面必须至少有一条语句。如果你不想在匹配到的 `case` 中执行代码,只需在该 `case` 中写一条 `break` 语句即可。
|
`switch` 语句会先计算*控制表达式*的值,然后与每一个 `case` 的模式进行匹配。如果匹配成功,程序将会执行对应的 `case` 中的语句。另外,每一个 `case` 的作用域都不能为空,也就是说在每一个 `case` 的冒号(`:`)后面必须至少有一条语句。如果你不想在匹配到的 `case` 中执行代码,只需在该 `case` 中写一条 `break` 语句即可。
|
||||||
|
|
||||||
可以用作控制表达式的值是十分灵活的。除了标量类型外,如 `Int`、`Character`,你可以使用任何类型的值,包括浮点数、字符串、元组、自定义类型的实例和可选类型。控制表达式的值还可以用来匹配枚举类型中的成员值或是检查该值是否包含在指定的 `Range` 中。关于如何在 `switch` 语句中使用这些类型,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章中的 [Switch](../chapter2/05_Control_Flow.html#switch)。
|
可以用作控制表达式的值是十分灵活的。除了标量类型外,如 `Int`、`Character`,你可以使用任何类型的值,包括浮点数、字符串、元组、自定义类型的实例和可选类型。控制表达式的值还可以用来匹配枚举类型中的成员值或是检查该值是否包含在指定的 `Range` 中。关于如何在 `switch` 语句中使用这些类型,请参阅 [控制流](../chapter2/05_Control_Flow.md) 一章中的 [Switch](../chapter2/05_Control_Flow.md#switch)。
|
||||||
|
|
||||||
每个 `case` 的模式后面可以有一个 `where` 子句。`where` 子句由 `where` 关键字紧跟一个提供额外条件的表达式组成。因此,当且仅当控制表达式匹配一个 `case` 的模式且 `where` 子句的表达式为真时,`case` 中的语句才会被执行。在下面的例子中,控制表达式只会匹配包含两个相等元素的元组,例如 `(1, 1)`:
|
每个 `case` 的模式后面可以有一个 `where` 子句。`where` 子句由 `where` 关键字紧跟一个提供额外条件的表达式组成。因此,当且仅当控制表达式匹配一个 `case` 的模式且 `where` 子句的表达式为真时,`case` 中的语句才会被执行。在下面的例子中,控制表达式只会匹配包含两个相等元素的元组,例如 `(1, 1)`:
|
||||||
|
|
||||||
@ -316,7 +316,7 @@ case .suppressed:
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
<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.md#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>
|
||||||
@ -329,7 +329,7 @@ case .suppressed:
|
|||||||
> *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** **:**
|
||||||
@ -339,7 +339,7 @@ case .suppressed:
|
|||||||
> *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>
|
||||||
@ -365,7 +365,7 @@ case .suppressed:
|
|||||||
|
|
||||||
标签的作用域在该标签所标记的语句内。可以嵌套使用带标签的语句,但标签名必须唯一。
|
标签的作用域在该标签所标记的语句内。可以嵌套使用带标签的语句,但标签名必须唯一。
|
||||||
|
|
||||||
关于使用带标签的语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章中的 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
|
关于使用带标签的语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.md) 一章中的 [带标签的语句](../chapter2/05_Control_Flow.md#labeled_statements)。
|
||||||
|
|
||||||
> 带标签的语句语法
|
> 带标签的语句语法
|
||||||
>
|
>
|
||||||
@ -383,7 +383,7 @@ case .suppressed:
|
|||||||
> *语句标签* → [*标签名称*](#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>
|
||||||
@ -422,7 +422,7 @@ 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.md) 一章的 [Break](../chapter2/05_Control_Flow.md#break) 和 [带标签的语句](../chapter2/05_Control_Flow.md#labeled_statements)。
|
||||||
|
|
||||||
> break 语句语法
|
> break 语句语法
|
||||||
>
|
>
|
||||||
@ -449,7 +449,7 @@ case .suppressed:
|
|||||||
|
|
||||||
在 `for` 语句中,`continue` 语句执行后,增量表达式还是会被计算,这是因为每次循环体执行完毕后,增量表达式都会被计算。
|
在 `for` 语句中,`continue` 语句执行后,增量表达式还是会被计算,这是因为每次循环体执行完毕后,增量表达式都会被计算。
|
||||||
|
|
||||||
关于使用 `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.md) 一章的 [Continue](../chapter2/05_Control_Flow.md#continue) 和 [带标签的语句](../chapter2/05_Control_Flow.md#labeled_statements)。
|
||||||
|
|
||||||
> continue 语句语法
|
> continue 语句语法
|
||||||
>
|
>
|
||||||
@ -465,7 +465,7 @@ case .suppressed:
|
|||||||
|
|
||||||
`fallthrough` 语句可出现在 `switch` 语句中的任意 `case` 中,但不能出现在最后一个 `case` 中。同时,`fallthrough` 语句也不能把控制权转移到使用了值绑定的 `case`。
|
`fallthrough` 语句可出现在 `switch` 语句中的任意 `case` 中,但不能出现在最后一个 `case` 中。同时,`fallthrough` 语句也不能把控制权转移到使用了值绑定的 `case`。
|
||||||
|
|
||||||
关于在 `switch` 语句中使用 `fallthrough` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [控制转移语句](../chapter2/05_Control_Flow.html#control_transfer_statements)。
|
关于在 `switch` 语句中使用 `fallthrough` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.md) 一章的 [控制转移语句](../chapter2/05_Control_Flow.md#control_transfer_statements)。
|
||||||
|
|
||||||
> fallthrough 语句语法
|
> fallthrough 语句语法
|
||||||
>
|
>
|
||||||
@ -491,7 +491,7 @@ case .suppressed:
|
|||||||
> 注意
|
> 注意
|
||||||
>
|
>
|
||||||
>
|
>
|
||||||
> 正如 [可失败构造器](05_Declarations.html#failable_initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。
|
> 正如 [可失败构造器](./05_Declarations.md#failable_initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。
|
||||||
>
|
>
|
||||||
|
|
||||||
而只写 `return` 时,仅仅是从该函数或方法中返回,而不返回任何值(也就是说,函数或方法的返回类型为 `Void` 或者说 `()`)。
|
而只写 `return` 时,仅仅是从该函数或方法中返回,而不返回任何值(也就是说,函数或方法的返回类型为 `Void` 或者说 `()`)。
|
||||||
@ -500,7 +500,7 @@ case .suppressed:
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
<a name="return-statement"></a>
|
<a name="return-statement"></a>
|
||||||
> *return 语句* → **return** [*表达式*](04_Expressions.html#expression)<sub>可选</sub>
|
> *return 语句* → **return** [*表达式*](./04_Expressions.md#expression)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="throw_statements"></a>
|
<a name="throw_statements"></a>
|
||||||
@ -517,13 +517,13 @@ case .suppressed:
|
|||||||
|
|
||||||
表达式的结果必须符合 `ErrorType` 协议。
|
表达式的结果必须符合 `ErrorType` 协议。
|
||||||
|
|
||||||
关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html) 一章的 [用 throwing 函数传递错误](../chapter2/18_Error_Handling.html#propagating_errors_using_throwing_functions)。
|
关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.md) 一章的 [用 throwing 函数传递错误](../chapter2/18_Error_Handling.md#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.md#expression)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="defer_statements"></a>
|
<a name="defer_statements"></a>
|
||||||
@ -561,7 +561,7 @@ f()
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
<a name="defer-statement"></a>
|
<a name="defer-statement"></a>
|
||||||
> *延迟语句* → **defer** [*代码块*](05_Declarations.html#code-block)
|
> *延迟语句* → **defer** [*代码块*](./05_Declarations.md#code-block)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="do_statements"></a>
|
<a name="do_statements"></a>
|
||||||
@ -586,21 +586,21 @@ do {
|
|||||||
|
|
||||||
如同 `switch` 语句,编译器会判断 `catch` 子句是否有遗漏。如果 `catch` 子句没有遗漏,则认为错误已被处理。否则,错误会自动传递到外围作用域,被某个 `catch` 子句处理掉或者被用 `throws` 关键字声明的抛出函数继续向外抛出。
|
如同 `switch` 语句,编译器会判断 `catch` 子句是否有遗漏。如果 `catch` 子句没有遗漏,则认为错误已被处理。否则,错误会自动传递到外围作用域,被某个 `catch` 子句处理掉或者被用 `throws` 关键字声明的抛出函数继续向外抛出。
|
||||||
|
|
||||||
为了确保错误已经被处理,可以让 `catch` 子句使用匹配所有错误的模式,如通配符模式(`_`)。如果一个 `catch` 子句不指定一种具体模式,`catch` 子句会匹配任何错误,并绑定到名为 `error` 的局部常量。有关在 `catch` 子句中使用模式的更多信息,请参阅 [模式](07_Patterns.html)。
|
为了确保错误已经被处理,可以让 `catch` 子句使用匹配所有错误的模式,如通配符模式(`_`)。如果一个 `catch` 子句不指定一种具体模式,`catch` 子句会匹配任何错误,并绑定到名为 `error` 的局部常量。有关在 `catch` 子句中使用模式的更多信息,请参阅 [模式](./07_Patterns.md)。
|
||||||
|
|
||||||
关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../chapter2/17_Error_Handling.html#handling_errors)。
|
关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../chapter2/17_Error_Handling.md#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.md#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.md#pattern)<sub>可选</sub> [*where 子句*](./#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="compiler_control_statements"></a>
|
<a name="compiler_control_statements"></a>
|
||||||
@ -725,9 +725,9 @@ statements to compile if both compilation conditions are false
|
|||||||
<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) **)**
|
||||||
>
|
>
|
||||||
@ -764,13 +764,13 @@ statements to compile if both compilation conditions are false
|
|||||||
> *架构* → **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>
|
||||||
> *环境* → **模拟器**
|
> *环境* → **模拟器**
|
||||||
@ -805,7 +805,7 @@ statements to compile if both compilation conditions are false
|
|||||||
> *行号* → 大于 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)
|
||||||
>
|
>
|
||||||
|
|
||||||
### 编译时诊断语句
|
### 编译时诊断语句
|
||||||
@ -828,7 +828,7 @@ statements to compile if both compilation conditions are false
|
|||||||
> *诊断语句* → **#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>
|
||||||
@ -877,9 +877,9 @@ if #available(platform name version, ..., *) {
|
|||||||
> *平台名称* → **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)
|
||||||
>
|
>
|
||||||
|
|||||||
@ -50,7 +50,7 @@ Swift 的源文件中的顶级代码(top-level code)由零个或多个语句
|
|||||||
|
|
||||||
> 顶级声明语法
|
> 顶级声明语法
|
||||||
>
|
>
|
||||||
> *顶级声明* → [*多条语句*](10_Statements.html#statements)<sub>可选</sub>
|
> *顶级声明* → [*多条语句*](./10_Statements.md#statements)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="code_blocks"></a>
|
<a name="code_blocks"></a>
|
||||||
@ -70,7 +70,7 @@ Swift 的源文件中的顶级代码(top-level code)由零个或多个语句
|
|||||||
>
|
>
|
||||||
> <a name="code-block"></a>
|
> <a name="code-block"></a>
|
||||||
>
|
>
|
||||||
> *代码块* → **{** [*多条语句*](10_Statements.html#statements)<sub>可选</sub> **}**
|
> *代码块* → **{** [*多条语句*](./10_Statements.md#statements)<sub>可选</sub> **}**
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="import_declaration"></a>
|
<a name="import_declaration"></a>
|
||||||
@ -94,7 +94,7 @@ 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.md#attributes)<sub>可选</sub> **import** [*导入类型*](#import-kind)<sub>可选</sub> [*导入路径*](#import-path)
|
||||||
>
|
>
|
||||||
> <a name="import-kind"></a>
|
> <a name="import-kind"></a>
|
||||||
>
|
>
|
||||||
@ -106,7 +106,7 @@ import 模块.子模块
|
|||||||
>
|
>
|
||||||
> <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.md#identifier) | [*运算符*](./02_Lexical_Structure.md#operator)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="constant_declaration"></a>
|
<a name="constant_declaration"></a>
|
||||||
@ -137,18 +137,18 @@ print("The second number is \(secondNumber).")
|
|||||||
// 打印“The second number is 42.”
|
// 打印“The second number is 42.”
|
||||||
```
|
```
|
||||||
|
|
||||||
当常量名称的类型(`:` 类型)可以被推断出时,类型标注在常量声明中是可选的,正如 [类型推断](03_Types.html#type_inference) 中所描述的。
|
当常量名称的类型(`:` 类型)可以被推断出时,类型标注在常量声明中是可选的,正如 [类型推断](./03_Types.md#type_inference) 中所描述的。
|
||||||
|
|
||||||
声明一个常量类型属性要使用 `static` 声明修饰符。类型属性在 [类型属性](../chapter2/10_Properties.html#type_properties)中有介绍。
|
声明一个常量类型属性要使用 `static` 声明修饰符。类型属性在 [类型属性](../chapter2/10_Properties.md#type_properties)中有介绍。
|
||||||
|
|
||||||
如果还想获得更多关于常量的信息或者想在使用中获得帮助,请参阅 [常量和变量](../chapter2/01_The_Basics.html#constants_and_variables) 和 [存储属性](../chapter2/10_Properties.html#stored_properties)。
|
如果还想获得更多关于常量的信息或者想在使用中获得帮助,请参阅 [常量和变量](../chapter2/01_The_Basics.md#constants_and_variables) 和 [存储属性](../chapter2/10_Properties.md#stored_properties)。
|
||||||
|
|
||||||
<a name="grammer_of_a_constant_declaration"></a>
|
<a name="grammer_of_a_constant_declaration"></a>
|
||||||
> 常量声明语法
|
> 常量声明语法
|
||||||
>
|
>
|
||||||
> <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.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **let** [*模式构造器列表*](pattern-initializer-list)
|
||||||
>
|
>
|
||||||
> <a name="pattern-initializer-list"></a>
|
> <a name="pattern-initializer-list"></a>
|
||||||
>
|
>
|
||||||
@ -156,11 +156,11 @@ print("The second number is \(secondNumber).")
|
|||||||
>
|
>
|
||||||
> <a name="pattern-initializer"></a>
|
> <a name="pattern-initializer"></a>
|
||||||
>
|
>
|
||||||
> *模式构造器* → [*模式*](07_Patterns.html#pattern) [*构造器*](#initializer)<sub>可选</sub>
|
> *模式构造器* → [*模式*](./07_Patterns.md#pattern) [*构造器*](#initializer)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
> <a name="initializer"></a>
|
> <a name="initializer"></a>
|
||||||
>
|
>
|
||||||
> *构造器* → **=** [*表达式*](04_Expressions.html#expression)
|
> *构造器* → **=** [*表达式*](./04_Expressions.md#expression)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="variable_declaration"></a>
|
<a name="variable_declaration"></a>
|
||||||
@ -176,7 +176,7 @@ print("The second number is \(secondNumber).")
|
|||||||
> 也可以在协议声明中声明属性,详情请参阅 [协议属性声明](#protocol_property_declaration)。
|
> 也可以在协议声明中声明属性,详情请参阅 [协议属性声明](#protocol_property_declaration)。
|
||||||
>
|
>
|
||||||
|
|
||||||
可以在子类中重写继承来的变量属性,使用 `override` 声明修饰符标记属性的声明即可,详情请参阅 [重写](../chapter2/13_Inheritance.html#overriding)。
|
可以在子类中重写继承来的变量属性,使用 `override` 声明修饰符标记属性的声明即可,详情请参阅 [重写](../chapter2/13_Inheritance.md#overriding)。
|
||||||
|
|
||||||
<a name="stored_variables_and_stored_variable_properties"></a>
|
<a name="stored_variables_and_stored_variable_properties"></a>
|
||||||
### 存储型变量和存储型变量属性
|
### 存储型变量和存储型变量属性
|
||||||
@ -213,13 +213,13 @@ var 变量名称: 类型 {
|
|||||||
|
|
||||||
可以在全局范围、函数内部,以及类、结构体、枚举、扩展的声明中使用这种形式的声明。当变量以这种形式在全局范围或者函数内部被声明时,它表示一个计算型变量。当它在类、结构体、枚举、扩展声明的上下文中被声明时,它表示一个*计算型属性(computed property)*。
|
可以在全局范围、函数内部,以及类、结构体、枚举、扩展的声明中使用这种形式的声明。当变量以这种形式在全局范围或者函数内部被声明时,它表示一个计算型变量。当它在类、结构体、枚举、扩展声明的上下文中被声明时,它表示一个*计算型属性(computed property)*。
|
||||||
|
|
||||||
getter 用来读取变量值,setter 用来写入变量值。setter 子句是可选的,getter 子句是必须的。不过也可以将这些子句都省略,直接返回请求的值,正如在 [只读计算型属性](../chapter2/10_Properties.html#computed_properties) 中描述的那样。但是如果提供了一个 setter 子句,就必须也提供一个 getter 子句。
|
getter 用来读取变量值,setter 用来写入变量值。setter 子句是可选的,getter 子句是必须的。不过也可以将这些子句都省略,直接返回请求的值,正如在 [只读计算型属性](../chapter2/10_Properties.md#computed_properties) 中描述的那样。但是如果提供了一个 setter 子句,就必须也提供一个 getter 子句。
|
||||||
|
|
||||||
setter 的圆括号以及 setter 名称是可选的。如果提供了 setter 名称,它就会作为 setter 的参数名称使用。如果不提供 setter 名称,setter 的参数的默认名称为 `newValue`,正如在 [便捷 setter 声明](../chapter2/10_Properties.html#shorthand_setter_declaration) 中描述的那样。
|
setter 的圆括号以及 setter 名称是可选的。如果提供了 setter 名称,它就会作为 setter 的参数名称使用。如果不提供 setter 名称,setter 的参数的默认名称为 `newValue`,正如在 [便捷 setter 声明](../chapter2/10_Properties.md#shorthand_setter_declaration) 中描述的那样。
|
||||||
|
|
||||||
与存储型变量和存储型属性不同,计算型变量和计算型属性的值不存储在内存中。
|
与存储型变量和存储型属性不同,计算型变量和计算型属性的值不存储在内存中。
|
||||||
|
|
||||||
要获得更多关于计算型属性的信息和例子,请参阅 [计算型属性](../chapter2/10_Properties.html#computed_properties)。
|
要获得更多关于计算型属性的信息和例子,请参阅 [计算型属性](../chapter2/10_Properties.md#computed_properties)。
|
||||||
|
|
||||||
<a name="stored_variable_observers_and_property_observers"></a>
|
<a name="stored_variable_observers_and_property_observers"></a>
|
||||||
### 存储型变量和属性的观察器
|
### 存储型变量和属性的观察器
|
||||||
@ -240,7 +240,7 @@ var 变量名称: 类型 = 表达式 {
|
|||||||
可以在全局范围、函数内部,或者类、结构体的声明中使用这种形式的声明。当变量以这种形式在全局范围或者函数内部被声明时,观察器表示一个存储型变量观察器。当它在类和结构体的声明中被声明时,观察器表示一个属性观察器。
|
可以在全局范围、函数内部,或者类、结构体的声明中使用这种形式的声明。当变量以这种形式在全局范围或者函数内部被声明时,观察器表示一个存储型变量观察器。当它在类和结构体的声明中被声明时,观察器表示一个属性观察器。
|
||||||
|
|
||||||
可以为任何存储型属性添加观察器。也可以通过重写父类属性的方式为任何继承的属性(无论是存储型还是计算型的)添加观察器
|
可以为任何存储型属性添加观察器。也可以通过重写父类属性的方式为任何继承的属性(无论是存储型还是计算型的)添加观察器
|
||||||
,正如 [重写属性观察器](../chapter2/13_Inheritance.html#overriding_property_observers) 中所描述的。
|
,正如 [重写属性观察器](../chapter2/13_Inheritance.md#overriding_property_observers) 中所描述的。
|
||||||
|
|
||||||
用于初始化的表达式在类或者结构的声明中是可选的,但是在其他声明中则是必须的。如果可以从初始化表达式中推断出类型信息,那么可以不提供类型标注。
|
用于初始化的表达式在类或者结构的声明中是可选的,但是在其他声明中则是必须的。如果可以从初始化表达式中推断出类型信息,那么可以不提供类型标注。
|
||||||
|
|
||||||
@ -252,12 +252,12 @@ var 变量名称: 类型 = 表达式 {
|
|||||||
|
|
||||||
提供了 `willSet` 时,`didSet` 是可选的。同样的,提供了 `didSet` 时,`willSet` 则是可选的。
|
提供了 `willSet` 时,`didSet` 是可选的。同样的,提供了 `didSet` 时,`willSet` 则是可选的。
|
||||||
|
|
||||||
要获得更多信息以及查看如何使用属性观察器的例子,请参阅 [属性观察器](../chapter2/10_Properties.html#property_observers)。
|
要获得更多信息以及查看如何使用属性观察器的例子,请参阅 [属性观察器](../chapter2/10_Properties.md#property_observers)。
|
||||||
|
|
||||||
<a name="type_variable_properties"></a>
|
<a name="type_variable_properties"></a>
|
||||||
### 类型变量属性
|
### 类型变量属性
|
||||||
|
|
||||||
要声明一个类型变量属性,用 `static` 声明修饰符标记该声明。类可以改用 `class` 声明修饰符标记类的类型计算型属性从而允许子类重写超类的实现。类型属性在 [类型属性](../chapter2/10_Properties.html#type_properties) 章节有详细讨论。
|
要声明一个类型变量属性,用 `static` 声明修饰符标记该声明。类可以改用 `class` 声明修饰符标记类的类型计算型属性从而允许子类重写超类的实现。类型属性在 [类型属性](../chapter2/10_Properties.md#type_properties) 章节有详细讨论。
|
||||||
|
|
||||||
> 注意
|
> 注意
|
||||||
>
|
>
|
||||||
@ -270,23 +270,23 @@ var 变量名称: 类型 = 表达式 {
|
|||||||
<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.md#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.md#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.md#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.md#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.md#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.md#identifier)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="getter-setter-block"></a>
|
<a name="getter-setter-block"></a>
|
||||||
@ -298,15 +298,15 @@ var 变量名称: 类型 = 表达式 {
|
|||||||
>
|
>
|
||||||
> <a name="getter-clause"></a>
|
> <a name="getter-clause"></a>
|
||||||
>
|
>
|
||||||
> *getter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get** [*代码块*](#code-block)
|
> *getter 子句* → [*特性列表*](./06_Attributes.md#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.md#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.md#identifier) **)**
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="getter-setter-keyword-block"></a>
|
<a name="getter-setter-keyword-block"></a>
|
||||||
@ -316,11 +316,11 @@ var 变量名称: 类型 = 表达式 {
|
|||||||
>
|
>
|
||||||
> <a name="getter-keyword-clause"></a>
|
> <a name="getter-keyword-clause"></a>
|
||||||
>
|
>
|
||||||
> *getter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get**
|
> *getter 关键字子句* → [*特性列表*](./06_Attributes.md#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.md#attributes)<sub>可选</sub> **set**
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="willSet-didSet-block"></a>
|
<a name="willSet-didSet-block"></a>
|
||||||
@ -330,11 +330,11 @@ var 变量名称: 类型 = 表达式 {
|
|||||||
>
|
>
|
||||||
> <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.md#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.md#attributes)<sub>可选</sub> **didSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="type_alias_declaration"></a>
|
<a name="type_alias_declaration"></a>
|
||||||
@ -400,15 +400,15 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
|
|||||||
>
|
>
|
||||||
> <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.md#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.md#identifier)
|
||||||
>
|
>
|
||||||
> <a name="typealias-assignment"></a>
|
> <a name="typealias-assignment"></a>
|
||||||
>
|
>
|
||||||
> *类型别名赋值* → **=** [*类型*](03_Types.html#type)
|
> *类型别名赋值* → **=** [*类型*](./03_Types.md#type)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="function_declaration"></a>
|
<a name="function_declaration"></a>
|
||||||
@ -439,7 +439,7 @@ func 函数名称(参数列表) {
|
|||||||
|
|
||||||
大多数时候,嵌套函数都是可逃逸的函数。仅当一个嵌套函数捕获了某个确保了永不逃逸的值——例如一个输入输出参数——或者传入一个非逃逸函数参数的时候,这个嵌套函数才是非逃逸的。
|
大多数时候,嵌套函数都是可逃逸的函数。仅当一个嵌套函数捕获了某个确保了永不逃逸的值——例如一个输入输出参数——或者传入一个非逃逸函数参数的时候,这个嵌套函数才是非逃逸的。
|
||||||
|
|
||||||
更多关于嵌套函数的讨论,请参阅 [嵌套函数](../chapter2/06_Functions.html#Nested_Functions)。
|
更多关于嵌套函数的讨论,请参阅 [嵌套函数](../chapter2/06_Functions.md#Nested_Functions)。
|
||||||
|
|
||||||
<a name="parameter_names"></a>
|
<a name="parameter_names"></a>
|
||||||
### 参数名
|
### 参数名
|
||||||
@ -487,7 +487,7 @@ repeatGreeting("Hello, world!", count: 2) // count 有标签, greeting 没有
|
|||||||
|
|
||||||
不能将同一个值传递给多个输入输出参数,因为这种情况下的拷贝与覆盖行为的顺序是不确定的,因此原始值的最终值也将无法确定。
|
不能将同一个值传递给多个输入输出参数,因为这种情况下的拷贝与覆盖行为的顺序是不确定的,因此原始值的最终值也将无法确定。
|
||||||
|
|
||||||
更多关于内存安全和内存独占权的讨论,请参阅 [内存安全](../chapter2/24_MemorySafety.html)。
|
更多关于内存安全和内存独占权的讨论,请参阅 [内存安全](../chapter2/24_MemorySafety.md)。
|
||||||
|
|
||||||
如果一个闭包或者嵌套函数捕获了一个输入输出参数,那么这个闭包或者嵌套函数必须是非逃逸的。如果你需要捕获一个输入输出参数,但并不对其进行修改或者在其他代码中观察其值变化,那么你可以使用捕获列表来显式地表明这是个不可变捕获。
|
如果一个闭包或者嵌套函数捕获了一个输入输出参数,那么这个闭包或者嵌套函数必须是非逃逸的。如果你需要捕获一个输入输出参数,但并不对其进行修改或者在其他代码中观察其值变化,那么你可以使用捕获列表来显式地表明这是个不可变捕获。
|
||||||
|
|
||||||
@ -514,7 +514,7 @@ func multithreadedFunction(queue: DispatchQueue, x: inout Int) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
关于输入输出参数的详细讨论,请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。
|
关于输入输出参数的详细讨论,请参阅 [输入输出参数](../chapter2/06_Functions.md#in_out_parameters)。
|
||||||
|
|
||||||
<a name="special_kinds_of_parameters"></a>
|
<a name="special_kinds_of_parameters"></a>
|
||||||
|
|
||||||
@ -530,7 +530,7 @@ _ : 参数类型
|
|||||||
|
|
||||||
以下划线(`_`)命名的参数会被显式忽略,无法在函数内使用。
|
以下划线(`_`)命名的参数会被显式忽略,无法在函数内使用。
|
||||||
|
|
||||||
一个参数的基本类型名称如果紧跟着三个点(`...`),会被视为可变参数。一个函数至多可以拥有一个可变参数,且必须是最后一个参数。可变参数会作为包含该参数类型元素的数组处理。举例来讲,可变参数 `Int...` 会作为 `[Int]` 来处理。关于使用可变参数的例子,请参阅 [可变参数](../chapter2/06_Functions.html#variadic_parameters)。
|
一个参数的基本类型名称如果紧跟着三个点(`...`),会被视为可变参数。一个函数至多可以拥有一个可变参数,且必须是最后一个参数。可变参数会作为包含该参数类型元素的数组处理。举例来讲,可变参数 `Int...` 会作为 `[Int]` 来处理。关于使用可变参数的例子,请参阅 [可变参数](../chapter2/06_Functions.md#variadic_parameters)。
|
||||||
|
|
||||||
如果在参数类型后面有一个以等号(`=`)连接的表达式,该参数会拥有默认值,即给定表达式的值。当函数被调用时,给定的表达式会被求值。如果参数在函数调用时被省略了,就会使用其默认值。
|
如果在参数类型后面有一个以等号(`=`)连接的表达式,该参数会拥有默认值,即给定表达式的值。当函数被调用时,给定的表达式会被求值。如果参数在函数调用时被省略了,就会使用其默认值。
|
||||||
|
|
||||||
@ -607,7 +607,7 @@ func someFunction(callback: () throws -> Void) rethrows {
|
|||||||
|
|
||||||
Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它的调用者。`Never` 返回类型的函数或方法可以称为不归,不归函数、方法要么引发不可恢复的错误,要么永远不停地运作,这会使调用后本应执行得代码就不再执行了。但即使是不归函数、方法,抛错函数和重抛出函数也可以将程序控制转移到合适的 `catch` 代码块。
|
Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它的调用者。`Never` 返回类型的函数或方法可以称为不归,不归函数、方法要么引发不可恢复的错误,要么永远不停地运作,这会使调用后本应执行得代码就不再执行了。但即使是不归函数、方法,抛错函数和重抛出函数也可以将程序控制转移到合适的 `catch` 代码块。
|
||||||
|
|
||||||
不归函数、方法可以在 guard 语句的 else 字句中调用,具体讨论在[*Guard 语句*](10_Statements.html#guard_statements)。
|
不归函数、方法可以在 guard 语句的 else 字句中调用,具体讨论在[*Guard 语句*](./10_Statements.md#guard_statements)。
|
||||||
|
|
||||||
你可以重写一个不归方法,但是新的方法必须保持原有的返回类型和没有返回的行为。
|
你可以重写一个不归方法,但是新的方法必须保持原有的返回类型和没有返回的行为。
|
||||||
|
|
||||||
@ -615,15 +615,15 @@ 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.md#generic-parameter-clause)<sub>可选</sub> [*函数签名*](./#function-signature) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#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.md#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.md#identifier) | [*运算符*](./02_Lexical_Structure.md#operator)
|
||||||
>
|
>
|
||||||
>
|
>
|
||||||
> <a name="function-signature"></a>
|
> <a name="function-signature"></a>
|
||||||
@ -635,7 +635,7 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
|||||||
>
|
>
|
||||||
> <a name="function-result"></a>
|
> <a name="function-result"></a>
|
||||||
>
|
>
|
||||||
> *函数结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type)
|
> *函数结果* → **->** [*特性列表*](./06_Attributes.md#attributes)<sub>可选</sub> [*类型*](./03_Types.md#type)
|
||||||
>
|
>
|
||||||
> <a name="function-body"></a>
|
> <a name="function-body"></a>
|
||||||
>
|
>
|
||||||
@ -652,23 +652,23 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
|||||||
>
|
>
|
||||||
> <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.md#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.md#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.md#type-annotation) **...**
|
||||||
>
|
>
|
||||||
> <a name="external-parameter-name"></a>
|
> <a name="external-parameter-name"></a>
|
||||||
>
|
>
|
||||||
> *外部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_**
|
> *外部参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | **_**
|
||||||
>
|
>
|
||||||
> <a name="local-parameter-name"></a>
|
> <a name="local-parameter-name"></a>
|
||||||
>
|
>
|
||||||
> *内部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_**
|
> *内部参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | **_**
|
||||||
>
|
>
|
||||||
> <a name="default-argument-clause"></a>
|
> <a name="default-argument-clause"></a>
|
||||||
>
|
>
|
||||||
> *默认参数子句* → **=** [*表达式*](04_Expressions.html#expression)
|
> *默认参数子句* → **=** [*表达式*](./04_Expressions.md#expression)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="enumeration_declaration"></a>
|
<a name="enumeration_declaration"></a>
|
||||||
@ -683,7 +683,7 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
|||||||
|
|
||||||
不同于类或者结构体,枚举类型并不隐式提供默认构造器,所有构造器必须显式声明。一个构造器可以委托给枚举中的其他构造器,但是构造过程仅当构造器将一个枚举用例赋值给 `self` 后才算完成。
|
不同于类或者结构体,枚举类型并不隐式提供默认构造器,所有构造器必须显式声明。一个构造器可以委托给枚举中的其他构造器,但是构造过程仅当构造器将一个枚举用例赋值给 `self` 后才算完成。
|
||||||
|
|
||||||
和结构体类似但是和类不同,枚举是值类型。枚举实例在被赋值到变量或常量时,或者传递给函数作为参数时会被复制。更多关于值类型的信息,请参阅 [结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.html#structures_and_enumerations_are_value_types)。
|
和结构体类似但是和类不同,枚举是值类型。枚举实例在被赋值到变量或常量时,或者传递给函数作为参数时会被复制。更多关于值类型的信息,请参阅 [结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.md#structures_and_enumerations_are_value_types)。
|
||||||
|
|
||||||
可以扩展枚举类型,正如在 [扩展声明](#extension_declaration) 中讨论的一样。
|
可以扩展枚举类型,正如在 [扩展声明](#extension_declaration) 中讨论的一样。
|
||||||
|
|
||||||
@ -719,7 +719,7 @@ let f = Number.integer
|
|||||||
let evenInts: [Number] = [0, 2, 4, 6].map(f)
|
let evenInts: [Number] = [0, 2, 4, 6].map(f)
|
||||||
```
|
```
|
||||||
|
|
||||||
要获得更多关于具有关联值的枚举用例的信息和例子,请参阅 [关联值](../chapter2/08_Enumerations.html#associated_values)。
|
要获得更多关于具有关联值的枚举用例的信息和例子,请参阅 [关联值](../chapter2/08_Enumerations.md#associated_values)。
|
||||||
|
|
||||||
<a name="enumerations_with_indirection"></a>
|
<a name="enumerations_with_indirection"></a>
|
||||||
#### 递归枚举
|
#### 递归枚举
|
||||||
@ -774,14 +774,14 @@ enum GamePlayMode: String {
|
|||||||
|
|
||||||
在上面这个例子中,`GamePlayMode.cooperative` 的原始值是 `"cooperative"`,`GamePlayMode.individual` 的原始值是 `"individual"`,`GamePlayMode.competitive` 的原始值是 `"competitive"`。
|
在上面这个例子中,`GamePlayMode.cooperative` 的原始值是 `"cooperative"`,`GamePlayMode.individual` 的原始值是 `"individual"`,`GamePlayMode.competitive` 的原始值是 `"competitive"`。
|
||||||
|
|
||||||
枚举用例具有原始值的枚举类型隐式地符合定义在 Swift 标准库中的 `RawRepresentable` 协议。所以,它们拥有一个 `rawValue` 属性和一个可失败构造器 `init?(rawValue: RawValue)`。可以使用 `rawValue` 属性去获取枚举用例的原始值,例如 `ExampleEnum.b.rawValue`。还可以根据原始值来创建一个相对应的枚举用例,只需调用枚举的可失败构造器,例如 `ExampleEnum(rawValue: 5)`,这个可失败构造器返回一个可选类型的用例。要获得更多关于具有原始值的枚举用例的信息和例子,请参阅 [原始值](../chapter2/08_Enumerations.html#raw_values)。
|
枚举用例具有原始值的枚举类型隐式地符合定义在 Swift 标准库中的 `RawRepresentable` 协议。所以,它们拥有一个 `rawValue` 属性和一个可失败构造器 `init?(rawValue: RawValue)`。可以使用 `rawValue` 属性去获取枚举用例的原始值,例如 `ExampleEnum.b.rawValue`。还可以根据原始值来创建一个相对应的枚举用例,只需调用枚举的可失败构造器,例如 `ExampleEnum(rawValue: 5)`,这个可失败构造器返回一个可选类型的用例。要获得更多关于具有原始值的枚举用例的信息和例子,请参阅 [原始值](../chapter2/08_Enumerations.md#raw_values)。
|
||||||
|
|
||||||
<a name="accessing_enumeration_cases"></a>
|
<a name="accessing_enumeration_cases"></a>
|
||||||
### 访问枚举用例
|
### 访问枚举用例
|
||||||
|
|
||||||
使用点语法(`.`)来引用枚举类型的枚举用例,例如 `EnumerationType.enumerationCase`。当枚举类型可以由上下文推断而出时,可以省略它(但是 `.` 仍然需要),正如 [枚举语法](../chapter2/08_Enumerations.html#enumeration_syntax) 和 [显式成员表达式](04_Expressions.html#explicit_member_expression) 所述。
|
使用点语法(`.`)来引用枚举类型的枚举用例,例如 `EnumerationType.enumerationCase`。当枚举类型可以由上下文推断而出时,可以省略它(但是 `.` 仍然需要),正如 [枚举语法](../chapter2/08_Enumerations.md#enumeration_syntax) 和 [显式成员表达式](./04_Expressions.md#explicit_member_expression) 所述。
|
||||||
|
|
||||||
可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../chapter2/08_Enumerations.html#matching_enumeration_values_with_a_switch_statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](07_Patterns.html#enumeration_case_pattern) 所述。
|
可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../chapter2/08_Enumerations.md#matching_enumeration_values_with_a_switch_statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](./07_Patterns.md#enumeration_case_pattern) 所述。
|
||||||
|
|
||||||
<a name="grammer_of_an_enumeration_declaration"></a>
|
<a name="grammer_of_an_enumeration_declaration"></a>
|
||||||
> 枚举声明语法
|
> 枚举声明语法
|
||||||
@ -789,12 +789,12 @@ enum GamePlayMode: String {
|
|||||||
>
|
>
|
||||||
> <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.md#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.md#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.md#generic-parameter-clause)<sub>可选</sub> [类型继承子句](./03_Types.md#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>
|
||||||
>
|
>
|
||||||
@ -802,11 +802,11 @@ enum GamePlayMode: String {
|
|||||||
>
|
>
|
||||||
> <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.md#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.md#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>
|
||||||
>
|
>
|
||||||
@ -814,21 +814,21 @@ enum GamePlayMode: String {
|
|||||||
>
|
>
|
||||||
> <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.md#tuple-type)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
> <a name="enum-name"></a>
|
> <a name="enum-name"></a>
|
||||||
>
|
>
|
||||||
> *枚举名称* → [*标识符*](02_Lexical_Structure.html#identifier)
|
> *枚举名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||||
>
|
>
|
||||||
> <a name="enum-case-name"></a>
|
> <a name="enum-case-name"></a>
|
||||||
>
|
>
|
||||||
> *枚举用例名称* → [*标识符*](02_Lexical_Structure.html#identifier)
|
> *枚举用例名称* → [*标识符*](./02_Lexical_Structure.md#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.md#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](./03_Types.md#type-inheritance-clause) [*泛型 where 子句*](./08_Generic_Parameters_and_Arguments.md#generic-where-clause) **{** [*多个原始值风格枚举成员*](#raw-value-style-enum-members) **}**
|
||||||
>
|
>
|
||||||
> <a name="raw-value-style-enum-members"></a>
|
> <a name="raw-value-style-enum-members"></a>
|
||||||
>
|
>
|
||||||
@ -836,11 +836,11 @@ enum GamePlayMode: String {
|
|||||||
>
|
>
|
||||||
> <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.md#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.md#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>
|
||||||
>
|
>
|
||||||
@ -856,7 +856,7 @@ enum GamePlayMode: String {
|
|||||||
>
|
>
|
||||||
> <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.md#numeric-literal) | [字符串型字面量](./02_Lexical_Structure.md#static-string-literal) | [布尔型字面量](./02_Lexical_Structure.md#boolean-literal)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="structure_declaration"></a>
|
<a name="structure_declaration"></a>
|
||||||
@ -870,24 +870,24 @@ struct 结构体名称: 采纳的协议 {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
结构体内可包含零个或多个声明。这些声明可以包括存储型和计算型属性、类型属性、实例方法、类型方法、构造器、下标、类型别名,甚至其他结构体、类、和枚举声明。结构体声明不能包含析构器或者协议声明。关于结构体的详细讨论和示例,请参阅 [类和结构体](../chapter2/09_Classes_and_Structures.html)。
|
结构体内可包含零个或多个声明。这些声明可以包括存储型和计算型属性、类型属性、实例方法、类型方法、构造器、下标、类型别名,甚至其他结构体、类、和枚举声明。结构体声明不能包含析构器或者协议声明。关于结构体的详细讨论和示例,请参阅 [类和结构体](../chapter2/09_Classes_and_Structures.md)。
|
||||||
|
|
||||||
结构体可以采纳任意数量的协议,但是不能继承自类、枚举或者其他结构体。
|
结构体可以采纳任意数量的协议,但是不能继承自类、枚举或者其他结构体。
|
||||||
|
|
||||||
有三种方法可以创建一个已声明的结构体实例:
|
有三种方法可以创建一个已声明的结构体实例:
|
||||||
|
|
||||||
* 调用结构体内声明的构造器,正如 [构造器](../chapter2/14_Initialization.html#initializers) 所述。
|
* 调用结构体内声明的构造器,正如 [构造器](../chapter2/14_Initialization.md#initializers) 所述。
|
||||||
|
|
||||||
* 如果没有声明构造器,调用结构体的成员逐一构造器,正如 [结构体类型的成员逐一构造器](../chapter2/14_Initialization.html#memberwise_initializers_for_structure_types) 所述。
|
* 如果没有声明构造器,调用结构体的成员逐一构造器,正如 [结构体类型的成员逐一构造器](../chapter2/14_Initialization.md#memberwise_initializers_for_structure_types) 所述。
|
||||||
|
|
||||||
* 如果没有声明构造器,而且结构体的所有属性都有初始值,调用结构体的默认构造器,正如 [默认构造器](../chapter2/14_Initialization.html#default_initializers) 所述。
|
* 如果没有声明构造器,而且结构体的所有属性都有初始值,调用结构体的默认构造器,正如 [默认构造器](../chapter2/14_Initialization.md#default_initializers) 所述。
|
||||||
|
|
||||||
结构体的构造过程请参阅 [构造过程](../chapter2/14_Initialization.html)。
|
结构体的构造过程请参阅 [构造过程](../chapter2/14_Initialization.md)。
|
||||||
|
|
||||||
结构体实例的属性可以用点语法(`.`)来访问,正如 [访问属性](../chapter2/09_Classes_and_Structures.html#accessing_properties) 所述。
|
结构体实例的属性可以用点语法(`.`)来访问,正如 [访问属性](../chapter2/09_Classes_and_Structures.md#accessing_properties) 所述。
|
||||||
|
|
||||||
结构体是值类型。结构体的实例在被赋予变量或常量,或传递给函数作为参数时会被复制。关于值类型的更多信息,请参阅
|
结构体是值类型。结构体的实例在被赋予变量或常量,或传递给函数作为参数时会被复制。关于值类型的更多信息,请参阅
|
||||||
[结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.html#structures_and_enumerations_are_value_types)。
|
[结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.md#structures_and_enumerations_are_value_types)。
|
||||||
|
|
||||||
可以使用扩展声明来扩展结构体类型的行为,请参阅 [扩展声明](#extension_declaration)。
|
可以使用扩展声明来扩展结构体类型的行为,请参阅 [扩展声明](#extension_declaration)。
|
||||||
|
|
||||||
@ -897,11 +897,11 @@ struct 结构体名称: 采纳的协议 {
|
|||||||
>
|
>
|
||||||
> <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.md#attributes)<sub>可选</sub> [*访问级别修饰符*](./#access-level-modifier) <sub>可选</sub> **struct** [*结构体名称*](#struct-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](./03_Types.md#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](./08_Generic_Parameters_and_Arguments.md#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.md#identifier)
|
||||||
>
|
>
|
||||||
> <a name="struct-body"></a>
|
> <a name="struct-body"></a>
|
||||||
>
|
>
|
||||||
@ -914,7 +914,7 @@ struct 结构体名称: 采纳的协议 {
|
|||||||
>
|
>
|
||||||
> <a name="struct-member"></a>
|
> <a name="struct-member"></a>
|
||||||
>
|
>
|
||||||
> *结构体成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
|
> *结构体成员* → [*声明*](./#declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="class_declaration"></a>
|
<a name="class_declaration"></a>
|
||||||
@ -929,7 +929,7 @@ class 类名: 超类, 采纳的协议 {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
类内可以包含零个或多个声明。这些声明可以包括存储型和计算型属性、实例方法、类型方法、构造器、唯一的析构器、下标、类型别名,甚至其他结构体、类和枚举声明。类声明不能包含协议声明。关于类的详细讨论和示例,请参阅 [类和结构体](../chapter2/09_Classes_and_Structures.html)。
|
类内可以包含零个或多个声明。这些声明可以包括存储型和计算型属性、实例方法、类型方法、构造器、唯一的析构器、下标、类型别名,甚至其他结构体、类和枚举声明。类声明不能包含协议声明。关于类的详细讨论和示例,请参阅 [类和结构体](../chapter2/09_Classes_and_Structures.md)。
|
||||||
|
|
||||||
一个类只能继承自一个超类,但是可以采纳任意数量的协议。超类紧跟在类名和冒号后面,其后跟着采纳的协议。泛型类可以继承自其它泛型类和非泛型类,但是非泛型类只能继承自其它非泛型类。当在冒号后面写泛型超类的名称时,必须写上泛型类的全名,包括它的泛型形参子句。
|
一个类只能继承自一个超类,但是可以采纳任意数量的协议。超类紧跟在类名和冒号后面,其后跟着采纳的协议。泛型类可以继承自其它泛型类和非泛型类,但是非泛型类只能继承自其它非泛型类。当在冒号后面写泛型超类的名称时,必须写上泛型类的全名,包括它的泛型形参子句。
|
||||||
|
|
||||||
@ -943,13 +943,13 @@ class 类名: 超类, 采纳的协议 {
|
|||||||
|
|
||||||
有两种方法来创建已声明的类的实例:
|
有两种方法来创建已声明的类的实例:
|
||||||
|
|
||||||
* 调用类中声明的构造器,请参阅 [构造器](../chapter2/14_Initialization.html#initializers)。
|
* 调用类中声明的构造器,请参阅 [构造器](../chapter2/14_Initialization.md#initializers)。
|
||||||
|
|
||||||
* 如果没有声明构造器,而且类的所有属性都被赋予了初始值,调用类的默认构造器,请参阅 [默认构造器](../chapter2/14_Initialization.html#default_initializers)。
|
* 如果没有声明构造器,而且类的所有属性都被赋予了初始值,调用类的默认构造器,请参阅 [默认构造器](../chapter2/14_Initialization.md#default_initializers)。
|
||||||
|
|
||||||
类实例属性可以用点语法(`.`)来访问,请参阅 [访问属性](../chapter2/09_Classes_and_Structures.html#accessing_properties)。
|
类实例属性可以用点语法(`.`)来访问,请参阅 [访问属性](../chapter2/09_Classes_and_Structures.md#accessing_properties)。
|
||||||
|
|
||||||
类是引用类型。当被赋予常量或变量,或者传递给函数作为参数时,类的实例会被引用,而不是被复制。关于引用类型的更多信息,请参阅 [结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.html#structures_and_enumerations_are_value_types)。
|
类是引用类型。当被赋予常量或变量,或者传递给函数作为参数时,类的实例会被引用,而不是被复制。关于引用类型的更多信息,请参阅 [结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.md#structures_and_enumerations_are_value_types)。
|
||||||
|
|
||||||
可以使用扩展声明来扩展类的行为,请参阅 [扩展声明](#extension_declaration)。
|
可以使用扩展声明来扩展类的行为,请参阅 [扩展声明](#extension_declaration)。
|
||||||
|
|
||||||
@ -959,13 +959,13 @@ class 类名: 超类, 采纳的协议 {
|
|||||||
>
|
>
|
||||||
> <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.md#attributes)<sub>可选</sub> [访问级别修饰符](./#access-level-modifier)<sub>可选</sub> **final**<sub>可选</sub> **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](./03_Types.md#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](./08_Generic_Parameters_and_Arguments.md#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.md#attributes)<sub>可选</sub> **final** [访问级别修饰符](./#access-level-modifier)<sub>可选</sub> **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](./03_Types.md#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](./08_Generic_Parameters_and_Arguments.md#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.md#identifier)
|
||||||
>
|
>
|
||||||
> <a name="class-body"></a>
|
> <a name="class-body"></a>
|
||||||
>
|
>
|
||||||
@ -976,7 +976,7 @@ class 类名: 超类, 采纳的协议 {
|
|||||||
>
|
>
|
||||||
> <a name="class-member"></a>
|
> <a name="class-member"></a>
|
||||||
>
|
>
|
||||||
> *类成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
|
> *类成员* → [*声明*](./#declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="protocol_declaration"></a>
|
<a name="protocol_declaration"></a>
|
||||||
@ -992,16 +992,16 @@ protocol 协议名称: 继承的协议 {
|
|||||||
|
|
||||||
协议的主体包含零个或多个协议成员声明,这些成员描述了任何采纳该协议的类型必须满足的一致性要求。一个协议可以声明采纳者必须实现的某些属性、方法、构造器以及下标。协议也可以声明各种各样的类型别名,叫做关联类型,它可以指定协议的不同声明之间的关系。协议声明不能包括类、结构体、枚举或者其它协议的声明。协议成员声明会在后面进行讨论。
|
协议的主体包含零个或多个协议成员声明,这些成员描述了任何采纳该协议的类型必须满足的一致性要求。一个协议可以声明采纳者必须实现的某些属性、方法、构造器以及下标。协议也可以声明各种各样的类型别名,叫做关联类型,它可以指定协议的不同声明之间的关系。协议声明不能包括类、结构体、枚举或者其它协议的声明。协议成员声明会在后面进行讨论。
|
||||||
|
|
||||||
协议类型可以继承自任意数量的其它协议。当一个协议类型继承自其它协议的时候,来自其它协议的所有要求会聚合在一起,而且采纳当前协议的类型必须符合所有的这些要求。关于如何使用协议继承的例子,请参阅 [协议继承](../chapter2/21_Protocols.html#protocol_inheritance)。
|
协议类型可以继承自任意数量的其它协议。当一个协议类型继承自其它协议的时候,来自其它协议的所有要求会聚合在一起,而且采纳当前协议的类型必须符合所有的这些要求。关于如何使用协议继承的例子,请参阅 [协议继承](../chapter2/21_Protocols.md#protocol_inheritance)。
|
||||||
|
|
||||||
> 注意
|
> 注意
|
||||||
>
|
>
|
||||||
> 也可以使用协议合成类型来聚合多个协议的一致性要求,请参阅 [协议合成类型](03_Types.html#protocol_composition_type) 和 [协议合成](../chapter2/21_Protocols.html#protocol_composition)。
|
> 也可以使用协议合成类型来聚合多个协议的一致性要求,请参阅 [协议合成类型](./03_Types.md#protocol_composition_type) 和 [协议合成](../chapter2/21_Protocols.md#protocol_composition)。
|
||||||
>
|
>
|
||||||
|
|
||||||
可以通过类型的扩展声明来采纳协议,从而为之前声明的类型添加协议一致性。在扩展中,必须实现所有采纳协议的要求。如果该类型已经实现了所有的要求,可以让这个扩展声明的主体留空。
|
可以通过类型的扩展声明来采纳协议,从而为之前声明的类型添加协议一致性。在扩展中,必须实现所有采纳协议的要求。如果该类型已经实现了所有的要求,可以让这个扩展声明的主体留空。
|
||||||
|
|
||||||
默认地,符合某个协议的类型必须实现所有在协议中声明的属性、方法和下标。即便如此,可以用 `optional` 声明修饰符标注协议成员声明,以指定它们的实现是可选的。`optional` 修饰符仅仅可以用于使用 `objc` 特性标记过的协议。因此,仅仅类类型可以采用并符合包含可选成员要求的协议。更多关于如何使用 `optional` 声明修饰符的信息,以及如何访问可选协议成员的指导——例如不能确定采纳协议的类型是否实现了它们时——请参阅 [可选协议要求](../chapter2/21_Protocols.html#optional_protocol_requirements)
|
默认地,符合某个协议的类型必须实现所有在协议中声明的属性、方法和下标。即便如此,可以用 `optional` 声明修饰符标注协议成员声明,以指定它们的实现是可选的。`optional` 修饰符仅仅可以用于使用 `objc` 特性标记过的协议。因此,仅仅类类型可以采用并符合包含可选成员要求的协议。更多关于如何使用 `optional` 声明修饰符的信息,以及如何访问可选协议成员的指导——例如不能确定采纳协议的类型是否实现了它们时——请参阅 [可选协议要求](../chapter2/21_Protocols.md#optional_protocol_requirements)
|
||||||
|
|
||||||
为了限制协议只能被类类型采纳,需要使用 `AnyObject` 关键字来标记协议,将 `AnyObject` 关键在写在冒号后面的继承的协议列表的首位。例如,下面的协议只能被类类型采纳:
|
为了限制协议只能被类类型采纳,需要使用 `AnyObject` 关键字来标记协议,将 `AnyObject` 关键在写在冒号后面的继承的协议列表的首位。例如,下面的协议只能被类类型采纳:
|
||||||
|
|
||||||
@ -1018,9 +1018,9 @@ protocol SomeProtocol: AnyObject {
|
|||||||
> 如果协议已经用 `objc` 特性标记了,`AnyObject` 要求就隐式地应用于该协议,无需显式使用 `AnyObject` 关键字。
|
> 如果协议已经用 `objc` 特性标记了,`AnyObject` 要求就隐式地应用于该协议,无需显式使用 `AnyObject` 关键字。
|
||||||
>
|
>
|
||||||
|
|
||||||
协议类型是命名的类型,因此它们可以像其他命名类型一样使用,正如 [协议作为类型](../chapter2/21_Protocols.html#protocols_as_types) 所讨论的。然而,不能构造一个协议的实例,因为协议实际上不提供它们指定的要求的实现。
|
协议类型是命名的类型,因此它们可以像其他命名类型一样使用,正如 [协议作为类型](../chapter2/21_Protocols.md#protocols_as_types) 所讨论的。然而,不能构造一个协议的实例,因为协议实际上不提供它们指定的要求的实现。
|
||||||
|
|
||||||
可以使用协议来声明作为代理的类或者结构体应该实现的方法,正如 [委托(代理)模式](../chapter2/21_Protocols.html#delegation) 中所述。
|
可以使用协议来声明作为代理的类或者结构体应该实现的方法,正如 [委托(代理)模式](../chapter2/21_Protocols.md#delegation) 中所述。
|
||||||
|
|
||||||
<a name="grammer_of_a_protocol_declaration"></a>
|
<a name="grammer_of_a_protocol_declaration"></a>
|
||||||
> 协议声明语法
|
> 协议声明语法
|
||||||
@ -1028,11 +1028,11 @@ protocol SomeProtocol: AnyObject {
|
|||||||
>
|
>
|
||||||
> <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.md#attributes)<sub>可选</sub> [*访问级别修饰符*](./#access-level-modifier)<sub>可选</sub> **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03_Types.md#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](./08_Generic_Parameters_and_Arguments.md#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.md#identifier)
|
||||||
>
|
>
|
||||||
> <a name="protocol-body"></a>
|
> <a name="protocol-body"></a>
|
||||||
>
|
>
|
||||||
@ -1043,7 +1043,7 @@ protocol SomeProtocol: AnyObject {
|
|||||||
>
|
>
|
||||||
> <a name="protocol-member"></a>
|
> <a name="protocol-member"></a>
|
||||||
>
|
>
|
||||||
> *协议成员* → [*协议成员声明*](#protocol-member-declaration) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
|
> *协议成员* → [*协议成员声明*](./#protocol-member-declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||||
>
|
>
|
||||||
>
|
>
|
||||||
> <a name="protocol-member-declaration"></a>
|
> <a name="protocol-member-declaration"></a>
|
||||||
@ -1074,7 +1074,7 @@ var 属性名: 类型 { get set }
|
|||||||
|
|
||||||
同其它协议成员声明一样,这些属性声明仅仅针对符合该协议的类型声明了 getter 和 setter 要求,你不能在协议中直接实现 getter 和 setter。
|
同其它协议成员声明一样,这些属性声明仅仅针对符合该协议的类型声明了 getter 和 setter 要求,你不能在协议中直接实现 getter 和 setter。
|
||||||
|
|
||||||
符合类型可以通过多种方式满足 getter 和 setter 要求。如果属性声明包含 `get` 和 `set` 关键字,符合类型就可以用存储型变量属性或可读可写的计算型属性来满足此要求,但是属性不能以常量属性或只读计算型属性实现。如果属性声明仅仅包含 `get` 关键字的话,它可以作为任意类型的属性被实现。关于如何实现协议中的属性要求的例子,请参阅 [属性要求](../chapter2/21_Protocols.html#property_requirements)
|
符合类型可以通过多种方式满足 getter 和 setter 要求。如果属性声明包含 `get` 和 `set` 关键字,符合类型就可以用存储型变量属性或可读可写的计算型属性来满足此要求,但是属性不能以常量属性或只读计算型属性实现。如果属性声明仅仅包含 `get` 关键字的话,它可以作为任意类型的属性被实现。关于如何实现协议中的属性要求的例子,请参阅 [属性要求](../chapter2/21_Protocols.md#property_requirements)
|
||||||
|
|
||||||
另请参阅 [变量声明](#variable_declaration)。
|
另请参阅 [变量声明](#variable_declaration)。
|
||||||
|
|
||||||
@ -1083,13 +1083,13 @@ 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.md#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="protocol_method_declaration"></a>
|
<a name="protocol_method_declaration"></a>
|
||||||
### 协议方法声明
|
### 协议方法声明
|
||||||
|
|
||||||
协议可以通过在协议声明主体中引入一个协议方法声明,来声明符合的类型必须实现的方法。协议方法声明和函数方法声明有着相同的形式,但有两项例外:它们不包括函数体,也不能包含默认参数。关于如何实现协议中的方法要求的例子,请参阅 [方法要求](../chapter2/21_Protocols.html#method_requirements)。
|
协议可以通过在协议声明主体中引入一个协议方法声明,来声明符合的类型必须实现的方法。协议方法声明和函数方法声明有着相同的形式,但有两项例外:它们不包括函数体,也不能包含默认参数。关于如何实现协议中的方法要求的例子,请参阅 [方法要求](../chapter2/21_Protocols.md#method_requirements)。
|
||||||
|
|
||||||
使用 `static` 声明修饰符可以在协议声明中声明一个类型方法。类在实现这些方法时使用 `class` 声明修饰符。结构体实现这些方法时必须使用 `static` 声明修饰符。通过扩展实现时亦是如此(类的扩展中使用 `class` 声明修饰符,结构体的扩展中使用 `static` 声明修饰符)。
|
使用 `static` 声明修饰符可以在协议声明中声明一个类型方法。类在实现这些方法时使用 `class` 声明修饰符。结构体实现这些方法时必须使用 `static` 声明修饰符。通过扩展实现时亦是如此(类的扩展中使用 `class` 声明修饰符,结构体的扩展中使用 `static` 声明修饰符)。
|
||||||
|
|
||||||
@ -1100,7 +1100,7 @@ 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.md#generic-parameter-clause)<sub>可选</sub> [*函数签名*](./#function-signature) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="protocol_initializer_declaration"></a>
|
<a name="protocol_initializer_declaration"></a>
|
||||||
@ -1121,9 +1121,9 @@ 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.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](./#parameter-clause) **throws**<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#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.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](./#parameter-clause) **rethrows** [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="protocol_subscript_declaration"></a>
|
<a name="protocol_subscript_declaration"></a>
|
||||||
@ -1145,14 +1145,14 @@ subscript (参数列表) -> 返回类型 { get set }
|
|||||||
>
|
>
|
||||||
> <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.md#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>
|
||||||
|
|
||||||
### 协议关联类型声明
|
### 协议关联类型声明
|
||||||
|
|
||||||
使用关键字 `associatedtype` 来声明协议关联类型。关联类型为作为协议声明的一部分,为某种类型提供了一个别名。关联类型和泛型参数子句中的类型参数很相似,但是它们和 `Self` 一样,用于协议中。`Self` 指代采纳协议的类型。要获得更多信息和例子,请参阅 [关联类型](../chapter2/22_Generics.html#associated_types)。
|
使用关键字 `associatedtype` 来声明协议关联类型。关联类型为作为协议声明的一部分,为某种类型提供了一个别名。关联类型和泛型参数子句中的类型参数很相似,但是它们和 `Self` 一样,用于协议中。`Self` 指代采纳协议的类型。要获得更多信息和例子,请参阅 [关联类型](../chapter2/22_Generics.md#associated_types)。
|
||||||
|
|
||||||
在协议声明中使用泛型 `where` 子句来为继承的协议关联类型添加约束,且不需要重新声明关联类型。例如下面代码中的 `SubProtocol` 声明。
|
在协议声明中使用泛型 `where` 子句来为继承的协议关联类型添加约束,且不需要重新声明关联类型。例如下面代码中的 `SubProtocol` 声明。
|
||||||
|
|
||||||
@ -1179,7 +1179,7 @@ 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.md#attributes)<sub>可选</sub> [*访问级别修饰符*](./#access-level-modifier)<sub>可选</sub> **associatedtype** [*类型别名头*](#typealias-head) [*类型继承子句*](03_Types.md#type-inheritance-clause)<sub>可选</sub> [*类型别名赋值*](./#typealias-assignment)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="initializer_declaration"></a>
|
<a name="initializer_declaration"></a>
|
||||||
@ -1187,7 +1187,7 @@ protocol SubProtocolB: SomeProtocol where SomeType: Equatable { }
|
|||||||
|
|
||||||
构造器声明会为程序中的类、结构体或枚举引入构造器。构造器使用关键字 `init` 来声明,有两种基本形式。
|
构造器声明会为程序中的类、结构体或枚举引入构造器。构造器使用关键字 `init` 来声明,有两种基本形式。
|
||||||
|
|
||||||
结构体、枚举、类可以有任意数量的构造器,但是类的构造器具有不同的规则和行为。不同于结构体和枚举,类有两种构造器,即指定构造器和便利构造器,请参阅 [构造过程](../chapter2/14_Initialization.html)。
|
结构体、枚举、类可以有任意数量的构造器,但是类的构造器具有不同的规则和行为。不同于结构体和枚举,类有两种构造器,即指定构造器和便利构造器,请参阅 [构造过程](../chapter2/14_Initialization.md)。
|
||||||
|
|
||||||
采用如下形式声明结构体和枚举的构造器,以及类的指定构造器:
|
采用如下形式声明结构体和枚举的构造器,以及类的指定构造器:
|
||||||
|
|
||||||
@ -1226,7 +1226,7 @@ convenience init(参数列表) {
|
|||||||
|
|
||||||
就像函数和方法,构造器也可以抛出或者重抛错误,你可以在构造器参数列表的圆括号之后使用 `throws` 或 `rethrows` 关键字来表明相应的抛出行为。
|
就像函数和方法,构造器也可以抛出或者重抛错误,你可以在构造器参数列表的圆括号之后使用 `throws` 或 `rethrows` 关键字来表明相应的抛出行为。
|
||||||
|
|
||||||
关于在不同类型中声明构造器的例子,请参阅 [构造过程](../chapter2/14_Initialization.html)。
|
关于在不同类型中声明构造器的例子,请参阅 [构造过程](../chapter2/14_Initialization.md)。
|
||||||
|
|
||||||
<a name="failable_initializers"></a>
|
<a name="failable_initializers"></a>
|
||||||
### 可失败构造器
|
### 可失败构造器
|
||||||
@ -1267,24 +1267,24 @@ if let actualInstance = SomeStruct(input: "Hello") {
|
|||||||
|
|
||||||
子类可以用任意种类的指定构造器重写超类的可失败指定构造器,但是只能用非可失败指定构造器重写超类的非可失败指定构造器。
|
子类可以用任意种类的指定构造器重写超类的可失败指定构造器,但是只能用非可失败指定构造器重写超类的非可失败指定构造器。
|
||||||
|
|
||||||
更多关于可失败构造器的信息和例子,请参阅 [可失败构造器](../chapter2/14_Initialization.html#failable_initializers)。
|
更多关于可失败构造器的信息和例子,请参阅 [可失败构造器](../chapter2/14_Initialization.md#failable_initializers)。
|
||||||
|
|
||||||
<a name="grammer_of_an_initializer_declaration"></a>
|
<a name="grammer_of_an_initializer_declaration"></a>
|
||||||
> 构造器声明语法
|
> 构造器声明语法
|
||||||
>
|
>
|
||||||
> <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.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](./#parameter-clause) **throws**<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#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.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](./#parameter-clause) **rethrows**<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#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.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init**
|
||||||
>
|
>
|
||||||
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **?**
|
> *构造器头* → [*特性列表*](./06_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **?**
|
||||||
>
|
>
|
||||||
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **!**
|
> *构造器头* → [*特性列表*](./06_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **!**
|
||||||
>
|
>
|
||||||
> <a name="initializer-body"></a>
|
> <a name="initializer-body"></a>
|
||||||
>
|
>
|
||||||
@ -1308,14 +1308,14 @@ deinit {
|
|||||||
|
|
||||||
析构器不能直接调用。
|
析构器不能直接调用。
|
||||||
|
|
||||||
关于如何在类声明中使用析构器的例子,请参阅 [析构过程](../chapter2/15_Deinitialization.html)。
|
关于如何在类声明中使用析构器的例子,请参阅 [析构过程](../chapter2/15_Deinitialization.md)。
|
||||||
|
|
||||||
<a name="grammer_of_a_deinitializer_declaration"></a>
|
<a name="grammer_of_a_deinitializer_declaration"></a>
|
||||||
> 析构器声明语法
|
> 析构器声明语法
|
||||||
>
|
>
|
||||||
> <a name="deinitializer-declaration"></a>
|
> <a name="deinitializer-declaration"></a>
|
||||||
>
|
>
|
||||||
> *析构器声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block)
|
> *析构器声明* → [*特性列表*](./06_Attributes.md#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="extension_declaration"></a>
|
<a name="extension_declaration"></a>
|
||||||
@ -1329,7 +1329,7 @@ extension 类型名称 where 要求 {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
扩展声明体可包含零个或多个声明语句。这些声明语句可以包括计算型属性、计算型类型属性、实例方法、类型方法、构造器、下标声明,甚至是类、结构体和枚举声明。扩展声明不能包含析构器、协议声明、存储型属性、属性观察器或其他扩展声明。关于扩展声明的详细讨论,以及各种扩展声明的例子,请参阅 [扩展](../chapter2/20_Extensions.html)。
|
扩展声明体可包含零个或多个声明语句。这些声明语句可以包括计算型属性、计算型类型属性、实例方法、类型方法、构造器、下标声明,甚至是类、结构体和枚举声明。扩展声明不能包含析构器、协议声明、存储型属性、属性观察器或其他扩展声明。关于扩展声明的详细讨论,以及各种扩展声明的例子,请参阅 [扩展](../chapter2/20_Extensions.md)。
|
||||||
|
|
||||||
如果类型为类,结构体,或枚举类型,则扩展声明会扩展相应的类型。如果类型为协议类型,则扩展声明会扩展所有遵守这个协议的类型。在扩展的协议体中声明语句不能使用 `final` 标识符。
|
如果类型为类,结构体,或枚举类型,则扩展声明会扩展相应的类型。如果类型为协议类型,则扩展声明会扩展所有遵守这个协议的类型。在扩展的协议体中声明语句不能使用 `final` 标识符。
|
||||||
|
|
||||||
@ -1516,7 +1516,7 @@ extension Array: Loggable where Element: MarkedLoggable { }
|
|||||||
>
|
>
|
||||||
> <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.md#type_attributes)<sub>可选</sub> [访问级别修饰符](./#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.md#type-identifier) [*类型-继承-子句*](./03_Types.md#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](./08_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub> [*扩展主体*](#extension-body)
|
||||||
>
|
>
|
||||||
> <a name="extension-body"></a>
|
> <a name="extension-body"></a>
|
||||||
>
|
>
|
||||||
@ -1524,7 +1524,7 @@ extension Array: Loggable where Element: MarkedLoggable { }
|
|||||||
>
|
>
|
||||||
> *多条声明* → [单条声明](#subscript_declaration) [多条声明](#declarations) <sub>可选</sub>
|
> *多条声明* → [单条声明](#subscript_declaration) [多条声明](#declarations) <sub>可选</sub>
|
||||||
>
|
>
|
||||||
> *单条声明* → [声明语句](#declarations) | [*编译控制流语句*](05_Statements.html#compiler-control-statement)
|
> *单条声明* → [声明语句](./#declarations) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="subscript_declaration"></a>
|
<a name="subscript_declaration"></a>
|
||||||
@ -1559,7 +1559,7 @@ subscript (参数列表) -> 返回类型 {
|
|||||||
|
|
||||||
同样可以在协议声明中声明下标,正如 [协议下标声明](#protocol_subscript_declaration) 中所述。
|
同样可以在协议声明中声明下标,正如 [协议下标声明](#protocol_subscript_declaration) 中所述。
|
||||||
|
|
||||||
更多关于下标的信息和例子,请参阅 [下标](../chapter2/12_Subscripts.html)。
|
更多关于下标的信息和例子,请参阅 [下标](../chapter2/12_Subscripts.md)。
|
||||||
|
|
||||||
<a name="grammer_of_a_subscript_declaration"></a>
|
<a name="grammer_of_a_subscript_declaration"></a>
|
||||||
> 下标声明语法
|
> 下标声明语法
|
||||||
@ -1567,19 +1567,19 @@ subscript (参数列表) -> 返回类型 {
|
|||||||
>
|
>
|
||||||
> <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.md#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.md#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.md#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.md#attributes)<sub>可选</sub> [*声明修饰符列表*](./#declaration-modifiers)<sub>可选</sub> **subscript** [*泛型参数子句*](08_Generic_Parameters_and_Arguments.md#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.md#attributes)<sub>可选</sub> [*类型*](./03_Types.md#type)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="operator_declaration"></a>
|
<a name="operator_declaration"></a>
|
||||||
@ -1589,7 +1589,7 @@ subscript (参数列表) -> 返回类型 {
|
|||||||
|
|
||||||
可以声明三种不同的缀性:中缀、前缀和后缀。运算符的缀性指定了运算符与其运算对象的相对位置。
|
可以声明三种不同的缀性:中缀、前缀和后缀。运算符的缀性指定了运算符与其运算对象的相对位置。
|
||||||
|
|
||||||
运算符声明有三种基本形式,每种缀性各一种。运算符的缀性通过在 `operator` 关键字之前添加声明修饰符 `infix`,`prefix` 或 `postfix` 来指定。每种形式中,运算符的名字只能包含 [运算符](02_Lexical_Structure.html#operators) 中定义的运算符字符。
|
运算符声明有三种基本形式,每种缀性各一种。运算符的缀性通过在 `operator` 关键字之前添加声明修饰符 `infix`,`prefix` 或 `postfix` 来指定。每种形式中,运算符的名字只能包含 [运算符](./02_Lexical_Structure.md#operators) 中定义的运算符字符。
|
||||||
|
|
||||||
下面的形式声明了一个新的中缀运算符:
|
下面的形式声明了一个新的中缀运算符:
|
||||||
|
|
||||||
@ -1621,7 +1621,7 @@ postfix operator 运算符名称 {}
|
|||||||
|
|
||||||
和前缀运算符一样,后缀运算符的声明中不指定优先级,而且后缀运算符是非结合的。
|
和前缀运算符一样,后缀运算符的声明中不指定优先级,而且后缀运算符是非结合的。
|
||||||
|
|
||||||
声明了一个新的运算符以后,需要实现一个跟这个运算符同名的函数来实现这个运算符。如果是实现一个前缀或者后缀运算符,也必须使用相符的 `prefix` 或者 `postfix` 声明修饰符标记函数声明。如果是实现中缀运算符,则不需要使用 `infix` 声明修饰符标记函数声明。关于如何实现一个新的运算符的例子,请参阅 [自定义运算符](../chapter2/25_Advanced_Operators.html#custom_operators)。
|
声明了一个新的运算符以后,需要实现一个跟这个运算符同名的函数来实现这个运算符。如果是实现一个前缀或者后缀运算符,也必须使用相符的 `prefix` 或者 `postfix` 声明修饰符标记函数声明。如果是实现中缀运算符,则不需要使用 `infix` 声明修饰符标记函数声明。关于如何实现一个新的运算符的例子,请参阅 [自定义运算符](../chapter2/25_Advanced_Operators.md#custom_operators)。
|
||||||
|
|
||||||
<a name="grammer_of_an_operator_declaration"></a>
|
<a name="grammer_of_an_operator_declaration"></a>
|
||||||
> 运算符声明语法
|
> 运算符声明语法
|
||||||
@ -1630,7 +1630,7 @@ postfix operator 运算符名称 {}
|
|||||||
> *运算符声明* → [*前缀运算符声明*](#prefix-operator-declaration) | [*后缀运算符声明*](#postfix-operator-declaration) | [*中缀运算符声明*](#infix-operator-declaration)
|
> *运算符声明* → [*前缀运算符声明*](#prefix-operator-declaration) | [*后缀运算符声明*](#postfix-operator-declaration) | [*中缀运算符声明*](#infix-operator-declaration)
|
||||||
>
|
>
|
||||||
<a name="prefix-operator-declaration"></a>
|
<a name="prefix-operator-declaration"></a>
|
||||||
> *前缀运算符声明* → **prefix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** **}**
|
> *前缀运算符声明* → **prefix** **运算符** [*运算符*](./02_Lexical_Structure.md#operator) **{** **}**
|
||||||
>
|
>
|
||||||
> <a name="postfix-operator-declaration"></a>
|
> <a name="postfix-operator-declaration"></a>
|
||||||
>
|
>
|
||||||
@ -1638,7 +1638,7 @@ postfix 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.md#operator) **{** [*中缀运算符属性*](#infix-operator-attributes)<sub>可选</sub> **}**
|
||||||
>
|
>
|
||||||
<a name="infix-operator-group"></a>
|
<a name="infix-operator-group"></a>
|
||||||
> *中缀运算符组* → [*优先级组名称*](#precedence-group-name)
|
> *中缀运算符组* → [*优先级组名称*](#precedence-group-name)
|
||||||
@ -1696,7 +1696,7 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
|||||||
>
|
>
|
||||||
> <a name="precedence-group-assignment"></a>
|
> <a name="precedence-group-assignment"></a>
|
||||||
>
|
>
|
||||||
> *优先级组赋值* → **assignment:**[*布尔字面值*](02_Lexical_Structure.html#boolean-literal)
|
> *优先级组赋值* → **assignment:**[*布尔字面值*](./02_Lexical_Structure.md#boolean-literal)
|
||||||
>
|
>
|
||||||
<a name="precedence-group-associativity"></a>
|
<a name="precedence-group-associativity"></a>
|
||||||
> *优先级组结合性* → **associativity:left**
|
> *优先级组结合性* → **associativity:left**
|
||||||
@ -1709,7 +1709,7 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
|||||||
> *多优先级组名称* → [*优先级组名称*](#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.md#identifier)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="Declaration_Modifiers"></a>
|
<a name="Declaration_Modifiers"></a>
|
||||||
@ -1729,13 +1729,13 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
|||||||
|
|
||||||
`lazy`
|
`lazy`
|
||||||
|
|
||||||
该修饰符用于修饰类或结构体中的存储型变量属性,表示该属性的初始值最多只被计算和存储一次,且发生在它被第一次访问时。关于如何使用 `lazy` 修饰符的例子,请参阅 [惰性存储型属性](../chapter2/10_Properties.html#lazy_stored_properties)。
|
该修饰符用于修饰类或结构体中的存储型变量属性,表示该属性的初始值最多只被计算和存储一次,且发生在它被第一次访问时。关于如何使用 `lazy` 修饰符的例子,请参阅 [惰性存储型属性](../chapter2/10_Properties.md#lazy_stored_properties)。
|
||||||
|
|
||||||
`optional`
|
`optional`
|
||||||
|
|
||||||
该修饰符用于修饰协议中的属性、方法以及下标成员,表示符合类型可以不实现这些成员要求。
|
该修饰符用于修饰协议中的属性、方法以及下标成员,表示符合类型可以不实现这些成员要求。
|
||||||
|
|
||||||
只能将 `optional` 修饰符用于被 `objc` 特性标记的协议。这样一来,就只有类类型可以采纳并符合拥有可选成员要求的协议。关于如何使用 `optional` 修饰符,以及如何访问可选协议成员(比如,不确定符合类型是否已经实现了这些可选成员)的信息,请参阅 [可选协议要求](../chapter2/22_Protocols.html#optional_protocol_requirements)。
|
只能将 `optional` 修饰符用于被 `objc` 特性标记的协议。这样一来,就只有类类型可以采纳并符合拥有可选成员要求的协议。关于如何使用 `optional` 修饰符,以及如何访问可选协议成员(比如,不确定符合类型是否已经实现了这些可选成员)的信息,请参阅 [可选协议要求](../chapter2/22_Protocols.md#optional_protocol_requirements)。
|
||||||
|
|
||||||
`required`
|
`required`
|
||||||
|
|
||||||
@ -1743,7 +1743,7 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
|||||||
|
|
||||||
`unowned`
|
`unowned`
|
||||||
|
|
||||||
该修饰符用于修饰存储型变量、常量或者存储型变量属性,表示该变量或属性持有其存储对象的无主引用。如果在此存储对象释放后尝试访问该对象,会引发运行时错误。如同弱引用一样,该引用类型的变量或属性必须是类类型。与弱引用不同的是,这种类型的变量或属性是非可选的。关于 `unowned` 更多的信息和例子,请参阅 [无主引用](../chapter2/16_Automatic_Reference_Counting.html#unowned_references)
|
该修饰符用于修饰存储型变量、常量或者存储型变量属性,表示该变量或属性持有其存储对象的无主引用。如果在此存储对象释放后尝试访问该对象,会引发运行时错误。如同弱引用一样,该引用类型的变量或属性必须是类类型。与弱引用不同的是,这种类型的变量或属性是非可选的。关于 `unowned` 更多的信息和例子,请参阅 [无主引用](../chapter2/16_Automatic_Reference_Counting.md#unowned_references)
|
||||||
|
|
||||||
`unowned(safe)`
|
`unowned(safe)`
|
||||||
|
|
||||||
@ -1755,12 +1755,12 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
|||||||
|
|
||||||
`weak`
|
`weak`
|
||||||
|
|
||||||
该修饰符用于修饰变量或存储型变量属性,表示该变量或属性持有其存储的对象的弱引用。这种变量或属性的类型必须是可选的类类型。使用 `weak` 修饰符可避免强引用循环。关于 `weak` 修饰符的更多信息和例子,请参阅 [弱引用](../chapter2/16_Automatic_Reference_Counting.html#resolving_strong_reference_cycles_between_class_instances)。
|
该修饰符用于修饰变量或存储型变量属性,表示该变量或属性持有其存储的对象的弱引用。这种变量或属性的类型必须是可选的类类型。使用 `weak` 修饰符可避免强引用循环。关于 `weak` 修饰符的更多信息和例子,请参阅 [弱引用](../chapter2/16_Automatic_Reference_Counting.md#resolving_strong_reference_cycles_between_class_instances)。
|
||||||
|
|
||||||
<a name="access_control_levels"></a>
|
<a name="access_control_levels"></a>
|
||||||
### 访问控制级别
|
### 访问控制级别
|
||||||
|
|
||||||
Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`。可以使用以下任意一种访问级别修饰符来指定声明的访问级别。访问控制在 [访问控制](../chapter2/24_Access_Control.html) 中有详细讨论。
|
Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`。可以使用以下任意一种访问级别修饰符来指定声明的访问级别。访问控制在 [访问控制](../chapter2/24_Access_Control.md) 中有详细讨论。
|
||||||
|
|
||||||
`public`
|
`public`
|
||||||
|
|
||||||
@ -1774,7 +1774,7 @@ Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`
|
|||||||
|
|
||||||
该修饰符表示声明只能被所在源文件的代码访问。
|
该修饰符表示声明只能被所在源文件的代码访问。
|
||||||
|
|
||||||
以上访问级别修饰符都可以选择带上一个参数,该参数由一对圆括号和其中的 `set` 关键字组成(例如,`private(set)`)。使用这种形式的访问级别修饰符来限制某个属性或下标的 setter 的访问级别低于其本身的访问级别,正如 [Getter 和 Setter](../chapter2/24_Access_Control.html#getters_and_setters) 中所讨论的。
|
以上访问级别修饰符都可以选择带上一个参数,该参数由一对圆括号和其中的 `set` 关键字组成(例如,`private(set)`)。使用这种形式的访问级别修饰符来限制某个属性或下标的 setter 的访问级别低于其本身的访问级别,正如 [Getter 和 Setter](../chapter2/24_Access_Control.md#getters_and_setters) 中所讨论的。
|
||||||
|
|
||||||
<a name="grammer_of_a_declaration_modifier"></a>
|
<a name="grammer_of_a_declaration_modifier"></a>
|
||||||
> 声明修饰符的语法
|
> 声明修饰符的语法
|
||||||
|
|||||||
@ -279,7 +279,7 @@ NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
|
|||||||
|
|
||||||
如果你将 `objc` 特性应用于枚举,每一个枚举用例都会以枚举名称和用例名称组合的方式暴露在 Objective-C 代码中。例如,在 `Planet` 枚举中有一个名为 `Venus` 的用例,该用例暴露在 Objective-C 代码中时叫做 `PlanetVenus`。
|
如果你将 `objc` 特性应用于枚举,每一个枚举用例都会以枚举名称和用例名称组合的方式暴露在 Objective-C 代码中。例如,在 `Planet` 枚举中有一个名为 `Venus` 的用例,该用例暴露在 Objective-C 代码中时叫做 `PlanetVenus`。
|
||||||
|
|
||||||
`objc` 特性有一个可选的参数,由标识符构成。当你想把 `objc` 所修饰的实体以一个不同的名字暴露给 Objective-C 时,你就可以使用这个特性参数。你可以使用这个参数来命名类、枚举类型、枚举用例、协议、方法、存取方法以及构造器。如果你要指定类、协议或枚举在 Objective-C 下的名称,在名称上包含三个字母的前缀,如 [Objective-C 编程](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011210) 中的 [约定](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html#//apple_ref/doc/uid/TP40011210-CH10-SW1)。下面的例子把 `ExampleClass` 中的 `enabled` 属性的取值方法暴露给 Objective-C,名字是 `isEnabled`,而不是它原来的属性名。
|
`objc` 特性有一个可选的参数,由标识符构成。当你想把 `objc` 所修饰的实体以一个不同的名字暴露给 Objective-C 时,你就可以使用这个特性参数。你可以使用这个参数来命名类、枚举类型、枚举用例、协议、方法、存取方法以及构造器。如果你要指定类、协议或枚举在 Objective-C 下的名称,在名称上包含三个字母的前缀,如 [Objective-C 编程](./https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.md#//apple_ref/doc/uid/TP40011210) 中的 [约定](./https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.md#//apple_ref/doc/uid/TP40011210-CH10-SW1)。下面的例子把 `ExampleClass` 中的 `enabled` 属性的取值方法暴露给 Objective-C,名字是 `isEnabled`,而不是它原来的属性名。
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
class ExampleClass: NSObject {
|
class ExampleClass: NSObject {
|
||||||
@ -349,7 +349,7 @@ class ExampleClass: NSObject {
|
|||||||
<a name="autoclosure"></a>
|
<a name="autoclosure"></a>
|
||||||
### `autoclosure`
|
### `autoclosure`
|
||||||
|
|
||||||
这个特性通过把表达式自动封装成无参数的闭包来延迟表达式的计算。它可以修饰类型为返回表达式结果类型的无参数函数类型的函数参数。关于如何使用 autoclosure 特性的例子,请参阅 [自动闭包](https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID543) 和 [函数类型](https://docs.swift.org/swift-book/ReferenceManual/Types.html#ID449)。
|
这个特性通过把表达式自动封装成无参数的闭包来延迟表达式的计算。它可以修饰类型为返回表达式结果类型的无参数函数类型的函数参数。关于如何使用 autoclosure 特性的例子,请参阅 [自动闭包](./https://docs.swift.org/swift-book/LanguageGuide/Closures.md#ID543) 和 [函数类型](./https://docs.swift.org/swift-book/ReferenceManual/Types.md#ID449)。
|
||||||
|
|
||||||
<a name="convention"></a>
|
<a name="convention"></a>
|
||||||
### `convention`
|
### `convention`
|
||||||
@ -369,7 +369,7 @@ convention 特性总是与下面的参数之一一起出现。
|
|||||||
<a name="escaping"></a>
|
<a name="escaping"></a>
|
||||||
### `escaping`
|
### `escaping`
|
||||||
|
|
||||||
在函数或者方法声明上使用该特性,它表示参数将不会被存储以供延迟执行,这将确保参数不会超出函数调用的生命周期。在使用 `escaping` 声明特性的函数类型中访问属性和方法时不需要显式地使用 `self.`。关于如何使用 `escaping` 特性的例子,请参阅 [逃逸闭包](https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID546)。
|
在函数或者方法声明上使用该特性,它表示参数将不会被存储以供延迟执行,这将确保参数不会超出函数调用的生命周期。在使用 `escaping` 声明特性的函数类型中访问属性和方法时不需要显式地使用 `self.`。关于如何使用 `escaping` 特性的例子,请参阅 [逃逸闭包](./https://docs.swift.org/swift-book/LanguageGuide/Closures.md#ID546)。
|
||||||
|
|
||||||
<a name="switch_case_attributes"></a>
|
<a name="switch_case_attributes"></a>
|
||||||
## Switch Case 特性
|
## Switch Case 特性
|
||||||
@ -378,7 +378,7 @@ convention 特性总是与下面的参数之一一起出现。
|
|||||||
|
|
||||||
### `unknown`
|
### `unknown`
|
||||||
|
|
||||||
次特性用于 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.md#ID602)。
|
||||||
|
|
||||||
> 特性语法
|
> 特性语法
|
||||||
>
|
>
|
||||||
@ -389,7 +389,7 @@ convention 特性总是与下面的参数之一一起出现。
|
|||||||
>
|
>
|
||||||
> <a name="attribute_name"></a>
|
> <a name="attribute_name"></a>
|
||||||
>
|
>
|
||||||
> *特性名* → [标识符](02_Lexical_Structure.html#identifier)
|
> *特性名* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||||
>
|
>
|
||||||
> <a name="atribute_argument_clause"></a>
|
> <a name="atribute_argument_clause"></a>
|
||||||
>
|
>
|
||||||
|
|||||||
@ -11,13 +11,13 @@ Swift 中的模式分为两类:一种能成功匹配任何类型的值,另
|
|||||||
> 模式语法
|
> 模式语法
|
||||||
>
|
>
|
||||||
<a name="pattern"></a>
|
<a name="pattern"></a>
|
||||||
> *模式* → [*通配符模式*](#wildcard_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
|
> *模式* → [*通配符模式*](./#wildcard_pattern) [*类型标注*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
> *模式* → [*标识符模式*](#identifier_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
|
> *模式* → [*标识符模式*](./#identifier_pattern) [*类型标注*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
> *模式* → [*值绑定模式*](#value-binding-pattern)
|
> *模式* → [*值绑定模式*](#value-binding-pattern)
|
||||||
>
|
>
|
||||||
> *模式* → [*元组模式*](#tuple-pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
|
> *模式* → [*元组模式*](./#tuple-pattern) [*类型标注*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
> *模式* → [*枚举用例模式*](#enum-case-pattern)
|
> *模式* → [*枚举用例模式*](#enum-case-pattern)
|
||||||
>
|
>
|
||||||
@ -61,7 +61,7 @@ let someValue = 42
|
|||||||
> 标识符模式语法
|
> 标识符模式语法
|
||||||
>
|
>
|
||||||
<a name="identifier-pattern"></a>
|
<a name="identifier-pattern"></a>
|
||||||
> *标识符模式* → [*标识符*](02_Lexical_Structure.html#identifier)
|
> *标识符模式* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="value-binding_pattern"></a>
|
<a name="value-binding_pattern"></a>
|
||||||
@ -131,12 +131,12 @@ let (a): Int = 2 // a: Int = 2
|
|||||||
|
|
||||||
*枚举用例模式*匹配现有的某个枚举类型的某个用例。枚举用例模式出现在 `switch` 语句中的 `case` 标签中,以及 `if`、`while`、`guard` 和 `for-in` 语句的 `case` 条件中。
|
*枚举用例模式*匹配现有的某个枚举类型的某个用例。枚举用例模式出现在 `switch` 语句中的 `case` 标签中,以及 `if`、`while`、`guard` 和 `for-in` 语句的 `case` 条件中。
|
||||||
|
|
||||||
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../chapter2/08_Enumerations.html#associated_values)。
|
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../chapter2/08_Enumerations.md#associated_values)。
|
||||||
|
|
||||||
> 枚举用例模式语法
|
> 枚举用例模式语法
|
||||||
>
|
>
|
||||||
<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.md#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](./05_Declarations.md#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="optional_pattern"></a>
|
<a name="optional_pattern"></a>
|
||||||
@ -175,7 +175,7 @@ for case let number? in arrayOfOptinalInts {
|
|||||||
> 可选模式语法
|
> 可选模式语法
|
||||||
>
|
>
|
||||||
<a name="optional-pattern"></a>
|
<a name="optional-pattern"></a>
|
||||||
> *可选模式* → [*标识符模式*](03_Types.html#type-identifier) **?**
|
> *可选模式* → [*标识符模式*](./03_Types.md#type-identifier) **?**
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="type-casting_patterns"></a>
|
<a name="type-casting_patterns"></a>
|
||||||
@ -192,7 +192,7 @@ for case let number? in arrayOfOptinalInts {
|
|||||||
|
|
||||||
`as` 模式仅当一个值的类型在运行时和 `as` 模式右边的指定类型一致,或者是其子类的情况下,才会匹配这个值。如果匹配成功,被匹配的值的类型被转换成 `as` 模式右边指定的类型。
|
`as` 模式仅当一个值的类型在运行时和 `as` 模式右边的指定类型一致,或者是其子类的情况下,才会匹配这个值。如果匹配成功,被匹配的值的类型被转换成 `as` 模式右边指定的类型。
|
||||||
|
|
||||||
关于使用 `switch` 语句配合 `is` 模式和 `as` 模式来匹配值的例子,请参阅 [Any 和 AnyObject 的类型转换](../chapter2/18_Type_Casting.html#type_casting_for_any_and_anyobject)。
|
关于使用 `switch` 语句配合 `is` 模式和 `as` 模式来匹配值的例子,请参阅 [Any 和 AnyObject 的类型转换](../chapter2/18_Type_Casting.md#type_casting_for_any_and_anyobject)。
|
||||||
|
|
||||||
> 类型转换模式语法
|
> 类型转换模式语法
|
||||||
>
|
>
|
||||||
@ -200,10 +200,10 @@ for case let number? in arrayOfOptinalInts {
|
|||||||
> *类型转换模式* → [*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.md#type)
|
||||||
>
|
>
|
||||||
<a name="as-pattern"></a>
|
<a name="as-pattern"></a>
|
||||||
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.html#type)
|
> *as 模式* → [*模式*](./#pattern) **as** [*类型*](03_Types.md#type)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="expression_pattern"></a>
|
<a name="expression_pattern"></a>
|
||||||
@ -247,5 +247,5 @@ default:
|
|||||||
> 表达式模式语法
|
> 表达式模式语法
|
||||||
>
|
>
|
||||||
<a name="expression-pattern"></a>
|
<a name="expression-pattern"></a>
|
||||||
> *表达式模式* → [*表达式*](04_Expressions.html#expression)
|
> *表达式模式* → [*表达式*](./04_Expressions.md#expression)
|
||||||
>
|
>
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
本节涉及泛型类型、泛型函数以及泛型构造器的参数,包括形参和实参。声明泛型类型、函数或构造器时,须指定相应的类型参数。类型参数相当于一个占位符,当实例化泛型类型、调用泛型函数或泛型构造器时,就用具体的类型实参替代之。
|
本节涉及泛型类型、泛型函数以及泛型构造器的参数,包括形参和实参。声明泛型类型、函数或构造器时,须指定相应的类型参数。类型参数相当于一个占位符,当实例化泛型类型、调用泛型函数或泛型构造器时,就用具体的类型实参替代之。
|
||||||
|
|
||||||
关于 Swift 语言的泛型概述,请参阅 [泛型](../chapter2/22_Generics.html)。
|
关于 Swift 语言的泛型概述,请参阅 [泛型](../chapter2/22_Generics.md)。
|
||||||
|
|
||||||
<a name="generic_parameter"></a>
|
<a name="generic_parameter"></a>
|
||||||
## 泛型形参子句
|
## 泛型形参子句
|
||||||
@ -56,7 +56,7 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
|||||||
|
|
||||||
泛型函数或构造器可以重载,但在泛型形参子句中的类型形参必须有不同的约束或要求,抑或二者皆不同。当调用重载的泛型函数或构造器时,编译器会根据这些约束来决定调用哪个重载函数或构造器。
|
泛型函数或构造器可以重载,但在泛型形参子句中的类型形参必须有不同的约束或要求,抑或二者皆不同。当调用重载的泛型函数或构造器时,编译器会根据这些约束来决定调用哪个重载函数或构造器。
|
||||||
|
|
||||||
更多关于泛型 where 从句的信息和关于泛型函数声明的例子,可以看一看 [泛型 where 子句](https://github.com/numbbbbb/the-swift-programming-language-in-chinese/blob/gh-pages/source/chapter2/22_Generics.html#where_clauses)
|
更多关于泛型 where 从句的信息和关于泛型函数声明的例子,可以看一看 [泛型 where 子句](./https://github.com/numbbbbb/the-swift-programming-language-in-chinese/blob/gh-pages/source/chapter2/22_Generics.md#where_clauses)
|
||||||
|
|
||||||
> 泛型形参子句语法
|
> 泛型形参子句语法
|
||||||
>
|
>
|
||||||
@ -67,11 +67,11 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
|||||||
> *泛型形参列表* → [*泛形形参*](#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.md#type-name)
|
||||||
>
|
>
|
||||||
> *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*类型标识符*](03_Types.html#type-identifier)
|
> *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*类型标识符*](./03_Types.md#type-identifier)
|
||||||
>
|
>
|
||||||
> *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*协议合成类型*](03_Types.html#protocol-composition-type)
|
> *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*协议合成类型*](./03_Types.md#protocol-composition-type)
|
||||||
>
|
>
|
||||||
> <a name="requirement-clause"></a>
|
> <a name="requirement-clause"></a>
|
||||||
>
|
>
|
||||||
@ -85,12 +85,12 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
|||||||
>
|
>
|
||||||
> <a name="conformance-requirement"></a>
|
> <a name="conformance-requirement"></a>
|
||||||
>
|
>
|
||||||
> *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*类型标识符*](03_Types.html#type-identifier)
|
> *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*类型标识符*](./03_Types.md#type-identifier)
|
||||||
>
|
>
|
||||||
> *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*协议合成类型*](03_Types.html#protocol-composition-type)
|
> *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*协议合成类型*](./03_Types.md#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.md#type-identifier) **==** [*类型*](./03_Types.md#type)
|
||||||
>
|
>
|
||||||
|
|
||||||
<a name="generic_argument"></a>
|
<a name="generic_argument"></a>
|
||||||
@ -129,5 +129,5 @@ let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
|||||||
> *泛型实参列表* → [*泛型实参*](#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.md#type)
|
||||||
>
|
>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user