fix anchor name and link format
This commit is contained in:
@ -116,7 +116,7 @@ print(friendlyWelcome)
|
||||
// 输出“Bonjour!”
|
||||
```
|
||||
|
||||
`print(_:separator:terminator:)` 是一个用来输出一个或多个值到适当输出区的全局函数。如果你用 Xcode,`print(_:separator:terminator:)` 将会输出内容到“console”面板上。`separator` 和 `terminator` 参数具有默认值,因此你调用这个函数的时候可以忽略它们。默认情况下,该函数通过添加换行符来结束当前行。如果不想换行,可以传递一个空字符串给 `terminator` 参数--例如,`print(someValue, terminator:"")` 。关于参数默认值的更多信息,请参考 [默认参数值](./06_Functions.md#default_parameter_values)。
|
||||
`print(_:separator:terminator:)` 是一个用来输出一个或多个值到适当输出区的全局函数。如果你用 Xcode,`print(_:separator:terminator:)` 将会输出内容到“console”面板上。`separator` 和 `terminator` 参数具有默认值,因此你调用这个函数的时候可以忽略它们。默认情况下,该函数通过添加换行符来结束当前行。如果不想换行,可以传递一个空字符串给 `terminator` 参数--例如,`print(someValue, terminator:"")` 。关于参数默认值的更多信息,请参考 [默认参数值](./06_Functions.md#default-parameter-values)。
|
||||
|
||||
Swift 用*字符串插值(string interpolation)*的方式把常量名或者变量名当做占位符加入到长字符串中,Swift 会用当前常量或变量的值替换这些占位符。将常量或变量名放入圆括号中,并在开括号前使用反斜杠将其转义:
|
||||
|
||||
@ -127,7 +127,7 @@ print("The current value of friendlyWelcome is \(friendlyWelcome)")
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 字符串插值所有可用的选项,请参考 [字符串插值](./03_Strings_and_Characters.md#string_interpolation)。
|
||||
> 字符串插值所有可用的选项,请参考 [字符串插值](./03_Strings_and_Characters.md#string-interpolation)。
|
||||
|
||||
## 注释 {#comments}
|
||||
|
||||
@ -200,7 +200,7 @@ Swift 也提供了一个特殊的无符号类型 `UInt`,长度与当前平台
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 尽量不要使用 `UInt`,除非你真的需要存储一个和当前平台原生字长相同的无符号整数。除了这种情况,最好使用 `Int`,即使你要存储的值已知是非负的。统一使用 `Int` 可以提高代码的可复用性,避免不同类型数字之间的转换,并且匹配数字的类型推断,请参考 [类型安全和类型推断](#type_safety_and_type_inference)。
|
||||
> 尽量不要使用 `UInt`,除非你真的需要存储一个和当前平台原生字长相同的无符号整数。除了这种情况,最好使用 `Int`,即使你要存储的值已知是非负的。统一使用 `Int` 可以提高代码的可复用性,避免不同类型数字之间的转换,并且匹配数字的类型推断,请参考 [类型安全和类型推断](#type-safety-and-type-inference)。
|
||||
|
||||
## 浮点数 {#floating-point-numbers}
|
||||
|
||||
@ -477,7 +477,7 @@ print("The status message is \(http200Status.description)")
|
||||
// 输出“The status message is OK”
|
||||
```
|
||||
|
||||
作为函数返回值时,元组非常有用。一个用来获取网页的函数可能会返回一个 `(Int, String)` 元组来描述是否获取成功。和只能返回一个类型的值比较起来,一个包含两个不同类型值的元组可以让函数的返回信息更有用。请参考 [函数参数与返回值](./06_Functions.md#Function_Parameters_and_Return_Values)。
|
||||
作为函数返回值时,元组非常有用。一个用来获取网页的函数可能会返回一个 `(Int, String)` 元组来描述是否获取成功。和只能返回一个类型的值比较起来,一个包含两个不同类型值的元组可以让函数的返回信息更有用。请参考 [函数参数与返回值](./06_Functions.md#Function-Parameters-and-Return-Values)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
@ -609,7 +609,7 @@ if let firstNumber = Int("4") {
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 在 `if` 条件语句中使用常量和变量来创建一个可选绑定,仅在 `if` 语句的句中(`body`)中才能获取到值。相反,在 `guard` 语句中使用常量和变量来创建一个可选绑定,仅在 `guard` 语句外且在语句后才能获取到值,请参考 [提前退出](./05_Control_Flow.md#early_exit)。
|
||||
> 在 `if` 条件语句中使用常量和变量来创建一个可选绑定,仅在 `if` 语句的句中(`body`)中才能获取到值。相反,在 `guard` 语句中使用常量和变量来创建一个可选绑定,仅在 `guard` 语句外且在语句后才能获取到值,请参考 [提前退出](./05_Control_Flow.md#early-exit)。
|
||||
|
||||
### 隐式解析可选类型 {#implicityly-unwrapped-optionals}
|
||||
|
||||
@ -619,7 +619,7 @@ if let firstNumber = Int("4") {
|
||||
|
||||
这种类型的可选状态被定义为隐式解析可选类型(implicitly unwrapped optionals)。把想要用作可选的类型的后面的问号(`String?`)改成感叹号(`String!`)来声明一个隐式解析可选类型。
|
||||
|
||||
当可选类型被第一次赋值之后就可以确定之后一直有值的时候,隐式解析可选类型非常有用。隐式解析可选类型主要被用在 Swift 中类的构造过程中,请参考 [无主引用以及隐式解析可选属性](./24_Automatic_Reference_Counting.md#unowned_references_and_implicitly_unwrapped_optional_properties)。
|
||||
当可选类型被第一次赋值之后就可以确定之后一直有值的时候,隐式解析可选类型非常有用。隐式解析可选类型主要被用在 Swift 中类的构造过程中,请参考 [无主引用以及隐式解析可选属性](./24_Automatic_Reference_Counting.md#unowned-references-and-implicitly-unwrapped-optional-properties)。
|
||||
|
||||
一个隐式解析可选类型其实就是一个普通的可选类型,但是可以被当做非可选类型来使用,并不需要每次都使用解析来获取可选值。下面的例子展示了可选类型 `String` 和隐式解析可选类型 `String` 之间的区别:
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
*运算符*是检查、改变、合并值的特殊符号或短语。例如,加号(`+`)将两个数相加(如 `let i = 1 + 2`)。更复杂的运算例子包括逻辑与运算符 `&&`(如 `if enteredDoorCode && passedRetinaScan`)。
|
||||
|
||||
Swift 支持大部分标准 C 语言的运算符,且为了减少常见编码错误做了部分改进。如:赋值符(`=`)不再有返回值,这样就消除了手误将判等运算符(`==`)写成赋值符导致代码错误的缺陷。算术运算符(`+`,`-`,`*`,`/`,`%` 等)的结果会被检测并禁止值溢出,以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果。当然允许你使用 Swift 的溢出运算符来实现溢出。详情参见 [溢出运算符](./27_Advanced_Operators.md#overflow_operators)。
|
||||
Swift 支持大部分标准 C 语言的运算符,且为了减少常见编码错误做了部分改进。如:赋值符(`=`)不再有返回值,这样就消除了手误将判等运算符(`==`)写成赋值符导致代码错误的缺陷。算术运算符(`+`,`-`,`*`,`/`,`%` 等)的结果会被检测并禁止值溢出,以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果。当然允许你使用 Swift 的溢出运算符来实现溢出。详情参见 [溢出运算符](./27_Advanced_Operators.md#overflow-operators)。
|
||||
|
||||
Swift 还提供了 C 语言没有的区间运算符,例如 `a..<b` 或 `a...b`,这方便我们表达一个区间内的数值。
|
||||
|
||||
@ -62,7 +62,7 @@ Swift 中所有数值类型都支持了基本的四则*算术运算符*:
|
||||
10.0 / 2.5 // 等于 4.0
|
||||
```
|
||||
|
||||
与 C 语言和 Objective-C 不同的是,Swift 默认情况下不允许在数值运算中出现溢出情况。但是你可以使用 Swift 的溢出运算符来实现溢出运算(如 `a &+ b`)。详情参见 [溢出运算符](./27_Advanced_Operators.md#overflow_operators)。
|
||||
与 C 语言和 Objective-C 不同的是,Swift 默认情况下不允许在数值运算中出现溢出情况。但是你可以使用 Swift 的溢出运算符来实现溢出运算(如 `a &+ b`)。详情参见 [溢出运算符](./27_Advanced_Operators.md#overflow-operators)。
|
||||
|
||||
加法运算符也可用于 `String` 的拼接:
|
||||
|
||||
@ -402,7 +402,7 @@ if !allowedEntry {
|
||||
|
||||
在示例代码中,小心地选择布尔常量或变量有助于代码的可读性,并且避免使用双重逻辑非运算,或混乱的逻辑语句。
|
||||
|
||||
### 逻辑与运算符 #{logical_and_operator}
|
||||
### 逻辑与运算符 #{logical-and-operator}
|
||||
|
||||
*逻辑与运算符*(`a && b`)表达了只有 `a` 和 `b` 的值都为 `true` 时,整个表达式的值才会是 `true`。
|
||||
|
||||
@ -421,7 +421,7 @@ if enteredDoorCode && passedRetinaScan {
|
||||
// 输出“ACCESS DENIED”
|
||||
```
|
||||
|
||||
### 逻辑或运算符 #{logical_or_operator}
|
||||
### 逻辑或运算符 #{logical-or-operator}
|
||||
|
||||
逻辑或运算符(`a || b`)是一个由两个连续的 `|` 组成的中置运算符。它表示了两个逻辑表达式的其中一个为 `true`,整个表达式就为 `true`。
|
||||
|
||||
|
||||
@ -157,7 +157,7 @@ constantString += " and another Highlander"
|
||||
|
||||
## 字符串是值类型 {#strings-are-value-types}
|
||||
|
||||
在 Swift 中 `String` 类型是*值类型*。如果你创建了一个新的字符串,那么当其进行常量、变量赋值操作,或在函数/方法中传递时,会进行值拷贝。在前述任一情况下,都会对已有字符串值创建新副本,并对该新副本而非原始字符串进行传递或赋值操作。值类型在 [结构体和枚举是值类型](./09_Classes_and_Structures.md#structures_and_enumerations_are_value_types) 中进行了详细描述。
|
||||
在 Swift 中 `String` 类型是*值类型*。如果你创建了一个新的字符串,那么当其进行常量、变量赋值操作,或在函数/方法中传递时,会进行值拷贝。在前述任一情况下,都会对已有字符串值创建新副本,并对该新副本而非原始字符串进行传递或赋值操作。值类型在 [结构体和枚举是值类型](./09_Classes_and_Structures.md#structures-and-enumerations-are-value-types) 中进行了详细描述。
|
||||
|
||||
Swift 默认拷贝字符串的行为保证了在函数/方法向你传递的字符串所属权属于你,无论该值来自于哪里。你可以确信传递的字符串不会被修改,除非你自己去修改它。
|
||||
|
||||
@ -178,7 +178,7 @@ for character in "Dog!🐶" {
|
||||
// 🐶
|
||||
```
|
||||
|
||||
`for-in` 循环在 [For 循环](./05_Control_Flow.md#for_loops) 中进行了详细描述。
|
||||
`for-in` 循环在 [For 循环](./05_Control_Flow.md#for-loops) 中进行了详细描述。
|
||||
|
||||
另外,通过标明一个 `Character` 类型并用字符字面量进行赋值,可以建立一个独立的字符常量或变量:
|
||||
|
||||
@ -458,7 +458,7 @@ Swift 提供了三种方式来比较文本值:字符串字符相等、前缀
|
||||
|
||||
### 字符串/字符相等 {#string-and-character-equality}
|
||||
|
||||
字符串/字符可以用等于操作符(`==`)和不等于操作符(`!=`),详细描述在 [比较运算符](./02_Basic_Operators.md#comparison_operators):
|
||||
字符串/字符可以用等于操作符(`==`)和不等于操作符(`!=`),详细描述在 [比较运算符](./02_Basic_Operators.md#comparison-operators):
|
||||
|
||||
```swift
|
||||
let quotation = "We're a lot alike, you and I."
|
||||
@ -556,13 +556,13 @@ print("\(mansionCount) mansion scenes; \(cellCount) cell scenes")
|
||||
|
||||
> 注意
|
||||
>
|
||||
> `hasPrefix(_:)` 和 `hasSuffix(_:)` 方法都是在每个字符串中逐字符比较其可扩展的字符群集是否标准相等,详细描述在 [字符串/字符相等](#string_and_character_equality)。
|
||||
> `hasPrefix(_:)` 和 `hasSuffix(_:)` 方法都是在每个字符串中逐字符比较其可扩展的字符群集是否标准相等,详细描述在 [字符串/字符相等](#string-and-character-equality)。
|
||||
|
||||
## 字符串的 Unicode 表示形式 {#unicode-representations-of-strings}
|
||||
|
||||
当一个 Unicode 字符串被写进文本文件或者其他储存时,字符串中的 Unicode 标量会用 Unicode 定义的几种 `编码格式`(encoding forms)编码。每一个字符串中的小块编码都被称 `代码单元`(code units)。这些包括 UTF-8 编码格式(编码字符串为 8 位的代码单元), UTF-16 编码格式(编码字符串位 16 位的代码单元),以及 UTF-32 编码格式(编码字符串32位的代码单元)。
|
||||
|
||||
Swift 提供了几种不同的方式来访问字符串的 Unicode 表示形式。你可以利用 `for-in` 来对字符串进行遍历,从而以 Unicode 可扩展的字符群集的方式访问每一个 `Character` 值。该过程在 [使用字符](#working_with_characters) 中进行了描述。
|
||||
Swift 提供了几种不同的方式来访问字符串的 Unicode 表示形式。你可以利用 `for-in` 来对字符串进行遍历,从而以 Unicode 可扩展的字符群集的方式访问每一个 `Character` 值。该过程在 [使用字符](#working-with-characters) 中进行了描述。
|
||||
|
||||
另外,能够以其他三种 Unicode 兼容的方式访问字符串的值:
|
||||
|
||||
|
||||
@ -235,7 +235,7 @@ for (index, value) in shoppingList.enumerated() {
|
||||
// Item 5: Bananas
|
||||
```
|
||||
|
||||
更多关于 `for-in` 循环的介绍请参见 [For 循环](05_Control_Flow.html#for_loops)。
|
||||
更多关于 `for-in` 循环的介绍请参见 [For 循环](05_Control_Flow.html#for-loops)。
|
||||
|
||||
## 集合(Sets) {#sets}
|
||||
|
||||
@ -380,7 +380,7 @@ for genre in favoriteGenres {
|
||||
// Hip hop
|
||||
```
|
||||
|
||||
更多关于 `for-in` 循环的信息,参见 [For 循环](./05_Control_Flow.md#for_loops)。
|
||||
更多关于 `for-in` 循环的信息,参见 [For 循环](./05_Control_Flow.md#for-loops)。
|
||||
|
||||
Swift 的 `Set` 类型没有确定的顺序,为了按照特定顺序来遍历一个集合中的值可以使用 `sorted()` 方法,它将返回一个有序数组,这个数组的元素排列顺序由操作符 `<` 对元素进行比较的结果来确定。
|
||||
|
||||
@ -613,7 +613,7 @@ for (airportCode, airportName) in airports {
|
||||
// LHR: London Heathrow
|
||||
```
|
||||
|
||||
更多关于 `for-in` 循环的信息,参见 [For 循环](./05_Control_Flow.md#for_loops)。
|
||||
更多关于 `for-in` 循环的信息,参见 [For 循环](./05_Control_Flow.md#for-loops)。
|
||||
|
||||
通过访问 `keys` 或者 `values` 属性,你也可以遍历字典的键或者值:
|
||||
|
||||
|
||||
@ -69,7 +69,7 @@ print("\(base) to the power of \(power) is \(answer)")
|
||||
|
||||
这个例子计算 base 这个数的 power 次幂(本例中,是 `3` 的 `10` 次幂),从 `1`(`3` 的 `0` 次幂)开始做 `3` 的乘法, 进行 `10` 次,使用 `1` 到 `10` 的闭区间循环。这个计算并不需要知道每一次循环中计数器具体的值,只需要执行了正确的循环次数即可。下划线符号 `_` (替代循环中的变量)能够忽略当前值,并且不提供循环遍历时对值的访问。
|
||||
|
||||
在某些情况下,你可能不想使用包括两个端点的闭区间。想象一下,你在一个手表上绘制分钟的刻度线。总共 `60` 个刻度,从 `0` 分开始。使用半开区间运算符(`..<`)来表示一个左闭右开的区间。有关区间的更多信息,请参阅 [区间运算符](./02_Basic_Operators.md#range_operators)。
|
||||
在某些情况下,你可能不想使用包括两个端点的闭区间。想象一下,你在一个手表上绘制分钟的刻度线。总共 `60` 个刻度,从 `0` 分开始。使用半开区间运算符(`..<`)来表示一个左闭右开的区间。有关区间的更多信息,请参阅 [区间运算符](./02_Basic_Operators.md#range-operators)。
|
||||
|
||||
```swift
|
||||
let minutes = 60
|
||||
@ -332,7 +332,7 @@ default:
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 虽然在 Swift 中 `break` 不是必须的,但你依然可以在 case 分支中的代码执行完毕前使用 `break` 跳出,详情请参见 [Switch 语句中的 break](#break_in_a_switch_statement)。
|
||||
> 虽然在 Swift 中 `break` 不是必须的,但你依然可以在 case 分支中的代码执行完毕前使用 `break` 跳出,详情请参见 [Switch 语句中的 break](#break-in-a-switch-statement)。
|
||||
|
||||
每一个 case 分支都*必须*包含至少一条语句。像下面这样书写代码是无效的,因为第一个 case 分支是空的:
|
||||
|
||||
@ -363,7 +363,7 @@ default:
|
||||
// 输出“The letter A”
|
||||
```
|
||||
|
||||
为了可读性,符合匹配可以写成多行形式,详情请参考 [复合匹配](#compound_cases)
|
||||
为了可读性,符合匹配可以写成多行形式,详情请参考 [复合匹配](#compound-cases)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
@ -527,7 +527,7 @@ default:
|
||||
- `return`
|
||||
- `throw`
|
||||
|
||||
我们将会在下面讨论 `continue`、`break` 和 `fallthrough` 语句。`return` 语句将会在 [函数](./06_Functions.md) 章节讨论,`throw` 语句会在 [错误抛出](./18_Error_Handling.md#throwing_errors) 章节讨论。
|
||||
我们将会在下面讨论 `continue`、`break` 和 `fallthrough` 语句。`return` 语句将会在 [函数](./06_Functions.md) 章节讨论,`throw` 语句会在 [错误抛出](./18_Error_Handling.md#throwing-errors) 章节讨论。
|
||||
|
||||
### Continue {#continue}
|
||||
|
||||
|
||||
@ -318,7 +318,7 @@ arithmeticMean(3, 8.25, 18.75)
|
||||
|
||||
函数参数默认是常量。试图在函数体中更改参数值将会导致编译错误。这意味着你不能错误地更改参数值。如果你想要一个函数可以修改参数的值,并且想要在这些修改在函数调用结束后仍然存在,那么就应该把这个参数定义为*输入输出参数(In-Out Parameters)*。
|
||||
|
||||
定义一个输入输出参数时,在参数定义前加 `inout` 关键字。一个 `输入输出参数`有传入函数的值,这个值被函数修改,然后被传出函数,替换原来的值。想获取更多的关于输入输出参数的细节和相关的编译器优化,请查看 [输入输出参数](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID545) 一节。
|
||||
定义一个输入输出参数时,在参数定义前加 `inout` 关键字。一个 `输入输出参数`有传入函数的值,这个值被函数修改,然后被传出函数,替换原来的值。想获取更多的关于输入输出参数的细节和相关的编译器优化,请查看 [输入输出参数](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple-ref/doc/uid/TP40014097-CH34-ID545) 一节。
|
||||
|
||||
你只能传递变量给输入输出参数。你不能传入常量或者字面量,因为这些量是不能被修改的。当传入的参数作为输入输出参数时,需要在参数名前加 `&` 符,表示这个值可以被函数修改。
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 如果你不熟悉捕获(capturing)这个概念也不用担心,在 [值捕获](#capturing_values) 章节有它更详细的介绍。
|
||||
> 如果你不熟悉捕获(capturing)这个概念也不用担心,在 [值捕获](#capturing-values) 章节有它更详细的介绍。
|
||||
|
||||
在 [函数](./06_Functions.md) 章节中介绍的全局和嵌套函数实际上也是特殊的闭包,闭包采用如下三种形式之一:
|
||||
|
||||
@ -23,7 +23,7 @@ Swift 的闭包表达式拥有简洁的风格,并鼓励在常见场景中进
|
||||
|
||||
## 闭包表达式 {#closure-expressions}
|
||||
|
||||
[嵌套函数](./06_Functions.md#Nested_Functions) 作为复杂函数的一部分时,它自包含代码块式的定义和命名形式在使用上带来了方便。当然,编写未完整声明和没有函数名的类函数结构代码是很有用的,尤其是在编码中涉及到函数作为参数的那些方法时。
|
||||
[嵌套函数](./06_Functions.md#Nested-Functions) 作为复杂函数的一部分时,它自包含代码块式的定义和命名形式在使用上带来了方便。当然,编写未完整声明和没有函数名的类函数结构代码是很有用的,尤其是在编码中涉及到函数作为参数的那些方法时。
|
||||
|
||||
*闭包表达式*是一种构建内联闭包的方式,它的语法简洁。在保证不丢失它语法清晰明了的同时,闭包表达式提供了几种优化的语法简写形式。下面通过对 `sorted(by:)` 这一个案例的多次迭代改进来展示这个过程,每次迭代都使用了更加简明的方式描述了相同功能。。
|
||||
|
||||
@ -129,7 +129,7 @@ reversedNames = names.sorted(by: { $0 > $1 } )
|
||||
reversedNames = names.sorted(by: >)
|
||||
```
|
||||
|
||||
更多关于运算符方法的内容请查看 [运算符方法](./27_Advanced_Operators.md#operator_methods)。
|
||||
更多关于运算符方法的内容请查看 [运算符方法](./27_Advanced_Operators.md#operator-methods)。
|
||||
|
||||
## 尾随闭包 {#trailing-closures}
|
||||
|
||||
@ -151,7 +151,7 @@ someFunctionThatTakesAClosure() {
|
||||
}
|
||||
```
|
||||
|
||||
在 [闭包表达式语法](#closure_expression_syntax) 上章节中的字符串排序闭包可以作为尾随包的形式改写在 `sorted(by:)` 方法圆括号的外面:
|
||||
在 [闭包表达式语法](#closure-expression-syntax) 上章节中的字符串排序闭包可以作为尾随包的形式改写在 `sorted(by:)` 方法圆括号的外面:
|
||||
|
||||
```swift
|
||||
reversedNames = names.sorted() { $0 > $1 }
|
||||
@ -233,7 +233,7 @@ func makeIncrementer(forIncrement amount: Int) -> () -> Int {
|
||||
}
|
||||
```
|
||||
|
||||
`makeIncrementer` 返回类型为 `() -> Int`。这意味着其返回的是一个*函数*,而非一个简单类型的值。该函数在每次调用时不接受参数,只返回一个 `Int` 类型的值。关于函数返回其他函数的内容,请查看 [函数类型作为返回类型](./06_Functions.md#function_types_as_return_types)。
|
||||
`makeIncrementer` 返回类型为 `() -> Int`。这意味着其返回的是一个*函数*,而非一个简单类型的值。该函数在每次调用时不接受参数,只返回一个 `Int` 类型的值。关于函数返回其他函数的内容,请查看 [函数类型作为返回类型](./06_Functions.md#function-types-as-return-types)。
|
||||
|
||||
`makeIncrementer(forIncrement:)` 函数定义了一个初始值为 `0` 的整型变量 `runningTotal`,用来存储当前总计数值。该值为 `incrementer` 的返回值。
|
||||
|
||||
@ -290,7 +290,7 @@ incrementByTen()
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 如果你将闭包赋值给一个类实例的属性,并且该闭包通过访问该实例或其成员而捕获了该实例,你将在闭包和该实例间创建一个循环强引用。Swift 使用捕获列表来打破这种循环强引用。更多信息,请参考 [闭包引起的循环强引用](./24_Automatic_Reference_Counting.md#strong_reference_cycles_for_closures)。
|
||||
> 如果你将闭包赋值给一个类实例的属性,并且该闭包通过访问该实例或其成员而捕获了该实例,你将在闭包和该实例间创建一个循环强引用。Swift 使用捕获列表来打破这种循环强引用。更多信息,请参考 [闭包引起的循环强引用](./24_Automatic_Reference_Counting.md#strong-reference-cycles-for-closures)。
|
||||
|
||||
## 闭包是引用类型 {#closures-are-reference-types}
|
||||
|
||||
@ -397,7 +397,7 @@ serve(customer: customersInLine.remove(at: 0))
|
||||
>
|
||||
> 过度使用 `autoclosures` 会让你的代码变得难以理解。上下文和函数名应该能够清晰地表明求值是被延迟执行的。
|
||||
|
||||
如果你想让一个自动闭包可以“逃逸”,则应该同时使用 `@autoclosure` 和 `@escaping` 属性。`@escaping` 属性的讲解见上面的 [逃逸闭包](#escaping_closures)。
|
||||
如果你想让一个自动闭包可以“逃逸”,则应该同时使用 `@autoclosure` 和 `@escaping` 属性。`@escaping` 属性的讲解见上面的 [逃逸闭包](#escaping-closures)。
|
||||
|
||||
```swift
|
||||
// customersInLine i= ["Barry", "Daniella"]
|
||||
|
||||
@ -200,7 +200,7 @@ case let .qrCode(productCode):
|
||||
|
||||
## 原始值 {#raw-values}
|
||||
|
||||
在 [关联值](#associated_values) 小节的条形码例子中,演示了如何声明存储不同类型关联值的枚举成员。作为关联值的替代选择,枚举成员可以被默认值(称为*原始值*)预填充,这些原始值的类型必须相同。
|
||||
在 [关联值](#associated-values) 小节的条形码例子中,演示了如何声明存储不同类型关联值的枚举成员。作为关联值的替代选择,枚举成员可以被默认值(称为*原始值*)预填充,这些原始值的类型必须相同。
|
||||
|
||||
这是一个使用 ASCII 码作为原始值的枚举:
|
||||
|
||||
@ -273,7 +273,7 @@ let possiblePlanet = Planet(rawValue: 7)
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 原始值构造器是一个可失败构造器,因为并不是每一个原始值都有与之对应的枚举成员。更多信息请参见 [可失败构造器](../03_language_reference/05_Declarations.html#failable_initializers)
|
||||
> 原始值构造器是一个可失败构造器,因为并不是每一个原始值都有与之对应的枚举成员。更多信息请参见 [可失败构造器](../03_language_reference/05_Declarations.html#failable-initializers)。
|
||||
|
||||
如果你试图寻找一个位置为 `11` 的行星,通过原始值构造器返回的可选 `Planet` 值将是 `nil`:
|
||||
|
||||
|
||||
@ -243,7 +243,7 @@ if tenEighty === alsoTenEighty {
|
||||
|
||||
请注意,“相同”(用三个等号表示,`===`)与“等于”(用两个等号表示,`==`)的不同。“相同”表示两个类类型(class type)的常量或者变量引用同一个类实例。“等于”表示两个实例的值“相等”或“等价”,判定时要遵照设计者定义的评判标准。
|
||||
|
||||
当在定义你的自定义结构体和类的时候,你有义务来决定判定两个实例“相等”的标准。在章节 [等价操作符](./27_Advanced_Operators.md#equivalence_operators) 中将会详细介绍实现自定义 == 和 !== 运算符的流程。
|
||||
当在定义你的自定义结构体和类的时候,你有义务来决定判定两个实例“相等”的标准。在章节 [等价操作符](./27_Advanced_Operators.md#equivalence-operators) 中将会详细介绍实现自定义 == 和 !== 运算符的流程。
|
||||
|
||||
### 指针 {#pointers}
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
简单来说,一个存储属性就是存储在特定类或结构体实例里的一个常量或变量。存储属性可以是*变量存储属性*(用关键字 `var` 定义),也可以是*常量存储属性*(用关键字 `let` 定义)。
|
||||
|
||||
可以在定义存储属性的时候指定默认值,请参考 [默认构造器](./14_Initialization.md#default_initializers) 一节。也可以在构造过程中设置或修改存储属性的值,甚至修改常量存储属性的值,请参考 [构造过程中常量属性的修改](./14_Initialization.md#assigning_constant_properties_during_initialization) 一节。
|
||||
可以在定义存储属性的时候指定默认值,请参考 [默认构造器](./14_Initialization.md#default-initializers) 一节。也可以在构造过程中设置或修改存储属性的值,甚至修改常量存储属性的值,请参考 [构造过程中常量属性的修改](./14-Initialization.md#assigning-constant-properties-during-initialization) 一节。
|
||||
|
||||
下面的例子定义了一个名为 `FixedLengthRange` 的结构体,该结构体用于描述整数的区间,且这个范围值在被创建后不能被修改。
|
||||
|
||||
@ -239,7 +239,7 @@ print("the volume of fourByFiveByTwo is \(fourByFiveByTwo.volume)")
|
||||
>
|
||||
> 在父类初始化方法调用之后,在子类构造器中给父类的属性赋值时,会调用父类属性的 `willSet` 和 `didSet` 观察器。而在父类初始化方法调用之前,给子类的属性赋值时不会调用子类属性的观察器。
|
||||
>
|
||||
> 有关构造器代理的更多信息,请参考 [值类型的构造器代理](./14_Initialization.md#initializer_delegation_for_value_types) 和 [类的构造器代理](./14_Initialization.md#initializer_delegation_for_class_types)。
|
||||
> 有关构造器代理的更多信息,请参考 [值类型的构造器代理](./14_Initialization.md#initializer-delegation-for-value-types) 和 [类的构造器代理](./14-Initialization.md#initializer-delegation-for-class-types)。
|
||||
|
||||
下面是一个 `willSet` 和 `didSet` 实际运用的例子,其中定义了一个名为 `StepCounter` 的类,用来统计一个人步行时的总步数。这个类可以跟计步器或其他日常锻炼的统计装置的输入数据配合使用。
|
||||
|
||||
@ -278,7 +278,7 @@ stepCounter.totalSteps = 896
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 如果将带有观察器的属性通过 in-out 方式传入函数,`willSet` 和 `didSet` 也会调用。这是因为 in-out 参数采用了拷入拷出内存模式:即在函数内部使用的是参数的 copy,函数结束后,又对参数重新赋值。关于 in-out 参数详细的介绍,请参考 [输入输出参数](../03_language_reference/05_Declarations.html#in-out_parameters)
|
||||
> 如果将带有观察器的属性通过 in-out 方式传入函数,`willSet` 和 `didSet` 也会调用。这是因为 in-out 参数采用了拷入拷出内存模式:即在函数内部使用的是参数的 copy,函数结束后,又对参数重新赋值。关于 in-out 参数详细的介绍,请参考 [输入输出参数](../03_language_reference/05_Declarations.html#in-out-parameters)。
|
||||
|
||||
## 全局变量和局部变量 {#global-and-local-variables}
|
||||
|
||||
@ -290,7 +290,7 @@ stepCounter.totalSteps = 896
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 全局的常量或变量都是延迟计算的,跟 [延时加载存储属性](#lazy_stored_properties) 相似,不同的地方在于,全局的常量或变量不需要标记 `lazy` 修饰符。
|
||||
> 全局的常量或变量都是延迟计算的,跟 [延时加载存储属性](#lazy-stored-properties) 相似,不同的地方在于,全局的常量或变量不需要标记 `lazy` 修饰符。
|
||||
>
|
||||
> 局部范围的常量和变量从不延迟计算。
|
||||
|
||||
|
||||
@ -47,7 +47,7 @@ counter.reset()
|
||||
// 计数值现在是0
|
||||
```
|
||||
|
||||
函数参数可以同时有一个局部名称(在函数体内部使用)和一个外部名称(在调用函数时使用),详情参见 [指定外部参数名](./06_Functions.md#specifying_external_parameter_names)。方法参数也一样,因为方法就是函数,只是这个函数与某个类型相关联了。
|
||||
函数参数可以同时有一个局部名称(在函数体内部使用)和一个外部名称(在调用函数时使用),详情参见 [指定外部参数名](./06_Functions.md#specifying-external-parameter-names)。方法参数也一样,因为方法就是函数,只是这个函数与某个类型相关联了。
|
||||
|
||||
### self 属性 {#the-self-property}
|
||||
|
||||
@ -107,7 +107,7 @@ print("The point is now at (\(somePoint.x), \(somePoint.y))")
|
||||
|
||||
上面的 `Point` 结构体定义了一个可变方法 `moveBy(x:y :)` 来移动 `Point` 实例到给定的位置。该方法被调用时修改了这个点,而不是返回一个新的点。方法定义时加上了 `mutating` 关键字,从而允许修改属性。
|
||||
|
||||
注意,不能在结构体类型的常量(a constant of structure type)上调用可变方法,因为其属性不能被改变,即使属性是变量属性,详情参见 [常量结构体的存储属性](./10_Properties.md#stored_properties_of_constant_structure_instances):
|
||||
注意,不能在结构体类型的常量(a constant of structure type)上调用可变方法,因为其属性不能被改变,即使属性是变量属性,详情参见 [常量结构体的存储属性](./10_Properties.md#stored-properties-of-constant-structure-instances):
|
||||
|
||||
```swift
|
||||
let fixedPoint = Point(x: 3.0, y: 3.0)
|
||||
|
||||
@ -64,7 +64,7 @@ numberOfLegs["bird"] = 2
|
||||
|
||||
上例定义一个名为 `numberOfLegs` 的变量,并用一个包含三对键值的字典字面量初始化它。`numberOfLegs` 字典的类型被推断为 `[String: Int]`。字典创建完成后,该例子通过下标将 `String` 类型的键 `bird` 和 `Int` 类型的值 `2` 添加到字典中。
|
||||
|
||||
更多关于 `Dictionary` 下标的信息请参考 [读取和修改字典](./04_Collection_Types.md#accessing_and_modifying_a_dictionary)。
|
||||
更多关于 `Dictionary` 下标的信息请参考 [读取和修改字典](./04_Collection_Types.md#accessing-and-modifying-a-dictionary)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
@ -103,7 +103,7 @@ struct Matrix {
|
||||
}
|
||||
```
|
||||
|
||||
`Matrix` 提供了一个接受两个入参的构造方法,入参分别是 `rows` 和 `columns`,创建了一个足够容纳 `rows * columns` 个 `Double` 类型的值的数组。通过传入数组长度和初始值 `0.0` 到数组的构造器,将矩阵中每个位置的值初始化为 `0.0`。关于数组的这种构造方法请参考 [创建一个带有默认值的数组](./04_Collection_Types.md#creating_an_array_with_a_default_value)。
|
||||
`Matrix` 提供了一个接受两个入参的构造方法,入参分别是 `rows` 和 `columns`,创建了一个足够容纳 `rows * columns` 个 `Double` 类型的值的数组。通过传入数组长度和初始值 `0.0` 到数组的构造器,将矩阵中每个位置的值初始化为 `0.0`。关于数组的这种构造方法请参考 [创建一个带有默认值的数组](./04_Collection_Types.md#creating-an-array-with-a-default-value)。
|
||||
|
||||
你可以通过传入合适的 `row` 和 `column` 数值来构造一个新的 `Matrix` 实例:
|
||||
|
||||
|
||||
@ -184,7 +184,7 @@ print("Car: \(car.description)")
|
||||
|
||||
#### 重写属性观察器 {#overriding-property-observers}
|
||||
|
||||
你可以通过重写属性为一个继承来的属性添加属性观察器。这样一来,无论被继承属性原本是如何实现的,当其属性值发生改变时,你就会被通知到。关于属性观察器的更多内容,请看 [属性观察器](../02_language_guide/10_Properties.html#property_observers)。
|
||||
你可以通过重写属性为一个继承来的属性添加属性观察器。这样一来,无论被继承属性原本是如何实现的,当其属性值发生改变时,你就会被通知到。关于属性观察器的更多内容,请看 [属性观察器](../02_language_guide/10_Properties.html#property-observers)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
|
||||
@ -131,7 +131,7 @@ let veryGreen = Color(0.0, 1.0, 0.0)
|
||||
|
||||
如果你不希望构造器的某个形参使用实参标签,可以使用下划线(`_`)来代替显式的实参标签来重写默认行为。
|
||||
|
||||
下面是之前 [形参的构造过程](#initialization_parameters) 中 `Celsius` 例子的扩展,多了一个用已经的摄氏表示的 `Double` 类型值来创建新的 `Celsius` 实例的额外构造器:
|
||||
下面是之前 [形参的构造过程](#initialization-parameters) 中 `Celsius` 例子的扩展,多了一个用已经的摄氏表示的 `Double` 类型值来创建新的 `Celsius` 实例的额外构造器:
|
||||
|
||||
```swift
|
||||
struct Celsius {
|
||||
@ -258,7 +258,7 @@ print(zeroByZero.width, zeroByZero.height)
|
||||
|
||||
构造器可以通过调用其它构造器来完成实例的部分构造过程。这一过程称为*构造器代理*,它能避免多个构造器间的代码重复。
|
||||
|
||||
构造器代理的实现规则和形式在值类型和类类型中有所不同。值类型(结构体和枚举类型)不支持继承,所以构造器代理的过程相对简单,因为它们只能代理给自己的其它构造器。类则不同,它可以继承自其它类(请参考 [继承](./13_Inheritance.md))。这意味着类有责任保证其所有继承的存储型属性在构造时也能正确的初始化。这些责任将在后续章节 [类的继承和构造过程](#class_inheritance_and_initialization) 中介绍。
|
||||
构造器代理的实现规则和形式在值类型和类类型中有所不同。值类型(结构体和枚举类型)不支持继承,所以构造器代理的过程相对简单,因为它们只能代理给自己的其它构造器。类则不同,它可以继承自其它类(请参考 [继承](./13_Inheritance.md))。这意味着类有责任保证其所有继承的存储型属性在构造时也能正确的初始化。这些责任将在后续章节 [类的继承和构造过程](#class-inheritance-and-initialization) 中介绍。
|
||||
|
||||
对于值类型,你可以使用 `self.init` 在自定义的构造器中引用相同类型中的其它构造器。并且你只能在构造器内部调用 `self.init`。
|
||||
|
||||
@ -342,7 +342,7 @@ Swift 为类类型提供了两种构造器来确保实例中所有存储型属
|
||||
|
||||
类倾向于拥有极少的指定构造器,普遍的是一个类只拥有一个指定构造器。指定构造器像一个个“漏斗”放在构造过程发生的地方,让构造过程沿着父类链继续往上进行。
|
||||
|
||||
每一个类都必须至少拥有一个指定构造器。在某些情况下,许多类通过继承了父类中的指定构造器而满足了这个条件。具体内容请参考后续章节 [构造器的自动继承](#automatic_initializer_inheritance)。
|
||||
每一个类都必须至少拥有一个指定构造器。在某些情况下,许多类通过继承了父类中的指定构造器而满足了这个条件。具体内容请参考后续章节 [构造器的自动继承](#automatic-initializer-inheritance)。
|
||||
|
||||
*便利构造器*是类中比较次要的、辅助型的构造器。你可以定义便利构造器来调用同一个类中的指定构造器,并为部分形参提供默认值。你也可以定义便利构造器来创建一个特殊用途或特定输入值的实例。
|
||||
|
||||
@ -479,11 +479,11 @@ Swift 编译器将执行 4 种有效的安全检查,以确保两段式构造
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 父类的构造器仅会在安全和适当的某些情况下被继承。具体内容请参考后续章节 [构造器的自动继承](#automatic_initializer_inheritance)。
|
||||
> 父类的构造器仅会在安全和适当的某些情况下被继承。具体内容请参考后续章节 [构造器的自动继承](#automatic-initializer-inheritance)。
|
||||
|
||||
假如你希望自定义的子类中能提供一个或多个跟父类相同的构造器,你可以在子类中提供这些构造器的自定义实现。
|
||||
|
||||
当你在编写一个和父类中指定构造器相匹配的子类构造器时,你实际上是在重写父类的这个指定构造器。因此,你必须在定义子类构造器时带上 `override` 修饰符。即使你重写的是系统自动提供的默认构造器,也需要带上 `override` 修饰符,具体内容请参考 [默认构造器](#default_initializers)。
|
||||
当你在编写一个和父类中指定构造器相匹配的子类构造器时,你实际上是在重写父类的这个指定构造器。因此,你必须在定义子类构造器时带上 `override` 修饰符。即使你重写的是系统自动提供的默认构造器,也需要带上 `override` 修饰符,具体内容请参考 [默认构造器](#default-initializers)。
|
||||
|
||||
正如重写属性,方法或者是下标,`override` 修饰符会让编译器去检查父类中是否有相匹配的指定构造器,并验证构造器参数是否被按预想中被指定。
|
||||
|
||||
@ -491,7 +491,7 @@ Swift 编译器将执行 4 种有效的安全检查,以确保两段式构造
|
||||
>
|
||||
> 当你重写一个父类的指定构造器时,你总是需要写 `override` 修饰符,即使是为了实现子类的便利构造器。
|
||||
|
||||
相反,如果你编写了一个和父类便利构造器相匹配的子类构造器,由于子类不能直接调用父类的便利构造器(每个规则都在上文 [类的构造器代理规则](#initializer_delegation_for_class_types) 有所描述),因此,严格意义上来讲,你的子类并未对一个父类构造器提供重写。最后的结果就是,你在子类中“重写”一个父类便利构造器时,不需要加 `override` 修饰符。
|
||||
相反,如果你编写了一个和父类便利构造器相匹配的子类构造器,由于子类不能直接调用父类的便利构造器(每个规则都在上文 [类的构造器代理规则](#initializer-delegation-for-class-types) 有所描述),因此,严格意义上来讲,你的子类并未对一个父类构造器提供重写。最后的结果就是,你在子类中“重写”一个父类便利构造器时,不需要加 `override` 修饰符。
|
||||
|
||||
在下面的例子中定义了一个叫 `Vehicle` 的基类。基类中声明了一个存储型属性 `numberOfWheels`,它是默认值为 `Int` 类型的 `0`。`numberOfWheels` 属性用在一个描述车辆特征 `String` 类型为 `descrpiption` 的计算型属性中:
|
||||
|
||||
@ -504,7 +504,7 @@ class Vehicle {
|
||||
}
|
||||
```
|
||||
|
||||
`Vehicle` 类只为存储型属性提供默认值,也没有提供自定义构造器。因此,它会自动获得一个默认构造器,具体内容请参考 [默认构造器](#default_initializers)。默认构造器(如果有的话)总是类中的指定构造器,可以用于创建 `numberOfWheels` 为 `0` 的 `Vehicle` 实例:
|
||||
`Vehicle` 类只为存储型属性提供默认值,也没有提供自定义构造器。因此,它会自动获得一个默认构造器,具体内容请参考 [默认构造器](#default-initializers)。默认构造器(如果有的话)总是类中的指定构造器,可以用于创建 `numberOfWheels` 为 `0` 的 `Vehicle` 实例:
|
||||
|
||||
```swift
|
||||
let vehicle = Vehicle()
|
||||
@ -642,11 +642,11 @@ class RecipeIngredient: Food {
|
||||
|
||||

|
||||
|
||||
`RecipeIngredient` 类拥有一个指定构造器 `init(name: String, quantity: Int)`,它可以用来填充 `RecipeIngredient` 实例的所有属性值。这个构造器一开始先将传入的 `quantity` 实参赋值给 `quantity` 属性,这个属性也是唯一在 `RecipeIngredient` 中新引入的属性。随后,构造器向上代理到父类 `Food` 的 `init(name: String)`。这个过程满足 [两段式构造过程](#two_phase_initialization) 中的安全检查 1。
|
||||
`RecipeIngredient` 类拥有一个指定构造器 `init(name: String, quantity: Int)`,它可以用来填充 `RecipeIngredient` 实例的所有属性值。这个构造器一开始先将传入的 `quantity` 实参赋值给 `quantity` 属性,这个属性也是唯一在 `RecipeIngredient` 中新引入的属性。随后,构造器向上代理到父类 `Food` 的 `init(name: String)`。这个过程满足 [两段式构造过程](#two-phase-initialization) 中的安全检查 1。
|
||||
|
||||
`RecipeIngredient` 也定义了一个便利构造器 `init(name: String)`,它只通过 `name` 来创建 `RecipeIngredient` 的实例。这个便利构造器假设任意 `RecipeIngredient` 实例的 `quantity` 为 `1`,所以不需要显式的质量即可创建出实例。这个便利构造器的定义可以更加方便和快捷地创建实例,并且避免了创建多个 `quantity` 为 `1` 的 `RecipeIngredient` 实例时的代码重复。这个便利构造器只是简单地横向代理到类中的指定构造器,并为 `quantity` 参数传递 `1`。
|
||||
|
||||
`RecipeIngredient` 的便利构造器 `init(name: String)` 使用了跟 `Food` 中指定构造器 `init(name: String)` 相同的形参。由于这个便利构造器重写了父类的指定构造器 `init(name: String)`,因此必须在前面使用 `override` 修饰符(参见 [构造器的继承和重写](#initializer_inheritance_and_overriding))。
|
||||
`RecipeIngredient` 的便利构造器 `init(name: String)` 使用了跟 `Food` 中指定构造器 `init(name: String)` 相同的形参。由于这个便利构造器重写了父类的指定构造器 `init(name: String)`,因此必须在前面使用 `override` 修饰符(参见 [构造器的继承和重写](#initializer-inheritance-and-overriding))。
|
||||
|
||||
尽管 `RecipeIngredient` 将父类的指定构造器重写为了便利构造器,但是它依然提供了父类的所有指定构造器的实现。因此,`RecipeIngredient` 会自动继承父类的所有便利构造器。
|
||||
|
||||
|
||||
@ -156,7 +156,7 @@ class Address {
|
||||
|
||||
## 通过可选链式调用访问属性 {#accessing-properties-through-optional-chaining}
|
||||
|
||||
正如 [使用可选链式调用代替强制展开](#optional_chaining_as_an_alternative_to_forced_unwrapping) 中所述,可以通过可选链式调用在一个可选值上访问它的属性,并判断访问是否成功。
|
||||
正如 [使用可选链式调用代替强制展开](#optional-chaining-as-an-alternative-to-forced-unwrapping) 中所述,可以通过可选链式调用在一个可选值上访问它的属性,并判断访问是否成功。
|
||||
|
||||
使用前面定义过的类,创建一个 `Person` 实例,然后像之前一样,尝试访问 `numberOfRooms` 属性:
|
||||
|
||||
@ -212,7 +212,7 @@ func printNumberOfRooms() {
|
||||
}
|
||||
```
|
||||
|
||||
这个方法没有返回值。然而,没有返回值的方法具有隐式的返回类型 `Void`,如 [无返回值函数](./06_Functions.md#functions_without_return_values) 中所述。这意味着没有返回值的方法也会返回 `()`,或者说空的元组。
|
||||
这个方法没有返回值。然而,没有返回值的方法具有隐式的返回类型 `Void`,如 [无返回值函数](./06_Functions.md#functions-without-return-values) 中所述。这意味着没有返回值的方法也会返回 `()`,或者说空的元组。
|
||||
|
||||
如果在可选值上通过可选链式调用来调用这个方法,该方法的返回类型会是 `Void?`,而不是 `Void`,因为通过可选链式调用得到的返回值都是可选的。这样我们就可以使用 `if` 语句来判断能否成功调用 `printNumberOfRooms()` 方法,即使方法本身没有定义返回值。通过判断返回值是否为 `nil` 可以判断调用是否成功:
|
||||
|
||||
@ -225,7 +225,7 @@ if john.residence?.printNumberOfRooms() != nil {
|
||||
// 打印“It was not possible to print the number of rooms.”
|
||||
```
|
||||
|
||||
同样的,可以据此判断通过可选链式调用为属性赋值是否成功。在上面的 [通过可选链式调用访问属性](#accessing_properties_through_optional_chaining) 的例子中,我们尝试给 `john.residence` 中的 `address` 属性赋值,即使 `residence` 为 `nil`。通过可选链式调用给属性赋值会返回 `Void?`,通过判断返回值是否为 `nil` 就可以知道赋值是否成功:
|
||||
同样的,可以据此判断通过可选链式调用为属性赋值是否成功。在上面的 [通过可选链式调用访问属性](#accessing-properties-through-optional-chaining) 的例子中,我们尝试给 `john.residence` 中的 `address` 属性赋值,即使 `residence` 为 `nil`。通过可选链式调用给属性赋值会返回 `Void?`,通过判断返回值是否为 `nil` 就可以知道赋值是否成功:
|
||||
|
||||
```swift
|
||||
if (john.residence?.address = someAddress) != nil {
|
||||
|
||||
@ -194,7 +194,7 @@ do {
|
||||
|
||||
如果 `vend(itemNamed:)` 抛出的是一个 `VendingMachineError` 类型的错误,`nourish(with:)` 会打印一条消息,否则 `nourish(with:)` 会将错误抛给它的调用方。这个错误之后会被通用的 `catch` 语句捕获。
|
||||
|
||||
### 将错误转换成可选值 {#converting_errors_to_optional_values}
|
||||
### 将错误转换成可选值 {#converting-errors-to-optional-values}
|
||||
|
||||
可以使用 `try?` 通过将错误转换成一个可选值来处理错误。如果是在计算 `try?` 表达式时抛出错误,该表达式的结果就为 `nil`。例如,在下面的代码中,`x` 和 `y` 有着相同的数值和等价的含义:
|
||||
|
||||
@ -225,7 +225,7 @@ func fetchData() -> Data? {
|
||||
}
|
||||
```
|
||||
|
||||
### 禁用错误传递 {#disabling_error_propagation}
|
||||
### 禁用错误传递 {#disabling-error-propagation}
|
||||
|
||||
有时你知道某个 `throwing` 函数实际上在运行时是不会抛出错误的,在这种情况下,你可以在表达式前面写 `try!` 来禁用错误传递,这会把调用包装在一个不会有错误抛出的运行时断言中。如果真的抛出了错误,你会得到一个运行时错误。
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
类型转换在 Swift 中使用 `is` 和 `as` 操作符实现。这两个操作符分别提供了一种简单达意的方式去检查值的类型或者转换它的类型。
|
||||
|
||||
你也可以用它来检查一个类型是否遵循了某个协议,就像在 [检验协议遵循](./21_Protocols.md#checking_for_protocol_conformance) 部分讲述的一样。
|
||||
你也可以用它来检查一个类型是否遵循了某个协议,就像在 [检验协议遵循](./21_Protocols.md#checking-for-protocol-conformance) 部分讲述的一样。
|
||||
|
||||
## 为类型转换定义类层次 {#defining-a-class-hierarchy-for-type-casting}
|
||||
|
||||
|
||||
@ -63,7 +63,7 @@ struct BlackjackCard {
|
||||
|
||||
`BlackjackCard` 结构体拥有两个属性——`rank` 与 `suit`。它也同样定义了一个计算型属性 `description`,`description` 属性用 `rank` 和 `suit` 中的内容来构建对扑克牌名字和数值的描述。该属性使用可选绑定来检查可选类型 `second` 是否有值,若有值,则在原有的描述中增加对 `second` 的描述。
|
||||
|
||||
因为 `BlackjackCard` 是一个没有自定义构造器的结构体,在 [结构体的逐一成员构造器](./14_Initialization.md#memberwise_initializers_for_structure_types) 中可知,结构体有默认的成员构造器,所以你可以用默认的构造器去初始化新常量 `theAceOfSpades`:
|
||||
因为 `BlackjackCard` 是一个没有自定义构造器的结构体,在 [结构体的逐一成员构造器](./14_Initialization.md#memberwise-initializers-for-structure-types) 中可知,结构体有默认的成员构造器,所以你可以用默认的构造器去初始化新常量 `theAceOfSpades`:
|
||||
|
||||
```swift
|
||||
let theAceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
|
||||
|
||||
@ -143,7 +143,7 @@ print("And another one: \(generator.random())")
|
||||
|
||||
## 异变方法要求 {#mutating-method-requirements}
|
||||
|
||||
有时需要在方法中改变(或*异变*)方法所属的实例。例如,在值类型(即结构体和枚举)的实例方法中,将 `mutating` 关键字作为方法的前缀,写在 `func` 关键字之前,表示可以在该方法中修改它所属的实例以及实例的任意属性的值。这一过程在 [在实例方法中修改值类型](./11_Methods.md#modifying_value_types_from_within_instance_methods) 章节中有详细描述。
|
||||
有时需要在方法中改变(或*异变*)方法所属的实例。例如,在值类型(即结构体和枚举)的实例方法中,将 `mutating` 关键字作为方法的前缀,写在 `func` 关键字之前,表示可以在该方法中修改它所属的实例以及实例的任意属性的值。这一过程在 [在实例方法中修改值类型](./11_Methods.md#modifying-value-types-from-within-instance-methods) 章节中有详细描述。
|
||||
|
||||
如果你在协议中定义了一个实例方法,该方法会改变遵循该协议的类型的实例,那么在定义协议时需要在方法前加 `mutating` 关键字。这使得结构体和枚举能够遵循此协议并满足此方法要求。
|
||||
|
||||
@ -206,11 +206,11 @@ class SomeClass: SomeProtocol {
|
||||
|
||||
使用 `required` 修饰符可以确保所有子类也必须提供此构造器实现,从而也能遵循协议。
|
||||
|
||||
关于 `required` 构造器的更多内容,请参考 [必要构造器](./14_Initialization.md#required_initializers)。
|
||||
关于 `required` 构造器的更多内容,请参考 [必要构造器](./14_Initialization.md#required-initializers)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 如果类已经被标记为 `final`,那么不需要在协议构造器的实现中使用 `required` 修饰符,因为 `final` 类不能有子类。关于 `final` 修饰符的更多内容,请参见 [防止重写](./13_Inheritance.md#preventing_overrides)。
|
||||
> 如果类已经被标记为 `final`,那么不需要在协议构造器的实现中使用 `required` 修饰符,因为 `final` 类不能有子类。关于 `final` 修饰符的更多内容,请参见 [防止重写](./13_Inheritance.md#preventing-overrides)。
|
||||
|
||||
如果一个子类重写了父类的指定构造器,并且该构造器满足了某个协议的要求,那么该构造器的实现需要同时标注 `required` 和 `override` 修饰符:
|
||||
|
||||
@ -236,7 +236,7 @@ class SomeSubClass: SomeSuperClass, SomeProtocol {
|
||||
|
||||
### 可失败构造器要求 {#failable-initializer-requirements}
|
||||
|
||||
协议还可以为遵循协议的类型定义可失败构造器要求,详见 [可失败构造器](./14_Initialization.md#failable_initializers)。
|
||||
协议还可以为遵循协议的类型定义可失败构造器要求,详见 [可失败构造器](./14_Initialization.md#failable-initializers)。
|
||||
|
||||
遵循协议的类型可以通过可失败构造器(`init?`)或非可失败构造器(`init`)来满足协议中定义的可失败构造器要求。协议中定义的非可失败构造器要求可以通过非可失败构造器(`init`)或隐式解包可失败构造器(`init!`)来满足。
|
||||
|
||||
@ -312,7 +312,7 @@ protocol DiceGameDelegate {
|
||||
|
||||
`DiceGame` 协议可以被任意涉及骰子的游戏遵循。
|
||||
|
||||
`DiceGameDelegate` 协议可以被任意类型遵循,用来追踪 `DiceGame` 的游戏过程。为了防止强引用导致的循环引用问题,可以把协议声明为弱引用,更多相关的知识请看 [类实例之间的循环强引用](./24_Automatic_Reference_Counting.md#strong_reference_cycles_between_class_instances),当协议标记为类专属可以使 `SnakesAndLadders` 类在声明协议时强制要使用弱引用。若要声明类专属的协议就必须继承于 `AnyObject` ,更多请看 [类专属的协议](#class_only_protocol)。
|
||||
`DiceGameDelegate` 协议可以被任意类型遵循,用来追踪 `DiceGame` 的游戏过程。为了防止强引用导致的循环引用问题,可以把协议声明为弱引用,更多相关的知识请看 [类实例之间的循环强引用](./24_Automatic_Reference_Counting.md#strong-reference-cycles-between-class-instances),当协议标记为类专属可以使 `SnakesAndLadders` 类在声明协议时强制要使用弱引用。若要声明类专属的协议就必须继承于 `AnyObject` ,更多请看 [类专属的协议](#class-only-protocol)。
|
||||
|
||||
如下所示,`SnakesAndLadders` 是 [控制流](./05_Control_Flow.md) 章节引入的蛇梯棋游戏的新版本。新版本使用 `Dice` 实例作为骰子,并且实现了 `DiceGame` 和 `DiceGameDelegate` 协议,后者用来记录游戏的过程:
|
||||
|
||||
@ -455,7 +455,7 @@ print(game.textualDescription)
|
||||
|
||||
## 有条件地遵循协议 {#Conditionally-Conforming-to-a-Protocol}
|
||||
|
||||
泛型类型可能只在某些情况下满足一个协议的要求,比如当类型的泛型形式参数遵循对应协议时。你可以通过在扩展类型时列出限制让泛型类型有条件地遵循某协议。在你采纳协议的名字后面写泛型 `where` 分句。更多关于泛型 `where` 分句,见 [泛型 Where 分句](./22_Generics.md##where_clauses)。
|
||||
泛型类型可能只在某些情况下满足一个协议的要求,比如当类型的泛型形式参数遵循对应协议时。你可以通过在扩展类型时列出限制让泛型类型有条件地遵循某协议。在你采纳协议的名字后面写泛型 `where` 分句。更多关于泛型 `where` 分句,见 [泛型 Where 分句](./22_Generics.md##where-clauses)。
|
||||
|
||||
下面的扩展让 `Array` 类型只要在存储遵循 `TextRepresentable` 协议的元素时就遵循 `TextRepresentable` 协议。
|
||||
|
||||
@ -500,7 +500,7 @@ print(somethingTextRepresentable.textualDescription)
|
||||
|
||||
## 协议类型的集合 {#collections-of-protocol-types}
|
||||
|
||||
协议类型可以在数组或者字典这样的集合中使用,在 [协议类型](./21_Protocols.md##protocols_as_types) 提到了这样的用法。下面的例子创建了一个元素类型为 `TextRepresentable` 的数组:
|
||||
协议类型可以在数组或者字典这样的集合中使用,在 [协议类型](./21_Protocols.md##protocols-as-types) 提到了这样的用法。下面的例子创建了一个元素类型为 `TextRepresentable` 的数组:
|
||||
|
||||
```swift
|
||||
let things: [TextRepresentable] = [game, d12, simonTheHamster]
|
||||
@ -588,7 +588,7 @@ protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 当协议定义的要求需要遵循协议的类型必须是引用语义而非值语义时,应该采用类类型专属协议。关于引用语义和值语义的更多内容,请查看 [结构体和枚举是值类型](./09_Classes_and_Structures.md#structures_and_enumerations_are_value_types) 和 [类是引用类型](./09_Classes_and_Structures.md#classes_are_reference_types)。
|
||||
> 当协议定义的要求需要遵循协议的类型必须是引用语义而非值语义时,应该采用类类型专属协议。关于引用语义和值语义的更多内容,请查看 [结构体和枚举是值类型](./09_Classes_and_Structures.md#structures-and-enumerations-are-value-types) 和 [类是引用类型](./09-Classes-and-Structures.md#classes-are-reference-types)。
|
||||
|
||||
## 协议合成 {#protocol-composition}
|
||||
|
||||
@ -883,7 +883,7 @@ extension PrettyTextRepresentable {
|
||||
|
||||
### 为协议扩展添加限制条件 {#adding-constraints-to-protocol-extensions}
|
||||
|
||||
在扩展协议的时候,可以指定一些限制条件,只有遵循协议的类型满足这些限制条件时,才能获得协议扩展提供的默认实现。这些限制条件写在协议名之后,使用 `where` 子句来描述,正如 [泛型 Where 子句](./22_Generics.md#where_clauses) 中所描述的。
|
||||
在扩展协议的时候,可以指定一些限制条件,只有遵循协议的类型满足这些限制条件时,才能获得协议扩展提供的默认实现。这些限制条件写在协议名之后,使用 `where` 子句来描述,正如 [泛型 Where 子句](./22_Generics.md#where-clauses) 中所描述的。
|
||||
|
||||
例如,你可以扩展 `Collection` 协议,适用于集合中的元素遵循了 `Equatable` 协议的情况。通过限制集合元素遵循 `Equatable` 协议, 作为标准库的一部分, 你可以使用 `==` 和 `!=` 操作符来检查两个元素的等价性和非等价性。
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ func swapTwoInts(_ a: inout Int, _ b: inout Int) {
|
||||
}
|
||||
```
|
||||
|
||||
这个函数使用输入输出参数(`inout`)来交换 `a` 和 `b` 的值,具体请参考 [输入输出参数](./06_Functions.md#in_out_parameters)。
|
||||
这个函数使用输入输出参数(`inout`)来交换 `a` 和 `b` 的值,具体请参考 [输入输出参数](./06_Functions.md#in-out-parameters)。
|
||||
|
||||
`swapTwoInts(_:_:)` 函数将 `b` 的原始值换成了 `a`,将 `a` 的原始值换成了 `b`,你可以调用这个函数来交换两个 `Int` 类型变量:
|
||||
|
||||
@ -410,7 +410,7 @@ struct Stack<Element>: Container {
|
||||
|
||||
### 扩展现有类型来指定关联类型 {#extending-an-existing-type-to-specify-an-associated-type}
|
||||
|
||||
[在扩展添加协议一致性](./21_Protocols.md#adding_protocol_conformance_with_an_extension) 中描述了如何利用扩展让一个已存在的类型遵循一个协议,这包括使用了关联类型协议。
|
||||
[在扩展添加协议一致性](./21_Protocols.md#adding-protocol-conformance-with-an-extension) 中描述了如何利用扩展让一个已存在的类型遵循一个协议,这包括使用了关联类型协议。
|
||||
|
||||
Swift 的 `Array` 类型已经提供 `append(_:)` 方法,`count` 属性,以及带有 `Int` 索引的下标来检索其元素。这三个功能都符合 `Container` 协议的要求,也就意味着你只需声明 `Array` 遵循`Container` 协议,就可以扩展 Array,使其遵从 Container 协议。你可以通过一个空扩展来实现这点,正如通过扩展采纳协议中的描述:
|
||||
|
||||
@ -486,7 +486,7 @@ extension IntStack: SuffixableContainer {
|
||||
|
||||
## 泛型 Where 语句 {#where-clauses}
|
||||
|
||||
[类型约束](#type_constraints) 让你能够为泛型函数、下标、类型的类型参数定义一些强制要求。
|
||||
[类型约束](#type-constraints) 让你能够为泛型函数、下标、类型的类型参数定义一些强制要求。
|
||||
|
||||
对关联类型添加约束通常是非常有用的。你可以通过定义一个泛型 `where` 子句来实现。通过泛型 `where` 子句让关联类型遵从某个特定的协议,以及某个特定的类型参数和关联类型必须类型相同。你可以通过将 `where` 关键字紧跟在类型参数列表后面来定义 `where` 子句,`where` 子句后跟一个或者多个针对关联类型的约束,以及一个或多个类型参数和关联类型间的相等关系。你可以在函数体或者类型的大括号之前添加 `where` 子句。
|
||||
|
||||
|
||||
@ -133,7 +133,7 @@ print(opaqueJoinedTriangles.draw())
|
||||
// *
|
||||
```
|
||||
|
||||
这个例子中 `opaqueJoinedTriangles` 的值和前文 [不透明类型解决的问题](#the-problem-that-opaque-types-solve) 中关于泛型的那个例子中的 `joinedTriangles` 完全一样。不过和前文不一样的是,`flip(_:)` 和 `join(_:_:)` 将对泛型参数的操作后的返回结果包装成了不透明类型,这样保证了在结果中泛型参数类型不可见。两个函数都是泛型函数,因为他们都依赖于泛型参数,而泛型参数又将 `FlippedShape` 和 `JoinedShape` 所需要的类型信息传递给它们。
|
||||
这个例子中 `opaqueJoinedTriangles` 的值和前文 [不透明类型解决的问题](#the-problem-that-opaque-types-solve) 中关于泛型的那个例子中的 `joinedTriangles` 完全一样。不过和前文不一样的是,`flip(-:)` 和 `join(-:-:)` 将对泛型参数的操作后的返回结果包装成了不透明类型,这样保证了在结果中泛型参数类型不可见。两个函数都是泛型函数,因为他们都依赖于泛型参数,而泛型参数又将 `FlippedShape` 和 `JoinedShape` 所需要的类型信息传递给它们。
|
||||
|
||||
如果函数中有多个地方返回了不透明类型,那么所有可能的返回值都必须是同一类型。即使对于泛型函数,不透明返回类型可以使用泛型参数,但仍需保证返回类型唯一。比如,下面就是一个*非法*示例 —— 包含针对 `Square` 类型进行特殊处理的翻转函数。
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
Swift 使用*自动引用计数(ARC)*机制来跟踪和管理你的应用程序的内存。通常情况下,Swift 内存管理机制会一直起作用,你无须自己来考虑内存的管理。ARC 会在类的实例不再被使用时,自动释放其占用的内存。
|
||||
|
||||
然而在少数情况下,为了能帮助你管理内存,ARC 需要更多的,代码之间关系的信息。本章描述了这些情况,并且为你示范怎样才能使 ARC 来管理你的应用程序的所有内存。在 Swift 使用 ARC 与在 Obejctive-C 中使用 ARC 非常类似,具体请参考 [过渡到 ARC 的发布说明](https://developer.apple.com/library/content/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226)。
|
||||
然而在少数情况下,为了能帮助你管理内存,ARC 需要更多的,代码之间关系的信息。本章描述了这些情况,并且为你示范怎样才能使 ARC 来管理你的应用程序的所有内存。在 Swift 使用 ARC 与在 Obejctive-C 中使用 ARC 非常类似,具体请参考 [过渡到 ARC 的发布说明](https://developer.apple.com/library/content/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple-ref/doc/uid/TP40011226)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
@ -87,7 +87,7 @@ reference3 = nil
|
||||
|
||||
然而,我们可能会写出一个类实例的强引用数*永远不能*变成 `0` 的代码。如果两个类实例互相持有对方的强引用,因而每个实例都让对方一直存在,就是这种情况。这就是所谓的*循环强引用*。
|
||||
|
||||
你可以通过定义类之间的关系为弱引用或无主引用,来替代强引用,从而解决循环强引用的问题。具体的过程在 [解决类实例之间的循环强引用](#resolving_strong_reference_cycles_between_class_instances) 中有描述。不管怎样,在你学习怎样解决循环强引用之前,很有必要了解一下它是怎样产生的。
|
||||
你可以通过定义类之间的关系为弱引用或无主引用,来替代强引用,从而解决循环强引用的问题。具体的过程在 [解决类实例之间的循环强引用](#resolving-strong-reference-cycles-between-class-instances) 中有描述。不管怎样,在你学习怎样解决循环强引用之前,很有必要了解一下它是怎样产生的。
|
||||
|
||||
下面展示了一个不经意产生循环强引用的例子。例子定义了两个类:`Person` 和 `Apartment`,用来建模公寓和它其中的居民:
|
||||
|
||||
@ -357,9 +357,9 @@ class City {
|
||||
|
||||
为了建立两个类的依赖关系,`City` 的构造器接受一个 `Country` 实例作为参数,并且将实例保存到 `country` 属性。
|
||||
|
||||
`Country` 的构造器调用了 `City` 的构造器。然而,只有 `Country` 的实例完全初始化后,`Country` 的构造器才能把 `self` 传给 `City` 的构造器。在 [两段式构造过程](./14_Initialization.md#two_phase_initialization) 中有具体描述。
|
||||
`Country` 的构造器调用了 `City` 的构造器。然而,只有 `Country` 的实例完全初始化后,`Country` 的构造器才能把 `self` 传给 `City` 的构造器。在 [两段式构造过程](./14_Initialization.md#two-phase-initialization) 中有具体描述。
|
||||
|
||||
为了满足这种需求,通过在类型结尾处加上感叹号(`City!`)的方式,将 `Country` 的 `capitalCity` 属性声明为隐式解包可选值类型的属性。这意味着像其他可选类型一样,`capitalCity` 属性的默认值为 `nil`,但是不需要展开它的值就能访问它。在 [隐式解包可选值](./01_The_Basics.md#implicityly_unwrapped_optionals) 中有描述。
|
||||
为了满足这种需求,通过在类型结尾处加上感叹号(`City!`)的方式,将 `Country` 的 `capitalCity` 属性声明为隐式解包可选值类型的属性。这意味着像其他可选类型一样,`capitalCity` 属性的默认值为 `nil`,但是不需要展开它的值就能访问它。在 [隐式解包可选值](./01_The_Basics.md#implicityly-unwrapped-optionals) 中有描述。
|
||||
|
||||
由于 `capitalCity` 默认值为 `nil`,一旦 `Country` 的实例在构造器中给 `name` 属性赋值后,整个初始化过程就完成了。这意味着一旦 `name` 属性被赋值后,`Country` 的构造器就能引用并传递隐式的 `self`。`Country` 的构造器在赋值 `capitalCity` 时,就能将 `self` 作为参数传递给 `City` 的构造器。
|
||||
|
||||
@ -453,7 +453,7 @@ print(paragraph!.asHTML())
|
||||
|
||||

|
||||
|
||||
实例的 `asHTML` 属性持有闭包的强引用。但是,闭包在其闭包体内使用了 `self`(引用了 `self.name` 和 `self.text`),因此闭包捕获了 `self`,这意味着闭包又反过来持有了 `HTMLElement` 实例的强引用。这样两个对象就产生了循环强引用。(更多关于闭包捕获值的信息,请参考 [值捕获](./07_Closures.md#capturing_values))。
|
||||
实例的 `asHTML` 属性持有闭包的强引用。但是,闭包在其闭包体内使用了 `self`(引用了 `self.name` 和 `self.text`),因此闭包捕获了 `self`,这意味着闭包又反过来持有了 `HTMLElement` 实例的强引用。这样两个对象就产生了循环强引用。(更多关于闭包捕获值的信息,请参考 [值捕获](./07_Closures.md#capturing-values))。
|
||||
|
||||
> 注意
|
||||
>
|
||||
|
||||
@ -80,7 +80,7 @@ fileprivate func someFilePrivateFunction() {}
|
||||
private func somePrivateFunction() {}
|
||||
```
|
||||
|
||||
除非专门指定,否则实体默认的访问级别为 `internal`,可以查阅 [默认访问级别](#default_access_levels) 这一节。这意味着在不使用修饰符显式声明访问级别的情况下,`SomeInternalClass` 和 `someInternalConstant` 的访问级别是 `internal`:
|
||||
除非专门指定,否则实体默认的访问级别为 `internal`,可以查阅 [默认访问级别](#default-access-levels) 这一节。这意味着在不使用修饰符显式声明访问级别的情况下,`SomeInternalClass` 和 `someInternalConstant` 的访问级别是 `internal`:
|
||||
|
||||
```swift
|
||||
class SomeInternalClass {} // 隐式 internal
|
||||
@ -142,7 +142,7 @@ func someFunction() -> (SomeInternalClass, SomePrivateClass) {
|
||||
}
|
||||
```
|
||||
|
||||
我们可以看到,这个函数的返回类型是一个元组,该元组中包含两个自定义的类(可查阅 [自定义类型](#custom_types))。其中一个类的访问级别是 `internal`,另一个的访问级别是 `private`,所以根据元组访问级别的原则,该元组的访问级别是 `private`(元组的访问级别与元组中访问级别最低的类型一致)。
|
||||
我们可以看到,这个函数的返回类型是一个元组,该元组中包含两个自定义的类(可查阅 [自定义类型](#custom-types))。其中一个类的访问级别是 `internal`,另一个的访问级别是 `private`,所以根据元组访问级别的原则,该元组的访问级别是 `private`(元组的访问级别与元组中访问级别最低的类型一致)。
|
||||
|
||||
因为该函数返回类型的访问级别是 `private`,所以你必须使用 `private` 修饰符来明确指定该函数的访问级别:
|
||||
|
||||
@ -277,13 +277,13 @@ public struct TrackedString {
|
||||
|
||||
## 构造器 {#initializers}
|
||||
|
||||
自定义构造器的访问级别可以低于或等于其所属类型的访问级别。唯一的例外是 [必要构造器](./14_Initialization.md#required_initializers),它的访问级别必须和所属类型的访问级别相同。
|
||||
自定义构造器的访问级别可以低于或等于其所属类型的访问级别。唯一的例外是 [必要构造器](./14_Initialization.md#required-initializers),它的访问级别必须和所属类型的访问级别相同。
|
||||
|
||||
如同函数或方法的参数,构造器参数的访问级别也不能低于构造器本身的访问级别。
|
||||
|
||||
### 默认构造器 {#default-initializers}
|
||||
|
||||
如 [默认构造器](./14_Initialization.md#default_initializers) 所述,Swift 会为结构体和类提供一个默认的无参数的构造器,只要它们为所有存储型属性设置了默认初始值,并且未提供自定义的构造器。
|
||||
如 [默认构造器](./14_Initialization.md#default-initializers) 所述,Swift 会为结构体和类提供一个默认的无参数的构造器,只要它们为所有存储型属性设置了默认初始值,并且未提供自定义的构造器。
|
||||
|
||||
默认构造器的访问级别与所属类型的访问级别相同,除非类型的访问级别是 `public`。如果一个类型被指定为 `public` 级别,那么默认构造器的访问级别将为 `internal`。如果你希望一个 `public` 级别的类型也能在其他模块中使用这种无参数的默认构造器,你只能自己提供一个 `public` 访问级别的无参数构造器。
|
||||
|
||||
|
||||
@ -210,7 +210,7 @@ unsignedOverflow = unsignedOverflow &- 1
|
||||
|
||||

|
||||
|
||||
溢出也会发生在有符号整型上。针对有符号整型的所有溢出加法或者减法运算都是按位运算的方式执行的,符号位也需要参与计算,正如 [按位左移、右移运算符](#bitwise_left_and_right_shift_operators) 所描述的。
|
||||
溢出也会发生在有符号整型上。针对有符号整型的所有溢出加法或者减法运算都是按位运算的方式执行的,符号位也需要参与计算,正如 [按位左移、右移运算符](#bitwise-left-and-right-shift-operators) 所描述的。
|
||||
|
||||
```Swift
|
||||
var signedOverflow = Int8.min
|
||||
@ -440,7 +440,7 @@ let afterDoubling = +++toBeDoubled
|
||||
|
||||
### 自定义中缀运算符的优先级 {#precedence-and-associativity-for-custom-infix-operators}
|
||||
|
||||
每个自定义中缀运算符都属于某个优先级组。优先级组指定了这个运算符相对于其他中缀运算符的优先级和结合性。[优先级和结合性](#precedence_and_associativity) 中详细阐述了这两个特性是如何对中缀运算符的运算产生影响的。
|
||||
每个自定义中缀运算符都属于某个优先级组。优先级组指定了这个运算符相对于其他中缀运算符的优先级和结合性。[优先级和结合性](#precedence-and-associativity) 中详细阐述了这两个特性是如何对中缀运算符的运算产生影响的。
|
||||
|
||||
而没有明确放入某个优先级组的自定义中缀运算符将会被放到一个默认的优先级组内,其优先级高于三元运算符。
|
||||
|
||||
@ -459,7 +459,7 @@ let plusMinusVector = firstVector +- secondVector
|
||||
// plusMinusVector 是一个 Vector2D 实例,并且它的值为 (4.0, -2.0)
|
||||
```
|
||||
|
||||
这个运算符把两个向量的 `x` 值相加,同时从第一个向量的 `y` 中减去第二个向量的 `y` 。因为它本质上是属于“相加型”运算符,所以将它放置在 `+` 和 `-` 等默认中缀“相加型”运算符相同的优先级组中。关于 Swift 标准库提供的运算符,以及完整的运算符优先级组和结合性设置,请参考 [运算符声明](https://developer.apple.com/documentation/swift/operator_declarations)。而更多关于优先级组以及自定义操作符和优先级组的语法,请参考 [运算符声明](./06_Declarations.md#operator_declaration)。
|
||||
这个运算符把两个向量的 `x` 值相加,同时从第一个向量的 `y` 中减去第二个向量的 `y` 。因为它本质上是属于“相加型”运算符,所以将它放置在 `+` 和 `-` 等默认中缀“相加型”运算符相同的优先级组中。关于 Swift 标准库提供的运算符,以及完整的运算符优先级组和结合性设置,请参考 [运算符声明](https://developer.apple.com/documentation/swift/operator_declarations)。而更多关于优先级组以及自定义操作符和优先级组的语法,请参考 [运算符声明](./06_Declarations.md#operator-declaration)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
|
||||
@ -19,14 +19,14 @@ Swift 语言相对较小,这是由于 Swift 代码中常用的类型、函数
|
||||
|
||||
> getter-setter 方法块语法
|
||||
>
|
||||
> *getter-setter 方法块* → { [*getter 子句*](./06_Declarations.md#getter-clause) [*setter 子句*](./06_Declarations.md#setter-clause)<sub>可选</sub> } | { [*setter 子句*](./06_Declarations.md#setter-clause) [*getter 子句*](./06_Declarations.md#getter-clause) }
|
||||
> *getter-setter 方法块* → { [*getter 子句*](./06_Declarations.md#getter-clause) [*setter 子句*](./06-Declarations.md#setter-clause)<sub>可选</sub> } | { [*setter 子句*](./06-Declarations.md#setter-clause) [*getter 子句*](./06-Declarations.md#getter-clause) }
|
||||
>
|
||||
|
||||
这个定义表明,一个 getter-setter 方法块可以由一个 getter 分句后跟一个可选的 setter 分句构成,然后用大括号括起来,或者由一个 setter 分句后跟一个 getter 分句构成,然后用大括号括起来。上述的语法产式等价于下面的两个语法产式, :
|
||||
|
||||
> getter-setter 方法块语法
|
||||
>
|
||||
> getter-setter 方法块 → { [*getter 子句*](./06_Declarations.md#getter-clause) [*setter 子句*](./06_Declarations.md#setter-clause)<sub>可选</sub> }
|
||||
> getter-setter 方法块 → { [*getter 子句*](./06_Declarations.md#getter-clause) [*setter 子句*](./06-Declarations.md#setter-clause)<sub>可选</sub> }
|
||||
>
|
||||
> getter-setter 方法块 → { [*setter 子句*](./06_Declarations.md#setter-clause) [*getter 子句*](./06_Declarations.md#getter-clause) }
|
||||
> getter-setter 方法块 → { [*setter 子句*](./06_Declarations.md#setter-clause) [*getter 子句*](./06-Declarations.md#getter-clause) }
|
||||
>
|
||||
|
||||
@ -212,7 +212,7 @@ true // 布尔值字面量
|
||||
>
|
||||
> *二进制数字* → 数值 0 到 1
|
||||
>
|
||||
> *二进制字面量字符* → [*二进制数字*](#binary-digit) | _
|
||||
> *二进制字面量字符* → [*二进制数字*](#binary-digit) | -
|
||||
>
|
||||
>
|
||||
#### binary-literal-characters {#binary-literal-characters}
|
||||
@ -229,7 +229,7 @@ true // 布尔值字面量
|
||||
>
|
||||
> *八进字数字* → 数值 0 到 7
|
||||
>
|
||||
> *八进制字符* → [*八进字数字*](#octal-digit) | _
|
||||
> *八进制字符* → [*八进字数字*](#octal-digit) | -
|
||||
>
|
||||
>
|
||||
#### octal-literal-characters {#octal-literal-characters}
|
||||
@ -251,7 +251,7 @@ true // 布尔值字面量
|
||||
>
|
||||
> *十进制数字组* → [*十进制数字*](#decimal-digit) [*十进制数字组*](#decimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
> *十进制字符* → [*十进制数字*](#decimal-digit) | _
|
||||
> *十进制字符* → [*十进制数字*](#decimal-digit) | -
|
||||
>
|
||||
> *十进制字符组* → [*十进制字符*](#decimal-literal-characters) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
@ -265,7 +265,7 @@ true // 布尔值字面量
|
||||
>
|
||||
> *十六进制数字* → 数值 0 到 9, 字母 a 到 f, 或 A 到 F
|
||||
>
|
||||
> *十六进制字符* → [*十六进制数字*](#hexadecimal-digit) | _
|
||||
> *十六进制字符* → [*十六进制数字*](#hexadecimal-digit) | -
|
||||
>
|
||||
>
|
||||
#### hexadecimal-literal-characters {#hexadecimal-literal-characters}
|
||||
@ -545,7 +545,7 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
|
||||
|
||||
在某些特定的设计中 ,以 `<` 或 `>` 开头的运算符会被分离成两个或多个符号,剩余部分可能会以同样的方式被再次分离。因此,在 `Dictionary<String, Array<Int>>` 中没有必要添加空白来消除闭合字符 `>` 的歧义。在这个例子中, 闭合字符 `>` 不会被视为单独的符号,因而不会被错误解析为 `>>` 运算符。
|
||||
|
||||
要学习如何自定义运算符,请参考 [自定义运算符](../02_language_guide/27_Advanced_Operators.md#custom_operators) 和 [运算符声明](./06_Declarations.md#operator_declaration)。要学习如何重载运算符,请参考 [运算符函数](../02_language_guide/27_Advanced_Operators.md#operator_functions)。
|
||||
要学习如何自定义运算符,请参考 [自定义运算符](../02_language_guide/27_Advanced_Operators.md#custom-operators) 和 [运算符声明](./06-Declarations.md#operator-declaration)。要学习如何重载运算符,请参考 [运算符函数](../02-language-guide/27-Advanced-Operators.md#operator-functions)。
|
||||
|
||||
> 运算符语法
|
||||
>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
Swift 语言存在两种类型:命名型类型和复合型类型。*命名型类型*是指定义时可以给定名字的类型。命名型类型包括类、结构体、枚举和协议。比如,一个用户定义类 `MyClass` 的实例拥有类型 `MyClass`。除了用户定义的命名型类型,Swift 标准库也定义了很多常用的命名型类型,包括那些表示数组、字典和可选值的类型。
|
||||
|
||||
那些通常被其它语言认为是基本或原始的数据型类型,比如表示数字、字符和字符串的类型,实际上就是命名型类型,这些类型在 Swift 标准库中是使用结构体来定义和实现的。因为它们是命名型类型,因此你可以按照 [扩展](../02_language_guide/20_Extensions.md) 和 [扩展声明](./06_Declarations.md#extension_declaration) 中讨论的那样,声明一个扩展来增加它们的行为以满足你程序的需求。
|
||||
那些通常被其它语言认为是基本或原始的数据型类型,比如表示数字、字符和字符串的类型,实际上就是命名型类型,这些类型在 Swift 标准库中是使用结构体来定义和实现的。因为它们是命名型类型,因此你可以按照 [扩展](../02_language_guide/20_Extensions.md) 和 [扩展声明](./06_Declarations.md#extension-declaration) 中讨论的那样,声明一个扩展来增加它们的行为以满足你程序的需求。
|
||||
|
||||
*复合型类型*是没有名字的类型,它由 Swift 本身定义。Swift 存在两种复合型类型:函数类型和元组类型。一个复合型类型可以包含命名型类型和其它复合型类型。例如,元组类型 `(Int, (Int, Int))` 包含两个元素:第一个是命名型类型 `Int`,第二个是另一个复合型类型 `(Int, Int)`。
|
||||
|
||||
@ -80,7 +80,7 @@ var someValue: ExampleModule.MyType
|
||||
>
|
||||
|
||||
#### type-identifier {#type-identifier}
|
||||
> *类型标识符* → [*类型名称*](#type-name) [*泛型实参子句*](./09_Generic_Parameters_and_Arguments.md#generic_argument_clause)<sub>可选</sub> | [*类型名称*](#type-name) [*泛型实参子句*](./09_Generic_Parameters_and_Arguments.md#generic_argument_clause)<sub>可选</sub> **.** [*类型标识符*](#type-identifier)
|
||||
> *类型标识符* → [*类型名称*](#type-name) [*泛型实参子句*](./09-Generic-Parameters-and-Arguments.md#generic-argument-clause)<sub>可选</sub> | [*类型名称*](#type-name) [*泛型实参子句*](./09-Generic-Parameters-and-Arguments.md#generic-argument-clause)<sub>可选</sub> **.** [*类型标识符*](#type-identifier)
|
||||
|
||||
#### type-name {#type-name}
|
||||
> *类型名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
@ -88,7 +88,7 @@ var someValue: ExampleModule.MyType
|
||||
## 元组类型 {#tuple-type-h}
|
||||
*元组类型*是使用括号括起来的零个或多个类型,类型间用逗号隔开。
|
||||
|
||||
你可以使用元组类型作为一个函数的返回类型,这样就可以使函数返回多个值。你也可以命名元组类型中的元素,然后用这些名字来引用每个元素的值。元素的名字由一个标识符紧跟一个冒号 `(:)` 组成。[函数和多返回值](../02_language_guide/06_Functions.md#functions_with_multiple_return_values) 章节里有一个展示上述特性的例子。
|
||||
你可以使用元组类型作为一个函数的返回类型,这样就可以使函数返回多个值。你也可以命名元组类型中的元素,然后用这些名字来引用每个元素的值。元素的名字由一个标识符紧跟一个冒号 `(:)` 组成。[函数和多返回值](../02_language_guide/06_Functions.md#functions-with-multiple-return-values) 章节里有一个展示上述特性的例子。
|
||||
|
||||
当一个元组类型的元素有名字的时候,这个名字就是类型的一部分。
|
||||
|
||||
@ -131,7 +131,7 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配
|
||||
|
||||
函数类型可以拥有一个可变参数在*形参类型*中。从语法角度上讲,可变参数由一个基础类型名字紧随三个点(`...`)组成,如 `Int...`。可变参数被认为是一个包含了基础类型元素的数组。即 `Int...` 就是 `[Int]`。关于使用可变参数的例子,请参阅 [可变参数](../02_language_guide/06_Functions.md#variadic-parameters)。
|
||||
|
||||
为了指定一个 `in-out` 参数,可以在形参类型前加 `inout` 前缀。但是你不可以对可变参数或返回值类型使用 `inout`。关于这种形参的详细讲解请参阅 [输入输出参数](../02_language_guide/06_Functions.md#in_out_parameters)。
|
||||
为了指定一个 `in-out` 参数,可以在形参类型前加 `inout` 前缀。但是你不可以对可变参数或返回值类型使用 `inout`。关于这种形参的详细讲解请参阅 [输入输出参数](../02_language_guide/06_Functions.md#in-out-parameters)。
|
||||
|
||||
如果函数类型只有一个类型是元组类型的一个形参,那么元组类型在写函数类型的时候必须用圆括号括起来。比如说,`((Int, Int)) -> Void` 是接收一个元组 `(Int, Int)` 作为形参并且不返回任何值的函数类型。与此相对,不加括号的 `(Int, Int) -> Void` 是一个接收两个 `Int` 作为形参并且不返回任何值的函数类型。相似地,因为 `Void` 是空元组类型 `()` 的别名,函数类型 `(Void)-> Void` 与 `(()) -> ()` 是一样的 - 一个将空元组作为唯一实参的函数。但这些类型和 `() -> ()` 是不一样的 - 一个无实参的函数。
|
||||
|
||||
@ -164,7 +164,7 @@ var operation: (Int, Int) -> Int // 正确
|
||||
|
||||
如果一个函数类型包涵多个箭头(->),那么函数类型将从右向左进行组合。例如,函数类型 `(Int) -> (Int) -> Int` 可以理解为 `(Int) -> ((Int) -> Int)`,也就是说,该函数传入 `Int`,并返回另一个传入并返回 `Int` 的函数。
|
||||
|
||||
函数类型若要抛出或重抛错误就必须使用 `throws` 关键字来标记。`throws` 关键字是函数类型的一部分,非抛出函数是抛出函数的子类型。因此,在使用抛出函数的地方也可以使用不抛出函数。抛出和重抛函数的相关描述见章节 [抛出函数与方法](./06_Declarations.md#throwing_functions_and_methods) 和 [重抛函数与方法](./06_Declarations.md#rethrowing_functions_and_methods)。
|
||||
函数类型若要抛出或重抛错误就必须使用 `throws` 关键字来标记。`throws` 关键字是函数类型的一部分,非抛出函数是抛出函数的子类型。因此,在使用抛出函数的地方也可以使用不抛出函数。抛出和重抛函数的相关描述见章节 [抛出函数与方法](./06_Declarations.md#throwing-functions-and-methods) 和 [重抛函数与方法](./06-Declarations.md#rethrowing-functions-and-methods)。
|
||||
|
||||
### 对非逃逸闭包的限制 {#Restrictions for Nonescaping Closures}
|
||||
当非逃逸闭包函数是形参时,不能存储在属性、变量或任何 `Any` 类型的常量中,因为这可能导致值的逃逸。
|
||||
@ -329,7 +329,7 @@ let implicitlyUnwrappedArray: [Int]! // 正确
|
||||
|
||||
可以使用可选链式调用对隐式解析可选表达式选择性地执行操作。如果值为 `nil`,就不会执行任何操作,因此也不会产生运行错误。
|
||||
|
||||
关于隐式解析可选类型的更多细节,请参阅 [隐式解析可选类型](../02_language_guide/01_The_Basics.md#implicityly_unwrapped_optionals)。
|
||||
关于隐式解析可选类型的更多细节,请参阅 [隐式解析可选类型](../02_language_guide/01_The_Basics.md#implicityly-unwrapped-optionals)。
|
||||
|
||||
> 隐式解析可选类型语法
|
||||
>
|
||||
@ -490,12 +490,12 @@ print(type(of: z.f()))
|
||||
|
||||
其它命名型类型只能继承自或采纳一系列协议。协议类型可以继承自任意数量的其他协议。当一个协议类型继承自其它协议时,其它协议中定义的要求会被整合在一起,然后从当前协议继承的任意类型必须符合所有这些条件。
|
||||
|
||||
枚举定义中的类型继承子句可以是一系列协议,或者是指定单一的命名类型,此时枚举为其用例分配原始值。在枚举定义中使用类型继承子句来指定原始值类型的例子,请参阅 [原始值](../02_language_guide/08_Enumerations.md#raw_values)。
|
||||
枚举定义中的类型继承子句可以是一系列协议,或者是指定单一的命名类型,此时枚举为其用例分配原始值。在枚举定义中使用类型继承子句来指定原始值类型的例子,请参阅 [原始值](../02_language_guide/08_Enumerations.md#raw-values)。
|
||||
|
||||
> 类型继承子句语法
|
||||
>
|
||||
|
||||
#### type_inheritance_clause {#type-inheritance-clause}
|
||||
#### type-inheritance-clause {#type-inheritance-clause}
|
||||
> *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list)
|
||||
>
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
|
||||
关于 Swift 标准库提供的运算符的更多信息,请参阅 [*Operators Declarations*](https://developer.apple.com/documentation/swift/operator_declarations)。
|
||||
|
||||
除了标准库运算符,你也可以对某个变量使用 `&` 运算符,从而将其传递给函数的输入输出参数。更多信息,请参阅 [输入输出参数](../02_language_guide/06_Functions.md#in_out_parameters)。
|
||||
除了标准库运算符,你也可以对某个变量使用 `&` 运算符,从而将其传递给函数的输入输出参数。更多信息,请参阅 [输入输出参数](../02_language_guide/06_Functions.md#in-out-parameters)。
|
||||
|
||||
> 前缀表达式语法
|
||||
>
|
||||
@ -137,7 +137,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
|
||||
如果条件为真,那么对第一个表达式进行求值并返回结果。否则,对第二个表达式进行求值并返回结果。未使用的表达式不会进行求值。
|
||||
|
||||
关于使用三元条件运算符的例子,请参阅 [三元条件运算符](../02_language_guide/02_Basic_Operators.md#ternary_conditional_operator)。
|
||||
关于使用三元条件运算符的例子,请参阅 [三元条件运算符](../02_language_guide/02_Basic_Operators.md#ternary-conditional-operator)。
|
||||
|
||||
> 三元条件运算符语法
|
||||
>
|
||||
@ -205,7 +205,7 @@ f(x as Any)
|
||||
>
|
||||
|
||||
#### primary-expression {#primary-expression}
|
||||
> *基本表达式* → [*标识符*](./02_Lexical_Structure.md#identifier) [*泛型实参子句*](./09_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub>
|
||||
> *基本表达式* → [*标识符*](./02_Lexical_Structure.md#identifier) [*泛型实参子句*](./09-Generic-Parameters-and-Arguments.md#generic-argument-clause)<sub>可选</sub>
|
||||
>
|
||||
> *基本表达式* → [*字面量表达式*](#literal-expression)
|
||||
>
|
||||
@ -330,7 +330,7 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者
|
||||
> *playground 字面量* → **#fileLiteral ( resourceName : [*表达式*](#expression) )**
|
||||
>
|
||||
>
|
||||
#### playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**self_expression {#self-expression}
|
||||
#### playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**self-expression {#self-expression}
|
||||
>
|
||||
|
||||
### Self 表达式
|
||||
@ -434,7 +434,7 @@ struct Point {
|
||||
}
|
||||
```
|
||||
|
||||
闭包的参数声明形式跟函数一样,请参阅 [函数声明](./06_Declarations.md#function_declaration)。
|
||||
闭包的参数声明形式跟函数一样,请参阅 [函数声明](./06_Declarations.md#function-declaration)。
|
||||
|
||||
闭包还有几种特殊的形式,能让闭包使用起来更加简洁:
|
||||
|
||||
@ -460,11 +460,11 @@ myFunction { return $0 + $1 }
|
||||
myFunction { $0 + $1 }
|
||||
```
|
||||
|
||||
关于如何将闭包作为参数来传递的内容,请参阅 [函数调用表达式](#function_call_expression)。
|
||||
关于如何将闭包作为参数来传递的内容,请参阅 [函数调用表达式](#function-call-expression)。
|
||||
|
||||
使用闭包表达式时,可以不必将其存储在一个变量或常量中,例如作为函数调用的一部分来立即使用一个闭包。在上面的例子中,传入 `myFunction` 的闭包表达式就是这种立即使用类型的闭包。因此,一个闭包是否逃逸与其使用时的上下文相关。一个会被立即调用或者作为函数的非逃逸参数传递的闭包表达式是非逃逸的,否则,这个闭包表达式是逃逸的。
|
||||
|
||||
关于逃逸闭包的内容,请参阅 [逃逸闭包](./02_language_guide/07_Closures.md#escaping_closures)。
|
||||
关于逃逸闭包的内容,请参阅 [逃逸闭包](./02_language_guide/07_Closures.md#escaping-closures)。
|
||||
|
||||
## 捕获列表 {#capture-lists}
|
||||
默认情况下,闭包会捕获附近作用域中的常量和变量,并使用强引用指向它们。你可以通过一个*捕获列表*来显式指定它的捕获行为。
|
||||
@ -522,7 +522,7 @@ myFunction { [unowned self] in print(self.title) } // 无主引用捕获
|
||||
myFunction { [weak parent = self.parent] in print(parent!.title) }
|
||||
```
|
||||
|
||||
关于闭包表达式的更多信息和例子,请参阅 [闭包表达式](../02_language_guide/07_Closures.md#closure_expressions)。关于捕获列表的更多信息和例子,请参阅 [解决闭包引起的循环强引用](../02_language_guide/24_Automatic_Reference_Counting.md#resolving_strong_reference_cycles_for_closures)。
|
||||
关于闭包表达式的更多信息和例子,请参阅 [闭包表达式](../02_language_guide/07_Closures.md#closure-expressions)。关于捕获列表的更多信息和例子,请参阅 [解决闭包引起的循环强引用](../02-language-guide/24-Automatic-Reference-Counting.md#resolving-strong-reference-cycles-for-closures)。
|
||||
|
||||
> 闭包表达式语法
|
||||
>
|
||||
@ -537,13 +537,13 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
|
||||
#### closure-signature {#closure-signature}
|
||||
>
|
||||
>
|
||||
> 闭包签名* → [*参数子句*](#parameter-clause) [*函数结果*](05_Declarations.md#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) [*函数结果*](./06_Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
> *闭包签名* → [*捕获列表*](#capture-list) [*参数子句*](05-Declarations.md#parameter-clause) [*函数结果*](./06-Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
>
|
||||
> *闭包签名* → [*捕获列表*](#capture-list) [*标识符列表*](02_Lexical_Structure.md#identifier-list) [*函数结果*](./06_Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
> *闭包签名* → [*捕获列表*](#capture-list) [*标识符列表*](02-Lexical-Structure.md#identifier-list) [*函数结果*](./06-Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
>
|
||||
> *闭包签名* → [*捕获列表*](#capture-list) **in**
|
||||
>
|
||||
@ -772,7 +772,7 @@ print(interestingNumbers[keyPath: \[String: [Int]].["hexagonal"]!.count.bitWidth
|
||||
// 打印 "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.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 表达式语法
|
||||
>
|
||||
@ -823,7 +823,7 @@ class SomeClass: NSObject {
|
||||
self.property = property
|
||||
}
|
||||
}
|
||||
let selectorForMethod = #selector(SomeClass.doSomething(_:))
|
||||
let selectorForMethod = #selector(SomeClass.doSomething(-:))
|
||||
let selectorForPropertyGetter = #selector(getter: SomeClass.property)
|
||||
```
|
||||
|
||||
@ -836,7 +836,7 @@ extension SomeClass {
|
||||
@objc(doSomethingWithString:)
|
||||
func doSomething(_ x: String) { }
|
||||
}
|
||||
let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (String) -> Void)
|
||||
let anotherSelector = #selector(SomeClass.doSomething(-:) as (SomeClass) -> (String) -> Void)
|
||||
```
|
||||
|
||||
由于选择器是在编译时创建的,因此编译器可以检查方法或者属性是否存在,以及是否在运行时暴露给了 Objective-C 。
|
||||
@ -852,11 +852,11 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
|
||||
>
|
||||
|
||||
#### selector-expression {#selector-expression}
|
||||
> *选择器表达式* → __#selector__ **(** [*表达式*](#expression) **)**
|
||||
> *选择器表达式* → __#selector-- **(** [*表达式*](#expression) **)**
|
||||
>
|
||||
> *选择器表达式* → __#selector__ **(** [*getter:表达式*](#expression) **)**
|
||||
> *选择器表达式* → __#selector-- **(** [*getter:表达式*](#expression) **)**
|
||||
>
|
||||
> *选择器表达式* → __#selector__ **(** [*setter:表达式*](#expression) **)**
|
||||
> *选择器表达式* → __#selector-- **(** [*setter:表达式*](#expression) **)**
|
||||
>
|
||||
|
||||
## Key-path 字符串表达式 {#key-path-string-expressions}
|
||||
@ -899,7 +899,7 @@ print(keyPath == c.getSomeKeyPath())
|
||||
|
||||
由于 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.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 与 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)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
@ -928,7 +928,7 @@ print(keyPath == c.getSomeKeyPath())
|
||||
#### postfix-expression {#postfix-expression}
|
||||
> *后缀表达式* → [*基本表达式*](#primary-expression)
|
||||
>
|
||||
> *后缀表达式* → [*后缀表达式*](#postfix-expression) [*后缀运算符*](02_Lexical_Structure.md#postfix-operator)
|
||||
> *后缀表达式* → [*后缀表达式*](#postfix-expression) [*后缀运算符*](02-Lexical-Structure.md#postfix-operator)
|
||||
>
|
||||
> *后缀表达式* → [*函数调用表达式*](#function-call-expression)
|
||||
>
|
||||
@ -999,9 +999,9 @@ myData.someMethod {$0 == 13}
|
||||
>
|
||||
#### function-call-argument {#function-call-argument}
|
||||
>
|
||||
> *函数调用参数* → [表达式](#expression) | [标识符](02_Lexical_Structure.md#identifier) **:** [*表达式*](#expression)
|
||||
> *函数调用参数* → [表达式](#expression) | [标识符](02-Lexical-Structure.md#identifier) **:** [*表达式*](#expression)
|
||||
>
|
||||
> *函数调用参数* → [运算符](./02_Lexical_Structure.md#operator) | [标识符](./02_Lexical_Structure.md#identifier) **:** [*运算符*](./02_Lexical_Structure.md#operator)
|
||||
> *函数调用参数* → [运算符](./02_Lexical_Structure.md#operator) | [标识符](./02-Lexical-Structure.md#identifier) **:** [*运算符*](./02-Lexical-Structure.md#operator)
|
||||
>
|
||||
>
|
||||
>
|
||||
@ -1116,11 +1116,11 @@ let x = [10, 3, 20, 15, 4]
|
||||
>
|
||||
|
||||
#### explicit-member-expression {#explicit-member-expression}
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*十进制数字*] (02_Lexical_Structure.md#decimal-digit)
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*十进制数字*] (02-Lexical-Structure.md#decimal-digit)
|
||||
>
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02_Lexical_Structure.md#identifier) [*泛型实参子句*](./09_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub><br/>
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02-Lexical-Structure.md#identifier) [*泛型实参子句*](./09-Generic-Parameters-and-Arguments.md#generic-argument-clause)<sub>可选</sub><br/>
|
||||
>
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.md#identifier) **(** [*参数名称*](#argument-names) **)**
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02-Lexical-Structure.md#identifier) **(** [*参数名称*](#argument-names) **)**
|
||||
>
|
||||
|
||||
#### argument-names {#argument-names}
|
||||
@ -1159,7 +1159,7 @@ let x = [10, 3, 20, 15, 4]
|
||||
|
||||
要获取下标表达式的值,可将索引表达式作为下标表达式的参数来调用下标 getter。下标 setter 的调用方式与之一样。
|
||||
|
||||
关于下标的声明,请参阅 [协议下标声明](./06_Declarations.md#protocol_subscript_declaration)。
|
||||
关于下标的声明,请参阅 [协议下标声明](./06_Declarations.md#protocol-subscript-declaration)。
|
||||
|
||||
> 下标表达式语法
|
||||
>
|
||||
|
||||
@ -32,7 +32,7 @@
|
||||
## 循环语句 {#loop-statements}
|
||||
循环语句会根据特定的循环条件来重复执行代码块。Swift 提供三种类型的循环语句:`for-in` 语句、`while` 语句和 `repeat-while` 语句。
|
||||
|
||||
通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。
|
||||
通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break-statement) 和 [Continue 语句](#continue-statement)。
|
||||
|
||||
> 循环语句语法
|
||||
>
|
||||
@ -65,7 +65,7 @@ for item in collection {
|
||||
>
|
||||
|
||||
#### for-in-statement {#for-in-statement}
|
||||
> *for-in 语句* → **for** **case**<sub>可选</sub> [*模式*](./08_Patterns.md#pattern) **in** [*表达式*](./04_Expressions.md#expression) [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
|
||||
> *for-in 语句* → **for** **case**<sub>可选</sub> [*模式*](./08_Patterns.md#pattern) **in** [*表达式*](./04-Expressions.md#expression) [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05-Declarations.md#code-block)
|
||||
>
|
||||
|
||||
### While 语句 {#while-statements}
|
||||
@ -86,19 +86,19 @@ while condition {
|
||||
|
||||
由于会在执行循环体中的语句前判断条件的值,因此循环体中的语句可能会被执行若干次,也可能一次也不会被执行。
|
||||
|
||||
条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../02_language_guide/01_The_Basics.md#optional_binding)。
|
||||
条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../02_language_guide/01_The_Basics.md#optional-binding)。
|
||||
|
||||
> while 语句语法
|
||||
>
|
||||
>
|
||||
|
||||
#### while-statement {#while-statement}
|
||||
> *while 语句* → **while** [*条件子句*](#condition-clause) [*代码块*](./05_Declarations.md#code-block)
|
||||
> *while 语句* → **while** [*条件子句*](#condition-clause) [*代码块*](./05-Declarations.md#code-block)
|
||||
>
|
||||
|
||||
|
||||
#### condition-clause {#condition-clause}
|
||||
> *条件子句* → [*表达式*](./04_Expressions.md#expression) | [*表达式*](./04_Expressions.md#expression) **,** [*条件列表*](#condition-list)
|
||||
> *条件子句* → [*表达式*](./04_Expressions.md#expression) | [*表达式*](./04-Expressions.md#expression) **,** [*条件列表*](#condition-list)
|
||||
>
|
||||
|
||||
#### condition {#condition}
|
||||
@ -107,11 +107,11 @@ while condition {
|
||||
>
|
||||
|
||||
#### case-condition {#case-condition}
|
||||
> *case 条件* → **case** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer)
|
||||
> *case 条件* → **case** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06-Declarations.md#initializer)
|
||||
>
|
||||
|
||||
#### optional-binding-condition {#optional-binding-condition}
|
||||
> *可选绑定条件* → **let** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer) | **var** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer)
|
||||
> *可选绑定条件* → **let** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06-Declarations.md#initializer) | **var** [*模式*](./08-Patterns.md#pattern) [*构造器*](./06-Declarations.md#initializer)
|
||||
>
|
||||
|
||||
### Repeat-While 语句 {#repeat-while-statements}
|
||||
@ -132,20 +132,20 @@ repeat {
|
||||
|
||||
由于条件的值是在循环体中的语句执行后才进行判断,因此循环体中的语句至少会被执行一次。
|
||||
|
||||
条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../02_language_guide/01_The_Basics.md#optional_binding)。
|
||||
条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../02_language_guide/01_The_Basics.md#optional-binding)。
|
||||
|
||||
> repeat-while 语句语法
|
||||
>
|
||||
>
|
||||
|
||||
#### repeat-while-statement {#repeat-while-statement}
|
||||
> *repeat-while 语句* → **repeat** [*代码块*](./06_Declarations.md#code-block) **while** [*表达式*](./04_Expressions.md#expression)
|
||||
> *repeat-while 语句* → **repeat** [*代码块*](./06_Declarations.md#code-block) **while** [*表达式*](./04-Expressions.md#expression)
|
||||
>
|
||||
|
||||
## 分支语句 {#branch-statements}
|
||||
分支语句会根据一个或者多个条件来执行指定部分的代码。分支语句中的条件将会决定程序如何分支以及执行哪部分代码。Swift 提供三种类型的分支语句:`if` 语句、 `guard` 语句和 `switch` 语句。
|
||||
|
||||
`if` 语句和 `switch` 语句中的控制流可以用 `break` 语句改变,请参阅 [Break 语句](#break_statement)。
|
||||
`if` 语句和 `switch` 语句中的控制流可以用 `break` 语句改变,请参阅 [Break 语句](#break-statement)。
|
||||
|
||||
> 分支语句语法
|
||||
>
|
||||
@ -194,14 +194,14 @@ if condition 1 {
|
||||
}
|
||||
```
|
||||
|
||||
`if` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../02_language_guide/01_The_Basics.md#optional_binding)。
|
||||
`if` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../02_language_guide/01_The_Basics.md#optional-binding)。
|
||||
|
||||
> if 语句语法
|
||||
>
|
||||
>
|
||||
|
||||
#### if-statement {#if-statement}
|
||||
> *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>
|
||||
>
|
||||
|
||||
#### else-clause {#else-clause}
|
||||
@ -219,7 +219,7 @@ guard condition else {
|
||||
}
|
||||
```
|
||||
|
||||
`guard` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件也可以是一条可选绑定,请参阅 [可选绑定](../02_language_guide/01_The_Basics.md#optional_binding)。
|
||||
`guard` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件也可以是一条可选绑定,请参阅 [可选绑定](../02_language_guide/01_The_Basics.md#optional-binding)。
|
||||
|
||||
在 `guard` 语句中进行可选绑定的任何常量或者变量,其可用范围从声明开始直到作用域结束。
|
||||
|
||||
@ -230,14 +230,14 @@ guard condition else {
|
||||
* `continue`
|
||||
* `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-statement {#guard-statement}
|
||||
> *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05_Declarations.md#code-block)
|
||||
> *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05-Declarations.md#code-block)
|
||||
>
|
||||
|
||||
### Switch 语句 {#switch-statements}
|
||||
@ -303,7 +303,7 @@ case .suppressed:
|
||||
|
||||
#### 不存在隐式落入
|
||||
|
||||
当匹配到的 `case` 中的代码执行完毕后,`switch` 语句会直接退出,而不会继续执行下一个 `case` 。这就意味着,如果你想执行下一个 `case`,需要显式地在当前 `case` 中使用 `fallthrough` 语句。关于 `fallthrough` 语句的更多信息,请参阅 [Fallthrough 语句](#fallthrough_statements)。
|
||||
当匹配到的 `case` 中的代码执行完毕后,`switch` 语句会直接退出,而不会继续执行下一个 `case` 。这就意味着,如果你想执行下一个 `case`,需要显式地在当前 `case` 中使用 `fallthrough` 语句。关于 `fallthrough` 语句的更多信息,请参阅 [Fallthrough 语句](#fallthrough-statements)。
|
||||
|
||||
> switch 语句语法
|
||||
>
|
||||
@ -327,7 +327,7 @@ case .suppressed:
|
||||
>
|
||||
|
||||
#### case-item-list {#case-item-list}
|
||||
> *case 项列表* → [*模式*](./08_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> | [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> **,** [*case 项列表*](#case-item-list)
|
||||
> *case 项列表* → [*模式*](./08_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> | [*模式*](07-Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> **,** [*case 项列表*](#case-item-list)
|
||||
>
|
||||
|
||||
#### default-label {#default-label}
|
||||
@ -344,39 +344,39 @@ case .suppressed:
|
||||
>
|
||||
>
|
||||
|
||||
#### grammar_conditional-switch-case {#grammar-conditional-switch-case}
|
||||
#### grammar-conditional-switch-case {#grammar-conditional-switch-case}
|
||||
> *conditional-switch-case* → [*switch-if-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label) <sub>可选</sub> [*switch-else-directive-clause*](#switch-case-attributes-label) <sub>可选</sub> [*endif-directive*](#switch-case-attributes-label)
|
||||
>
|
||||
|
||||
#### grammar_switch-if-directive-clause {#grammar-switch-if-directive-clause}
|
||||
#### grammar-switch-if-directive-clause {#grammar-switch-if-directive-clause}
|
||||
> *switch-if-directive 语句* → [*if-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) <sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_switch-elseif-directive-clauses {#grammar-switch-elseif-directive-clauses}
|
||||
#### grammar-switch-elseif-directive-clauses {#grammar-switch-elseif-directive-clauses}
|
||||
> *switch-elseif-directive 语句(复数)* → [*elseif-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_switch-elseif-directive-clause {#grammar-switch-elseif-directive-clause}
|
||||
#### grammar-switch-elseif-directive-clause {#grammar-switch-elseif-directive-clause}
|
||||
> *switch-elseif-directive 语句* → [*elseif-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_switch-else-directive-clause {#grammar-switch-else-directive-clause}
|
||||
#### grammar-switch-else-directive-clause {#grammar-switch-else-directive-clause}
|
||||
> *switch-else-directive 语句* → [*else-directive*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) <sub>可选</sub>
|
||||
>
|
||||
|
||||
## 带标签的语句 {#labeled-statements}
|
||||
你可以在循环语句或 `switch` 语句前面加上标签,它由标签名和紧随其后的冒号(`:`)组成。在 `break` 和 `continue` 后面跟上标签名可以显式地在循环语句或 `switch` 语句中改变相应的控制流。关于这两条语句用法,请参阅 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。
|
||||
你可以在循环语句或 `switch` 语句前面加上标签,它由标签名和紧随其后的冒号(`:`)组成。在 `break` 和 `continue` 后面跟上标签名可以显式地在循环语句或 `switch` 语句中改变相应的控制流。关于这两条语句用法,请参阅 [Break 语句](#break-statement) 和 [Continue 语句](#continue-statement)。
|
||||
|
||||
标签的作用域在该标签所标记的语句内。可以嵌套使用带标签的语句,但标签名必须唯一。
|
||||
|
||||
关于使用带标签的语句的例子,请参阅 [控制流](../02_language_guide/05_Control_Flow.md) 一章中的 [带标签的语句](../02_language_guide/05_Control_Flow.md#labeled_statements)。
|
||||
关于使用带标签的语句的例子,请参阅 [控制流](../02_language_guide/05_Control_Flow.md) 一章中的 [带标签的语句](../02_language_guide/05_Control_Flow.md#labeled-statements)。
|
||||
|
||||
> 带标签的语句语法
|
||||
>
|
||||
>
|
||||
|
||||
#### labeled-statement {#labeled-statement}
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#grammar_loop-statement)
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#grammar-loop-statement)
|
||||
>
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*if 语句*](#if-statement)
|
||||
>
|
||||
@ -426,7 +426,7 @@ case .suppressed:
|
||||
|
||||
无论哪种情况,控制权都会被转移给被终止的控制流语句后面的第一行语句。
|
||||
|
||||
关于使用 `break` 语句的例子,请参阅 [控制流](../02_language_guide/05_Control_Flow.md) 一章的 [Break](../02_language_guide/05_Control_Flow.md#break) 和 [带标签的语句](../02_language_guide/05_Control_Flow.md#labeled_statements)。
|
||||
关于使用 `break` 语句的例子,请参阅 [控制流](../02_language_guide/05_Control_Flow.md) 一章的 [Break](../02_language_guide/05_Control_Flow.md#break) 和 [带标签的语句](../02-language-guide/05-Control-Flow.md#labeled-statements)。
|
||||
|
||||
> break 语句语法
|
||||
>
|
||||
@ -452,7 +452,7 @@ case .suppressed:
|
||||
|
||||
在 `for` 语句中,`continue` 语句执行后,增量表达式还是会被计算,这是因为每次循环体执行完毕后,增量表达式都会被计算。
|
||||
|
||||
关于使用 `continue` 语句的例子,请参阅 [控制流](../02_language_guide/05_Control_Flow.md) 一章的 [Continue](../02_language_guide/05_Control_Flow.md#continue) 和 [带标签的语句](../02_language_guide/05_Control_Flow.md#labeled_statements)。
|
||||
关于使用 `continue` 语句的例子,请参阅 [控制流](../02_language_guide/05_Control_Flow.md) 一章的 [Continue](../02_language_guide/05_Control_Flow.md#continue) 和 [带标签的语句](../02-language-guide/05-Control-Flow.md#labeled-statements)。
|
||||
|
||||
> continue 语句语法
|
||||
>
|
||||
@ -467,7 +467,7 @@ case .suppressed:
|
||||
|
||||
`fallthrough` 语句可出现在 `switch` 语句中的任意 `case` 中,但不能出现在最后一个 `case` 中。同时,`fallthrough` 语句也不能把控制权转移到使用了值绑定的 `case`。
|
||||
|
||||
关于在 `switch` 语句中使用 `fallthrough` 语句的例子,请参阅 [控制流](../02_language_guide/05_Control_Flow.md) 一章的 [控制转移语句](../02_language_guide/05_Control_Flow.md#control_transfer_statements)。
|
||||
关于在 `switch` 语句中使用 `fallthrough` 语句的例子,请参阅 [控制流](../02_language_guide/05_Control_Flow.md) 一章的 [控制转移语句](../02_language_guide/05_Control_Flow.md#control-transfer-statements)。
|
||||
|
||||
> fallthrough 语句语法
|
||||
>
|
||||
@ -492,7 +492,7 @@ case .suppressed:
|
||||
> 注意
|
||||
>
|
||||
>
|
||||
> 正如 [可失败构造器](./06_Declarations.md#failable_initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。
|
||||
> 正如 [可失败构造器](./06_Declarations.md#failable-initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。
|
||||
>
|
||||
|
||||
而只写 `return` 时,仅仅是从该函数或方法中返回,而不返回任何值(也就是说,函数或方法的返回类型为 `Void` 或者说 `()`)。
|
||||
@ -517,7 +517,7 @@ case .suppressed:
|
||||
|
||||
表达式的结果必须符合 `ErrorType` 协议。
|
||||
|
||||
关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../02_language_guide/17_Error_Handling.md) 一章的 [用 throwing 函数传递错误](../02_language_guide/17_Error_Handling.md#propagating_errors_using_throwing_functions)。
|
||||
关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../02_language_guide/17_Error_Handling.md) 一章的 [用 throwing 函数传递错误](../02_language_guide/17_Error_Handling.md#propagating-errors-using-throwing-functions)。
|
||||
|
||||
> throw 语句语法
|
||||
>
|
||||
@ -586,7 +586,7 @@ do {
|
||||
|
||||
为了确保错误已经被处理,可以让 `catch` 子句使用匹配所有错误的模式,如通配符模式(`_`)。如果一个 `catch` 子句不指定一种具体模式,`catch` 子句会匹配任何错误,并绑定到名为 `error` 的局部常量。有关在 `catch` 子句中使用模式的更多信息,请参阅 [模式](./08_Patterns.md)。
|
||||
|
||||
关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../02_language_guide/17_Error_Handling.md#handling_errors)。
|
||||
关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../02_language_guide/17_Error_Handling.md#handling-errors)。
|
||||
|
||||
> do 语句语法
|
||||
>
|
||||
@ -601,7 +601,7 @@ do {
|
||||
>
|
||||
|
||||
#### catch-clause {#catch-clause}
|
||||
> *catch 子句* → **catch** [*模式*](./08_Patterns.md#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
|
||||
> *catch 子句* → **catch** [*模式*](./08_Patterns.md#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05-Declarations.md#code-block)
|
||||
>
|
||||
|
||||
## 编译器控制语句 {#compiler-control-statements}
|
||||
@ -612,11 +612,11 @@ do {
|
||||
>
|
||||
|
||||
#### compiler-control-statement {#compiler-control-statement}
|
||||
> *编译器控制语句* → [*条件编译语句*](#grammar_conditional-compilation-block)
|
||||
> *编译器控制语句* → [*条件编译语句*](#grammar-conditional-compilation-block)
|
||||
>
|
||||
> *编译器控制语句* → [*线路控制语句*](#line-control-statement)
|
||||
>
|
||||
> *编译器控制语句* → [*诊断语句*](#grammar_diagnostic-statement)
|
||||
> *编译器控制语句* → [*诊断语句*](#grammar-diagnostic-statement)
|
||||
>
|
||||
|
||||
### 条件编译代码块 {#Conditional-Compilation-Block}
|
||||
@ -697,24 +697,24 @@ statements to compile if both compilation conditions are false
|
||||
>
|
||||
>
|
||||
|
||||
#### grammar_conditional-compilation-block {#grammar-conditional-compilation-block}
|
||||
> *条件编译代码块* → [*if-directive 语句*](#grammar_if-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses)<sub>可选</sub> [*else-directive 语句*](#grammar_else-directive-clause)<sub>可选</sub> [*endif-directive*](#grammar_endif-directive)
|
||||
#### grammar-conditional-compilation-block {#grammar-conditional-compilation-block}
|
||||
> *条件编译代码块* → [*if-directive 语句*](#grammar-if-directive-clause) [*elseif-directive 语句(复数)*](#grammar-elseif-directive-clauses)<sub>可选</sub> [*else-directive 语句*](#grammar-else-directive-clause)<sub>可选</sub> [*endif-directive*](#grammar-endif-directive)
|
||||
>
|
||||
|
||||
#### grammar_if-directive-clause {#grammar-if-directive-clause}
|
||||
> *if-directive 语句* → [*if-directive*](#grammar_if-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
#### grammar-if-directive-clause {#grammar-if-directive-clause}
|
||||
> *if-directive 语句* → [*if-directive*](#grammar-if-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_elseif-directive-clauses {#grammar-elseif-directive-clauses}
|
||||
> *elseif-directive 语句(复数)* → [*elseif-directive 语句*](#grammar_elseif-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses)
|
||||
#### grammar-elseif-directive-clauses {#grammar-elseif-directive-clauses}
|
||||
> *elseif-directive 语句(复数)* → [*elseif-directive 语句*](#grammar-elseif-directive-clause) [*elseif-directive 语句(复数)*](#grammar-elseif-directive-clauses)
|
||||
>
|
||||
|
||||
#### grammar_elseif-directive-clauses {#grammar-elseif-directive-clauses}
|
||||
> *elseif-directive 语句* → [*elseif-directive*](#grammar_elseif-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
#### grammar-elseif-directive-clauses {#grammar-elseif-directive-clauses}
|
||||
> *elseif-directive 语句* → [*elseif-directive*](#grammar-elseif-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_else-directive-clause {#grammar-else-directive-clause}
|
||||
> *else-directive 语句* → [*else-directive*](#grammar_else-directive) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
#### grammar-else-directive-clause {#grammar-else-directive-clause}
|
||||
> *else-directive 语句* → [*else-directive*](#grammar-else-directive) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
|
||||
@ -729,7 +729,7 @@ statements to compile if both compilation conditions are false
|
||||
|
||||
|
||||
#### compilation-condition {#compilation-condition}
|
||||
> *编译条件* → [*平台条件*](#grammar_platform-condition)
|
||||
> *编译条件* → [*平台条件*](#grammar-platform-condition)
|
||||
>
|
||||
> *编译条件* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
@ -745,30 +745,30 @@ statements to compile if both compilation conditions are false
|
||||
>
|
||||
|
||||
|
||||
#### grammar_platform-condition {#grammar-platform-condition}
|
||||
#### grammar-platform-condition {#grammar-platform-condition}
|
||||
|
||||
#### grammar_platform-condition-os {#grammar-platform-condition-os}
|
||||
#### grammar-platform-condition-os {#grammar-platform-condition-os}
|
||||
> *平台条件* → **os ( [*操作系统*](#operating-system) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-arch {#grammar-platform-condition-arch}
|
||||
#### grammar-platform-condition-arch {#grammar-platform-condition-arch}
|
||||
> *平台条件* → **arch ( [*架构*](#architecture) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-swift {#grammar-platform-condition-swift}
|
||||
#### grammar-platform-condition-swift {#grammar-platform-condition-swift}
|
||||
> *平台条件* → **swift ( >= [*swift 版本*](#swift-version) )** | **swift ( < [*swift 版本*](#swift-version) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-compiler {#grammar-platform-condition-compiler}
|
||||
#### grammar-platform-condition-compiler {#grammar-platform-condition-compiler}
|
||||
> *平台条件* → **compiler ( >= [*swift 版本*](#swift-version) )** | **compiler ( < [*swift 版本*](#swift-version) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-canImport {#grammar-platform-condition-canImport}
|
||||
> *平台条件* → **canImport ( [*模块名*](#grammar_module-name) )**
|
||||
#### grammar-platform-condition-canImport {#grammar-platform-condition-canImport}
|
||||
> *平台条件* → **canImport ( [*模块名*](#grammar-module-name) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-targetEnvironment {#grammar-platform-condition-targetEnvironment}
|
||||
> *平台条件* → **targetEnvironment ( [*环境*](#grammar_environment) )**
|
||||
#### grammar-platform-condition-targetEnvironment {#grammar-platform-condition-targetEnvironment}
|
||||
> *平台条件* → **targetEnvironment ( [*环境*](#grammar-environment) )**
|
||||
>
|
||||
|
||||
#### operating-system {#operating-system}
|
||||
@ -780,18 +780,18 @@ statements to compile if both compilation conditions are false
|
||||
>
|
||||
|
||||
#### swift-version {#swift-version}
|
||||
> *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>
|
||||
>
|
||||
|
||||
#### grammar_swift-version-continuation {#grammar-swift-version-continuation}
|
||||
> *swift 版本延续* → **.** [*十进制数字*](./02_Lexical_Structure.md#decimal-digit) [*swift 版本延续*](#grammar_swift-version-continuation) <sub>可选</sub>
|
||||
#### grammar-swift-version-continuation {#grammar-swift-version-continuation}
|
||||
> *swift 版本延续* → **.** [*十进制数字*](./02_Lexical_Structure.md#decimal-digit) [*swift 版本延续*](#grammar-swift-version-continuation) <sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_module-name {#grammar-module-name}
|
||||
#### grammar-module-name {#grammar-module-name}
|
||||
> *模块名* → [*identifier*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
#### grammar_environment {#grammar-environment}
|
||||
#### grammar-environment {#grammar-environment}
|
||||
> *环境* → **模拟器**
|
||||
>
|
||||
|
||||
@ -843,10 +843,10 @@ statements to compile if both compilation conditions are false
|
||||
>
|
||||
>
|
||||
|
||||
#### grammar_compile-time-diagnostic-statement {#grammar-compile-time-diagnostic-statement}
|
||||
> *诊断语句* → **#error** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)**
|
||||
#### grammar-compile-time-diagnostic-statement {#grammar-compile-time-diagnostic-statement}
|
||||
> *诊断语句* → **#error** **(** [*diagnostic-message*](#grammar-diagnostic-message) **)**
|
||||
>
|
||||
> *诊断语句* → **#warning** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)**
|
||||
> *诊断语句* → **#warning** **(** [*diagnostic-message*](#grammar-diagnostic-message) **)**
|
||||
>
|
||||
> *诊断语句* → [*静态字符串字面量*](./02_Lexical_Structure.md#static-string-literal)
|
||||
>
|
||||
@ -903,7 +903,7 @@ if #available(platform name version, ..., *) {
|
||||
#### platform-version {#platform-version}
|
||||
> *平台版本* → [十进制数字](./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)
|
||||
>
|
||||
|
||||
@ -44,7 +44,7 @@
|
||||
>
|
||||
|
||||
## 顶级代码 {#top-level-code}
|
||||
Swift 的源文件中的顶级代码(top-level code)由零个或多个语句、声明和表达式组成。默认情况下,在一个源文件的顶层声明的变量,常量和其他具有命名的声明可以被同模块中的每一个源文件中的代码访问。可以使用一个访问级别修饰符来标记声明来覆盖这种默认行为,请参阅 [访问控制级别](#access_control_levels)。
|
||||
Swift 的源文件中的顶级代码(top-level code)由零个或多个语句、声明和表达式组成。默认情况下,在一个源文件的顶层声明的变量,常量和其他具有命名的声明可以被同模块中的每一个源文件中的代码访问。可以使用一个访问级别修饰符来标记声明来覆盖这种默认行为,请参阅 [访问控制级别](#access-control-levels)。
|
||||
|
||||
> 顶级声明语法
|
||||
>
|
||||
@ -85,7 +85,7 @@ import 模块.子模块
|
||||
```
|
||||
|
||||
|
||||
#### grammer_of_an_import_declaration {#grammer-of-an-import-declaration}
|
||||
#### grammer-of-an-import-declaration {#grammer-of-an-import-declaration}
|
||||
> 导入声明语法
|
||||
>
|
||||
>
|
||||
@ -106,7 +106,7 @@ import 模块.子模块
|
||||
>
|
||||
#### import-path-identifier {#import-path-identifier}
|
||||
>
|
||||
> *导入路径标识符* → [*标识符*](./02_Lexical_Structure.md#identifier) | [*运算符*](./02_Lexical_Structure.md#operator)
|
||||
> *导入路径标识符* → [*标识符*](./02_Lexical_Structure.md#identifier) | [*运算符*](./02-Lexical-Structure.md#operator)
|
||||
>
|
||||
|
||||
## 常量声明 {#constant-declaration}
|
||||
@ -135,14 +135,14 @@ print("The second number is \(secondNumber).")
|
||||
// 打印“The second number is 42.”
|
||||
```
|
||||
|
||||
当常量名称的类型(`:` 类型)可以被推断出时,类型注解在常量声明中是可选的,正如 [类型推断](./03_Types.md#type_inference) 中所描述的。
|
||||
当常量名称的类型(`:` 类型)可以被推断出时,类型注解在常量声明中是可选的,正如 [类型推断](./03_Types.md#type-inference) 中所描述的。
|
||||
|
||||
声明一个常量类型属性要使用 `static` 声明修饰符。类的常量类型属性总是隐式地被标记为 `final` ;你无法用 `class` 或 `final` 声明修饰符实现允许或禁止被子类重写的目的。类型属性在 [类型属性](../02_language_guide/10_Properties.md#type_properties) 中有介绍。
|
||||
声明一个常量类型属性要使用 `static` 声明修饰符。类的常量类型属性总是隐式地被标记为 `final` ;你无法用 `class` 或 `final` 声明修饰符实现允许或禁止被子类重写的目的。类型属性在 [类型属性](../02_language_guide/10_Properties.md#type-properties) 中有介绍。
|
||||
|
||||
如果还想获得更多关于常量的信息或者想在使用中获得帮助,请参阅 [常量和变量](../02_language_guide/01_The_Basics.md#constants_and_variables) 和 [存储属性](../02_language_guide/10_Properties.md#stored_properties)。
|
||||
如果还想获得更多关于常量的信息或者想在使用中获得帮助,请参阅 [常量和变量](../02_language_guide/01_The_Basics.md#constants-and-variables) 和 [存储属性](../02-language-guide/10-Properties.md#stored-properties)。
|
||||
|
||||
|
||||
#### grammer_of_a_constant_declaration {#grammer-of-a-constant-declaration}
|
||||
#### grammer-of-a-constant-declaration {#grammer-of-a-constant-declaration}
|
||||
> 常量声明语法
|
||||
>
|
||||
>
|
||||
@ -173,7 +173,7 @@ print("The second number is \(secondNumber).")
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 也可以在协议声明中声明属性,详情请参阅 [协议属性声明](#protocol_property_declaration)。
|
||||
> 也可以在协议声明中声明属性,详情请参阅 [协议属性声明](#protocol-property-declaration)。
|
||||
>
|
||||
|
||||
可以在子类中重写继承来的变量属性,使用 `override` 声明修饰符标记属性的声明即可,详情请参阅 [重写](../02_language_guide/13_Inheritance.md#overriding)。
|
||||
@ -209,13 +209,13 @@ var 变量名称: 类型 {
|
||||
|
||||
可以在全局范围、函数内部,以及类、结构体、枚举、扩展的声明中使用这种形式的声明。当变量以这种形式在全局范围或者函数内部被声明时,它表示一个计算型变量。当它在类、结构体、枚举、扩展声明的上下文中被声明时,它表示一个*计算型属性(computed property)*。
|
||||
|
||||
getter 用来读取变量值,setter 用来写入变量值。setter 子句是可选的,getter 子句是必须的。不过也可以将这些子句都省略,直接返回请求的值,正如在 [只读计算型属性](../02_language_guide/10_Properties.md#computed_properties) 中描述的那样。但是如果提供了一个 setter 子句,就必须也提供一个 getter 子句。
|
||||
getter 用来读取变量值,setter 用来写入变量值。setter 子句是可选的,getter 子句是必须的。不过也可以将这些子句都省略,直接返回请求的值,正如在 [只读计算型属性](../02_language_guide/10_Properties.md#computed-properties) 中描述的那样。但是如果提供了一个 setter 子句,就必须也提供一个 getter 子句。
|
||||
|
||||
setter 的圆括号以及 setter 名称是可选的。如果提供了 setter 名称,它就会作为 setter 的参数名称使用。如果不提供 setter 名称,setter 的参数的默认名称为 `newValue`,正如在 [便捷 setter 声明](../02_language_guide/10_Properties.md#shorthand_setter_declaration) 中描述的那样。
|
||||
setter 的圆括号以及 setter 名称是可选的。如果提供了 setter 名称,它就会作为 setter 的参数名称使用。如果不提供 setter 名称,setter 的参数的默认名称为 `newValue`,正如在 [便捷 setter 声明](../02_language_guide/10_Properties.md#shorthand-setter-declaration) 中描述的那样。
|
||||
|
||||
与存储型变量和存储型属性不同,计算型变量和计算型属性的值不存储在内存中。
|
||||
|
||||
要获得更多关于计算型属性的信息和例子,请参阅 [计算型属性](../02_language_guide/10_Properties.md#computed_properties)。
|
||||
要获得更多关于计算型属性的信息和例子,请参阅 [计算型属性](../02_language_guide/10_Properties.md#computed-properties)。
|
||||
|
||||
### 存储型变量和属性的观察器 {#stored-variable-observers-and-property-observers}
|
||||
可以在声明存储型变量或属性时提供 `willSet` 和 `didSet` 观察器。一个包含观察器的存储型变量或属性以如下形式声明:
|
||||
@ -234,7 +234,7 @@ var 变量名称: 类型 = 表达式 {
|
||||
可以在全局范围、函数内部,或者类、结构体的声明中使用这种形式的声明。当变量以这种形式在全局范围或者函数内部被声明时,观察器表示一个存储型变量观察器。当它在类和结构体的声明中被声明时,观察器表示一个属性观察器。
|
||||
|
||||
可以为任何存储型属性添加观察器。也可以通过重写父类属性的方式为任何继承的属性(无论是存储型还是计算型的)添加观察器
|
||||
,正如 [重写属性观察器](../02_language_guide/13_Inheritance.md#overriding_property_observers) 中所描述的。
|
||||
,正如 [重写属性观察器](../02_language_guide/13_Inheritance.md#overriding-property-observers) 中所描述的。
|
||||
|
||||
用于初始化的表达式在类或者结构的声明中是可选的,但是在其他声明中则是必须的。如果可以从初始化表达式中推断出类型信息,那么可以不提供类型注解。
|
||||
|
||||
@ -246,28 +246,28 @@ var 变量名称: 类型 = 表达式 {
|
||||
|
||||
提供了 `willSet` 时,`didSet` 是可选的。同样的,提供了 `didSet` 时,`willSet` 则是可选的。
|
||||
|
||||
要获得更多信息以及查看如何使用属性观察器的例子,请参阅 [属性观察器](../02_language_guide/10_Properties.md#property_observers)。
|
||||
要获得更多信息以及查看如何使用属性观察器的例子,请参阅 [属性观察器](../02_language_guide/10_Properties.md#property-observers)。
|
||||
|
||||
### 类型变量属性 {#type-variable-properties}
|
||||
要声明一个类型变量属性,用 `static` 声明修饰符标记该声明。类可以改用 `class` 声明修饰符标记类的类型计算型属性从而允许子类重写超类的实现。类型属性在 [类型属性](../02_language_guide/10_Properties.md#type_properties) 章节有详细讨论。
|
||||
要声明一个类型变量属性,用 `static` 声明修饰符标记该声明。类可以改用 `class` 声明修饰符标记类的类型计算型属性从而允许子类重写超类的实现。类型属性在 [类型属性](../02_language_guide/10_Properties.md#type-properties) 章节有详细讨论。
|
||||
|
||||
|
||||
#### grammer_of_a_variable_declaration {#grammer-of-a-variable-declaration}
|
||||
#### grammer-of-a-variable-declaration {#grammer-of-a-variable-declaration}
|
||||
> 变量声明语法
|
||||
>
|
||||
|
||||
#### variable-declaration {#variable-declaration}
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*模式构造器列表*](#pattern-initializer-list)
|
||||
>
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型注解*](03_Types.md#type-annotation) [*代码块*](#code-block)
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型注解*](03-Types.md#type-annotation) [*代码块*](#code-block)
|
||||
>
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型注解*](03_Types.md#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.md#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) [*类型注解*](03_Types.md#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)
|
||||
>
|
||||
|
||||
|
||||
@ -386,10 +386,10 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
|
||||
|
||||
假如没有类型别名,sum 函数将必须引用关联类型通过 T.Iterator.Element 的形式来替代 T.Element。
|
||||
|
||||
另请参阅 [协议关联类型声明](#protocol_associated_type_declaration)。
|
||||
另请参阅 [协议关联类型声明](#protocol-associated-type-declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_type_alias_declaration {#grammer-of-a-type-alias-declaration}
|
||||
#### grammer-of-a-type-alias-declaration {#grammer-of-a-type-alias-declaration}
|
||||
> 类型别名声明语法
|
||||
>
|
||||
>
|
||||
@ -426,7 +426,7 @@ func 函数名称(参数列表) {
|
||||
}
|
||||
```
|
||||
|
||||
每个参数的类型都要标明,因为它们不能被推断出来。如果您在某个参数类型前面加上了 `inout`,那么这个参数就可以在这个函数作用域当中被修改。更多关于 `inout` 参数的讨论,请参阅 [输入输出参数](#in-out_parameters)。
|
||||
每个参数的类型都要标明,因为它们不能被推断出来。如果您在某个参数类型前面加上了 `inout`,那么这个参数就可以在这个函数作用域当中被修改。更多关于 `inout` 参数的讨论,请参阅 [输入输出参数](#in-out-parameters)。
|
||||
|
||||
函数声明中语句只包含一个表达式,可以理解为返回该表达式的值。
|
||||
|
||||
@ -436,7 +436,7 @@ func 函数名称(参数列表) {
|
||||
|
||||
大多数时候,嵌套函数都是可逃逸的函数。仅当一个嵌套函数捕获了某个确保了永不逃逸的值——例如一个输入输出参数——或者传入一个非逃逸函数参数的时候,这个嵌套函数才是非逃逸的。
|
||||
|
||||
更多关于嵌套函数的讨论,请参阅 [嵌套函数](../02_language_guide/06_Functions.md#Nested_Functions)。
|
||||
更多关于嵌套函数的讨论,请参阅 [嵌套函数](../02_language_guide/06_Functions.md#Nested-Functions)。
|
||||
|
||||
### 参数名 {#parameter-names}
|
||||
函数的参数列表由一个或多个函数参数组成,参数间以逗号分隔。函数调用时的参数顺序必须和函数声明时的参数顺序一致。最简单的参数列表有着如下的形式:
|
||||
@ -505,7 +505,7 @@ func multithreadedFunction(queue: DispatchQueue, x: inout Int) {
|
||||
}
|
||||
```
|
||||
|
||||
关于输入输出参数的详细讨论,请参阅 [输入输出参数](../02_language_guide/06_Functions.md#in_out_parameters)。
|
||||
关于输入输出参数的详细讨论,请参阅 [输入输出参数](../02_language_guide/06_Functions.md#in-out-parameters)。
|
||||
|
||||
### 特殊参数 {#special-kinds-of-parameters}
|
||||
参数可以被忽略,数量可以不固定,还可以为其提供默认值,使用形式如下:
|
||||
@ -518,7 +518,7 @@ _ : 参数类型
|
||||
|
||||
以下划线(`_`)命名的参数会被显式忽略,无法在函数内使用。
|
||||
|
||||
一个参数的基本类型名称如果紧跟着三个点(`...`),会被视为可变参数。一个函数至多可以拥有一个可变参数,且必须是最后一个参数。可变参数会作为包含该参数类型元素的数组处理。举例来讲,可变参数 `Int...` 会作为 `[Int]` 来处理。关于使用可变参数的例子,请参阅 [可变参数](../02_language_guide/06_Functions.md#variadic_parameters)。
|
||||
一个参数的基本类型名称如果紧跟着三个点(`...`),会被视为可变参数。一个函数至多可以拥有一个可变参数,且必须是最后一个参数。可变参数会作为包含该参数类型元素的数组处理。举例来讲,可变参数 `Int...` 会作为 `[Int]` 来处理。关于使用可变参数的例子,请参阅 [可变参数](../02_language_guide/06_Functions.md#variadic-parameters)。
|
||||
|
||||
如果在参数类型后面有一个以等号(`=`)连接的表达式,该参数会拥有默认值,即给定表达式的值。当函数被调用时,给定的表达式会被求值。如果参数在函数调用时被省略了,就会使用其默认值。
|
||||
|
||||
@ -584,17 +584,17 @@ func someFunction(callback: () throws -> Void) rethrows {
|
||||
### 永不返回的函数 {#functions-that-never-return}
|
||||
Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它的调用者。`Never` 返回类型的函数或方法可以称为不归,不归函数、方法要么引发不可恢复的错误,要么永远不停地运作,这会使调用后本应执行得代码就不再执行了。但即使是不归函数、方法,抛错函数和重抛出函数也可以将程序控制转移到合适的 `catch` 代码块。
|
||||
|
||||
不归函数、方法可以在 guard 语句的 else 字句中调用,具体讨论在 [*Guard 语句*](./05_Statements.md#guard_statements)。
|
||||
不归函数、方法可以在 guard 语句的 else 字句中调用,具体讨论在 [*Guard 语句*](./05_Statements.md#guard-statements)。
|
||||
|
||||
你可以重写一个不归方法,但是新的方法必须保持原有的返回类型和没有返回的行为。
|
||||
|
||||
|
||||
#### grammer_of_a_function_declaration {#grammer-of-a-function-declaration}
|
||||
#### grammer-of-a-function-declaration {#grammer-of-a-function-declaration}
|
||||
> 函数声明语法
|
||||
>
|
||||
|
||||
#### function-declaration {#function-declaration}
|
||||
> *函数声明* → [*函数头*](#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>
|
||||
> *函数声明* → [*函数头*](#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>
|
||||
>
|
||||
|
||||
|
||||
@ -604,7 +604,7 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
>
|
||||
#### function-name {#function-name}
|
||||
>
|
||||
> *函数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | [*运算符*](./02_Lexical_Structure.md#operator)
|
||||
> *函数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | [*运算符*](./02-Lexical-Structure.md#operator)
|
||||
>
|
||||
>
|
||||
>
|
||||
@ -618,7 +618,7 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
>
|
||||
#### function-result {#function-result}
|
||||
>
|
||||
> *函数结果* → **->** [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*类型*](./03_Types.md#type)
|
||||
> *函数结果* → **->** [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*类型*](./03-Types.md#type)
|
||||
>
|
||||
>
|
||||
#### function-body {#function-body}
|
||||
@ -639,21 +639,21 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
>
|
||||
#### parameter {#parameter}
|
||||
>
|
||||
> *参数* → [*外部参数名*](#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.md#type-annotation) [*默认参数子句*](#default-argument-clause)<sub>可选</sub>
|
||||
>
|
||||
> *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型注解*](03_Types.md#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.md#type-annotation) **...**
|
||||
> *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型注解*](03-Types.md#type-annotation) **...**
|
||||
>
|
||||
>
|
||||
#### external-parameter-name {#external-parameter-name}
|
||||
>
|
||||
> *外部参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | **_**
|
||||
> *外部参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | **-**
|
||||
>
|
||||
>
|
||||
#### local-parameter-name {#local-parameter-name}
|
||||
>
|
||||
> *内部参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | **_**
|
||||
> *内部参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | **-**
|
||||
>
|
||||
>
|
||||
#### default-argument-clause {#default-argument-clause}
|
||||
@ -670,9 +670,9 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
|
||||
不同于类或者结构体,枚举类型并不隐式提供默认构造器,所有构造器必须显式声明。一个构造器可以委托给枚举中的其他构造器,但是构造过程仅当构造器将一个枚举用例赋值给 `self` 后才算完成。
|
||||
|
||||
和结构体类似但是和类不同,枚举是值类型。枚举实例在被赋值到变量或常量时,或者传递给函数作为参数时会被复制。更多关于值类型的信息,请参阅 [结构体和枚举是值类型](../02_language_guide/09_Structures_And_Classes.md#structures_and_enumerations_are_value_types)。
|
||||
和结构体类似但是和类不同,枚举是值类型。枚举实例在被赋值到变量或常量时,或者传递给函数作为参数时会被复制。更多关于值类型的信息,请参阅 [结构体和枚举是值类型](../02_language_guide/09_Structures_And_Classes.md#structures-and-enumerations-are-value-types)。
|
||||
|
||||
可以扩展枚举类型,正如在 [扩展声明](#extension_declaration) 中讨论的一样。
|
||||
可以扩展枚举类型,正如在 [扩展声明](#extension-declaration) 中讨论的一样。
|
||||
|
||||
### 任意类型的枚举用例 {#enumerations-with-cases-of-any-type}
|
||||
如下的形式声明了一个包含任意类型枚举用例的枚举变量:
|
||||
@ -703,7 +703,7 @@ let f = Number.integer
|
||||
let evenInts: [Number] = [0, 2, 4, 6].map(f)
|
||||
```
|
||||
|
||||
要获得更多关于具有关联值的枚举用例的信息和例子,请参阅 [关联值](../02_language_guide/08_Enumerations.md#associated_values)。
|
||||
要获得更多关于具有关联值的枚举用例的信息和例子,请参阅 [关联值](../02_language_guide/08_Enumerations.md#associated-values)。
|
||||
|
||||
#### 递归枚举 {#enumerations-with-indirection}
|
||||
枚举类型可以具有递归结构,就是说,枚举用例的关联值类型可以是枚举类型自身。然而,枚举类型的实例具有值语义,这意味着它们在内存中有固定布局。为了支持递归,编译器必须插入一个间接层。
|
||||
@ -753,15 +753,15 @@ enum GamePlayMode: String {
|
||||
|
||||
在上面这个例子中,`GamePlayMode.cooperative` 的原始值是 `"cooperative"`,`GamePlayMode.individual` 的原始值是 `"individual"`,`GamePlayMode.competitive` 的原始值是 `"competitive"`。
|
||||
|
||||
枚举用例具有原始值的枚举类型隐式地符合定义在 Swift 标准库中的 `RawRepresentable` 协议。所以,它们拥有一个 `rawValue` 属性和一个可失败构造器 `init?(rawValue: RawValue)`。可以使用 `rawValue` 属性去获取枚举用例的原始值,例如 `ExampleEnum.b.rawValue`。还可以根据原始值来创建一个相对应的枚举用例,只需调用枚举的可失败构造器,例如 `ExampleEnum(rawValue: 5)`,这个可失败构造器返回一个可选类型的用例。要获得更多关于具有原始值的枚举用例的信息和例子,请参阅 [原始值](../02_language_guide/08_Enumerations.md#raw_values)。
|
||||
枚举用例具有原始值的枚举类型隐式地符合定义在 Swift 标准库中的 `RawRepresentable` 协议。所以,它们拥有一个 `rawValue` 属性和一个可失败构造器 `init?(rawValue: RawValue)`。可以使用 `rawValue` 属性去获取枚举用例的原始值,例如 `ExampleEnum.b.rawValue`。还可以根据原始值来创建一个相对应的枚举用例,只需调用枚举的可失败构造器,例如 `ExampleEnum(rawValue: 5)`,这个可失败构造器返回一个可选类型的用例。要获得更多关于具有原始值的枚举用例的信息和例子,请参阅 [原始值](../02_language_guide/08_Enumerations.md#raw-values)。
|
||||
|
||||
### 访问枚举用例 {#accessing-enumeration-cases}
|
||||
使用点语法(`.`)来引用枚举类型的枚举用例,例如 `EnumerationType.enumerationCase`。当枚举类型可以由上下文推断而出时,可以省略它(但是 `.` 仍然需要),正如 [枚举语法](../02_language_guide/08_Enumerations.md#enumeration_syntax) 和 [显式成员表达式](./04_Expressions.md#explicit_member_expression) 所述。
|
||||
使用点语法(`.`)来引用枚举类型的枚举用例,例如 `EnumerationType.enumerationCase`。当枚举类型可以由上下文推断而出时,可以省略它(但是 `.` 仍然需要),正如 [枚举语法](../02_language_guide/08_Enumerations.md#enumeration-syntax) 和 [显式成员表达式](./04-Expressions.md#explicit-member-expression) 所述。
|
||||
|
||||
可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../02_language_guide/08_Enumerations.md#matching_enumeration_values_with_a_switch_statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](./08_Patterns.md#enumeration_case_pattern) 所述。
|
||||
可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../02_language_guide/08_Enumerations.md#matching-enumeration-values-with-a-switch-statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](./08-Patterns.md#enumeration-case-pattern) 所述。
|
||||
|
||||
|
||||
#### grammer_of_an_enumeration_declaration {#grammer-of-an-enumeration-declaration}
|
||||
#### grammer-of-an-enumeration-declaration {#grammer-of-an-enumeration-declaration}
|
||||
> 枚举声明语法
|
||||
>
|
||||
>
|
||||
@ -773,7 +773,7 @@ enum GamePlayMode: String {
|
||||
> *枚举声明* → [*特性列表*](./07_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.md#generic-parameter-clause)<sub>可选</sub> [类型继承子句](./03_Types.md#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> **}**
|
||||
>
|
||||
>
|
||||
#### union-style-enum-members {#union-style-enum-members}
|
||||
@ -783,7 +783,7 @@ enum GamePlayMode: String {
|
||||
>
|
||||
#### union-style-enum-member {#union-style-enum-member}
|
||||
>
|
||||
> *联合风格枚举成员* → [*声明*](#declaration) | [*联合风格枚举用例子句*](#union-style-enum-case-clause) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||
> *联合风格枚举成员* → [*声明*](#declaration) | [*联合风格枚举用例子句*](#union-style-enum-case-clause) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
>
|
||||
#### union-style-enum-case-clause {#union-style-enum-case-clause}
|
||||
@ -798,7 +798,7 @@ enum GamePlayMode: String {
|
||||
>
|
||||
#### union-style-enum-case {#union-style-enum-case}
|
||||
>
|
||||
> *联合风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*元组类型*](03_Types.md#tuple-type)<sub>可选</sub>
|
||||
> *联合风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*元组类型*](03-Types.md#tuple-type)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### enum-name {#enum-name}
|
||||
@ -814,7 +814,7 @@ enum GamePlayMode: String {
|
||||
> #### raw-value-style-enum {#raw-value-style-enum}
|
||||
>
|
||||
>
|
||||
> *原始值风格枚举* → **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](./03_Types.md#type-inheritance-clause) [*泛型 where 子句*](./09_Generic_Parameters_and_Arguments.md#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 子句*](./09-Generic-Parameters-and-Arguments.md#generic-where-clause) **{** [*多个原始值风格枚举成员*](#raw-value-style-enum-members) **}**
|
||||
>
|
||||
>
|
||||
#### raw-value-style-enum-members {#raw-value-style-enum-members}
|
||||
@ -824,7 +824,7 @@ enum GamePlayMode: String {
|
||||
>
|
||||
#### raw-value-style-enum-member {#raw-value-style-enum-member}
|
||||
>
|
||||
> *原始值风格枚举成员* → [*声明*](#declaration) | [*原始值风格枚举用例子句*](#raw-value-style-enum-case-clause) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||
> *原始值风格枚举成员* → [*声明*](#declaration) | [*原始值风格枚举用例子句*](#raw-value-style-enum-case-clause) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
>
|
||||
#### raw-value-style-enum-case-clause {#raw-value-style-enum-case-clause}
|
||||
@ -849,7 +849,7 @@ enum GamePlayMode: String {
|
||||
>
|
||||
#### raw-value-literal {#raw-value-literal}
|
||||
>
|
||||
> *原始值字面量* → [数字型字面量](./02_Lexical_Structure.md#numeric-literal) | [字符串型字面量](./02_Lexical_Structure.md#static-string-literal) | [布尔型字面量](./02_Lexical_Structure.md#boolean-literal)
|
||||
> *原始值字面量* → [数字型字面量](./02_Lexical_Structure.md#numeric-literal) | [字符串型字面量](./02-Lexical-Structure.md#static-string-literal) | [布尔型字面量](./02-Lexical-Structure.md#boolean-literal)
|
||||
>
|
||||
|
||||
## 结构体声明 {#structure-declaration}
|
||||
@ -869,28 +869,28 @@ struct 结构体名称: 采纳的协议 {
|
||||
|
||||
* 调用结构体内声明的构造器,正如 [构造器](../02_language_guide/14_Initialization.md#initializers) 所述。
|
||||
|
||||
* 如果没有声明构造器,调用结构体的成员逐一构造器,正如 [结构体类型的成员逐一构造器](../02_language_guide/14_Initialization.md#memberwise_initializers_for_structure_types) 所述。
|
||||
* 如果没有声明构造器,调用结构体的成员逐一构造器,正如 [结构体类型的成员逐一构造器](../02_language_guide/14_Initialization.md#memberwise-initializers-for-structure-types) 所述。
|
||||
|
||||
* 如果没有声明构造器,而且结构体的所有属性都有初始值,调用结构体的默认构造器,正如 [默认构造器](../02_language_guide/14_Initialization.md#default_initializers) 所述。
|
||||
* 如果没有声明构造器,而且结构体的所有属性都有初始值,调用结构体的默认构造器,正如 [默认构造器](../02_language_guide/14_Initialization.md#default-initializers) 所述。
|
||||
|
||||
结构体的构造过程请参阅 [构造过程](../02_language_guide/14_Initialization.md)。
|
||||
|
||||
结构体实例的属性可以用点语法(`.`)来访问,正如 [访问属性](../02_language_guide/09_Structures_And_Classes.md#accessing_properties) 所述。
|
||||
结构体实例的属性可以用点语法(`.`)来访问,正如 [访问属性](../02_language_guide/09_Structures_And_Classes.md#accessing-properties) 所述。
|
||||
|
||||
结构体是值类型。结构体的实例在被赋予变量或常量,或传递给函数作为参数时会被复制。关于值类型的更多信息,请参阅
|
||||
[结构体和枚举是值类型](../02_language_guide/09_Structures_And_Classes.md#structures_and_enumerations_are_value_types)。
|
||||
[结构体和枚举是值类型](../02_language_guide/09_Structures_And_Classes.md#structures-and-enumerations-are-value-types)。
|
||||
|
||||
可以使用扩展声明来扩展结构体类型的行为,请参阅 [扩展声明](#extension_declaration)。
|
||||
可以使用扩展声明来扩展结构体类型的行为,请参阅 [扩展声明](#extension-declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_structure_declaration {#grammer-of-a-structure-declaration}
|
||||
#### grammer-of-a-structure-declaration {#grammer-of-a-structure-declaration}
|
||||
> 结构体声明语法
|
||||
>
|
||||
>
|
||||
>
|
||||
#### struct-declaration {#struct-declaration}
|
||||
>
|
||||
> *结构体声明* → [*特性列表*](./07_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 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub> [*结构体主体*](#struct-body)
|
||||
> *结构体声明* → [*特性列表*](./07_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 子句*](./09-Generic-Parameters-and-Arguments.md#generic-where-clause)<sub>可选</sub> [*结构体主体*](#struct-body)
|
||||
>
|
||||
>
|
||||
#### struct-name {#struct-name}
|
||||
@ -911,7 +911,7 @@ struct 结构体名称: 采纳的协议 {
|
||||
>
|
||||
#### struct-member {#struct-member}
|
||||
>
|
||||
> *结构体成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||
> *结构体成员* → [*声明*](#declaration) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
|
||||
## 类声明 {#class-declaration}
|
||||
@ -927,7 +927,7 @@ class 类名: 超类, 采纳的协议 {
|
||||
|
||||
一个类只能继承自一个超类,但是可以采纳任意数量的协议。超类紧跟在类名和冒号后面,其后跟着采纳的协议。泛型类可以继承自其它泛型类和非泛型类,但是非泛型类只能继承自其它非泛型类。当在冒号后面写泛型超类的名称时,必须写上泛型类的全名,包括它的泛型形参子句。
|
||||
|
||||
正如 [构造器声明](#initializer_declaration) 所讨论的,类可以有指定构造器和便利构造器。类的指定构造器必须初始化类中声明的所有属性,并且必须在调用超类构造器之前。
|
||||
正如 [构造器声明](#initializer-declaration) 所讨论的,类可以有指定构造器和便利构造器。类的指定构造器必须初始化类中声明的所有属性,并且必须在调用超类构造器之前。
|
||||
|
||||
类可以重写属性、方法、下标以及构造器。重写的属性、方法、下标和指定构造器必须以 `override` 声明修饰符标记。
|
||||
|
||||
@ -939,25 +939,25 @@ class 类名: 超类, 采纳的协议 {
|
||||
|
||||
* 调用类中声明的构造器,请参阅 [构造器](../02_language_guide/14_Initialization.md#initializers)。
|
||||
|
||||
* 如果没有声明构造器,而且类的所有属性都被赋予了初始值,调用类的默认构造器,请参阅 [默认构造器](../02_language_guide/14_Initialization.md#default_initializers)。
|
||||
* 如果没有声明构造器,而且类的所有属性都被赋予了初始值,调用类的默认构造器,请参阅 [默认构造器](../02_language_guide/14_Initialization.md#default-initializers)。
|
||||
|
||||
类实例属性可以用点语法(`.`)来访问,请参阅 [访问属性](../02_language_guide/09_Structures_And_Classes.md#accessing_properties)。
|
||||
类实例属性可以用点语法(`.`)来访问,请参阅 [访问属性](../02_language_guide/09_Structures_And_Classes.md#accessing-properties)。
|
||||
|
||||
类是引用类型。当被赋予常量或变量,或者传递给函数作为参数时,类的实例会被引用,而不是被复制。关于引用类型的更多信息,请参阅 [结构体和枚举是值类型](../02_language_guide/09_Structures_And_Classes.md#structures_and_enumerations_are_value_types)。
|
||||
类是引用类型。当被赋予常量或变量,或者传递给函数作为参数时,类的实例会被引用,而不是被复制。关于引用类型的更多信息,请参阅 [结构体和枚举是值类型](../02_language_guide/09_Structures_And_Classes.md#structures-and-enumerations-are-value-types)。
|
||||
|
||||
可以使用扩展声明来扩展类的行为,请参阅 [扩展声明](#extension_declaration)。
|
||||
可以使用扩展声明来扩展类的行为,请参阅 [扩展声明](#extension-declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_class_declaration {#grammer-of-a-class-declaration}
|
||||
#### grammer-of-a-class-declaration {#grammer-of-a-class-declaration}
|
||||
> 类声明语法
|
||||
>
|
||||
>
|
||||
>
|
||||
#### class-declaration {#class-declaration}
|
||||
>
|
||||
> *类声明* → [*特性列表*](./07_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 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub> [*类主体*](#class-body)
|
||||
> *类声明* → [*特性列表*](./07_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 子句*](./09-Generic-Parameters-and-Arguments.md#generic-where-clause)<sub>可选</sub> [*类主体*](#class-body)
|
||||
>
|
||||
> *类声明* → [*特性列表*](./07_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 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub> [*类主体*](#class-body)
|
||||
> *类声明* → [*特性列表*](./07_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 子句*](./09-Generic-Parameters-and-Arguments.md#generic-where-clause)<sub>可选</sub> [*类主体*](#class-body)
|
||||
>
|
||||
>
|
||||
#### class-name {#class-name}
|
||||
@ -975,7 +975,7 @@ class 类名: 超类, 采纳的协议 {
|
||||
>
|
||||
#### class-member {#class-member}
|
||||
>
|
||||
> *类成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||
> *类成员* → [*声明*](#declaration) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
|
||||
## 协议声明 {#protocol-declaration}
|
||||
@ -989,16 +989,16 @@ protocol 协议名称: 继承的协议 {
|
||||
|
||||
协议的主体包含零个或多个协议成员声明,这些成员描述了任何采纳该协议的类型必须满足的一致性要求。一个协议可以声明采纳者必须实现的某些属性、方法、构造器以及下标。协议也可以声明各种各样的类型别名,叫做关联类型,它可以指定协议的不同声明之间的关系。协议声明不能包括类、结构体、枚举或者其它协议的声明。协议成员声明会在后面进行讨论。
|
||||
|
||||
协议类型可以继承自任意数量的其它协议。当一个协议类型继承自其它协议的时候,来自其它协议的所有要求会聚合在一起,而且采纳当前协议的类型必须符合所有的这些要求。关于如何使用协议继承的例子,请参阅 [协议继承](../02_language_guide/21_Protocols.md#protocol_inheritance)。
|
||||
协议类型可以继承自任意数量的其它协议。当一个协议类型继承自其它协议的时候,来自其它协议的所有要求会聚合在一起,而且采纳当前协议的类型必须符合所有的这些要求。关于如何使用协议继承的例子,请参阅 [协议继承](../02_language_guide/21_Protocols.md#protocol-inheritance)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 也可以使用协议合成类型来聚合多个协议的一致性要求,请参阅 [协议合成类型](./03_Types.md#protocol_composition_type) 和 [协议合成](../02_language_guide/21_Protocols.md#protocol_composition)。
|
||||
> 也可以使用协议合成类型来聚合多个协议的一致性要求,请参阅 [协议合成类型](./03_Types.md#protocol-composition-type) 和 [协议合成](../02-language-guide/21-Protocols.md#protocol-composition)。
|
||||
>
|
||||
|
||||
可以通过类型的扩展声明来采纳协议,从而为之前声明的类型添加协议一致性。在扩展中,必须实现所有采纳协议的要求。如果该类型已经实现了所有的要求,可以让这个扩展声明的主体留空。
|
||||
|
||||
默认地,符合某个协议的类型必须实现所有在协议中声明的属性、方法和下标。即便如此,可以用 `optional` 声明修饰符标注协议成员声明,以指定它们的实现是可选的。`optional` 修饰符仅仅可以用于使用 `objc` 特性标记过的协议。因此,仅仅类类型可以采用并符合包含可选成员要求的协议。更多关于如何使用 `optional` 声明修饰符的信息,以及如何访问可选协议成员的指导——例如不能确定采纳协议的类型是否实现了它们时——请参阅 [可选协议要求](../02_language_guide/21_Protocols.md#optional_protocol_requirements)。
|
||||
默认地,符合某个协议的类型必须实现所有在协议中声明的属性、方法和下标。即便如此,可以用 `optional` 声明修饰符标注协议成员声明,以指定它们的实现是可选的。`optional` 修饰符仅仅可以用于使用 `objc` 特性标记过的协议。因此,仅仅类类型可以采用并符合包含可选成员要求的协议。更多关于如何使用 `optional` 声明修饰符的信息,以及如何访问可选协议成员的指导——例如不能确定采纳协议的类型是否实现了它们时——请参阅 [可选协议要求](../02_language_guide/21_Protocols.md#optional-protocol-requirements)。
|
||||
|
||||
为了限制协议只能被类类型采纳,需要使用 `AnyObject` 关键字来标记协议,将 `AnyObject` 关键在写在冒号后面的继承的协议列表的首位。例如,下面的协议只能被类类型采纳:
|
||||
|
||||
@ -1015,19 +1015,19 @@ protocol SomeProtocol: AnyObject {
|
||||
> 如果协议已经用 `objc` 特性标记了,`AnyObject` 要求就隐式地应用于该协议,无需显式使用 `AnyObject` 关键字。
|
||||
>
|
||||
|
||||
协议类型是命名的类型,因此它们可以像其他命名类型一样使用,正如 [协议作为类型](../02_language_guide/21_Protocols.md#protocols_as_types) 所讨论的。然而,不能构造一个协议的实例,因为协议实际上不提供它们指定的要求的实现。
|
||||
协议类型是命名的类型,因此它们可以像其他命名类型一样使用,正如 [协议作为类型](../02_language_guide/21_Protocols.md#protocols-as-types) 所讨论的。然而,不能构造一个协议的实例,因为协议实际上不提供它们指定的要求的实现。
|
||||
|
||||
可以使用协议来声明作为代理的类或者结构体应该实现的方法,正如 [委托(代理)模式](../02_language_guide/21_Protocols.md#delegation) 中所述。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_declaration {#grammer-of-a-protocol-declaration}
|
||||
#### grammer-of-a-protocol-declaration {#grammer-of-a-protocol-declaration}
|
||||
> 协议声明语法
|
||||
>
|
||||
>
|
||||
>
|
||||
#### protocol-declaration {#protocol-declaration}
|
||||
>
|
||||
> *协议声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03_Types.md#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub> [*协议主体*](#protocol-body)
|
||||
> *协议声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03-Types.md#type-inheritance-clause)<sub>可选</sub> [*泛型 where 子句*](./09-Generic-Parameters-and-Arguments.md#generic-where-clause)<sub>可选</sub> [*协议主体*](#protocol-body)
|
||||
>
|
||||
>
|
||||
#### protocol-name {#protocol-name}
|
||||
@ -1045,7 +1045,7 @@ protocol SomeProtocol: AnyObject {
|
||||
>
|
||||
#### protocol-member {#protocol-member}
|
||||
>
|
||||
> *协议成员* → [*协议成员声明*](#protocol-member-declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||
> *协议成员* → [*协议成员声明*](#protocol-member-declaration) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
>
|
||||
>
|
||||
@ -1076,37 +1076,37 @@ var 属性名: 类型 { get set }
|
||||
|
||||
同其它协议成员声明一样,这些属性声明仅仅针对符合该协议的类型声明了 getter 和 setter 要求,你不能在协议中直接实现 getter 和 setter。
|
||||
|
||||
符合类型可以通过多种方式满足 getter 和 setter 要求。如果属性声明包含 `get` 和 `set` 关键字,符合类型就可以用存储型变量属性或可读可写的计算型属性来满足此要求,但是属性不能以常量属性或只读计算型属性实现。如果属性声明仅仅包含 `get` 关键字的话,它可以作为任意类型的属性被实现。关于如何实现协议中的属性要求的例子,请参阅 [属性要求](../02_language_guide/21_Protocols.md#property_requirements) 。
|
||||
符合类型可以通过多种方式满足 getter 和 setter 要求。如果属性声明包含 `get` 和 `set` 关键字,符合类型就可以用存储型变量属性或可读可写的计算型属性来满足此要求,但是属性不能以常量属性或只读计算型属性实现。如果属性声明仅仅包含 `get` 关键字的话,它可以作为任意类型的属性被实现。关于如何实现协议中的属性要求的例子,请参阅 [属性要求](../02_language_guide/21_Protocols.md#property-requirements) 。
|
||||
|
||||
协议声明中声明一个类型属性,属性声明语句必须用 `static` 声明修饰符。当结构体和枚举遵循该协议时,使用 `static` 关键字修饰,而类遵循该协议时,使用 `static` 或 `class` 关键字皆可。当结构体,枚举或类添加扩展遵循协议时,和之前扩展用到的关键字保持一致。扩展为类属性提供默认实现时,必须使用 `static` 关键字修饰。
|
||||
|
||||
另请参阅 [变量声明](#variable_declaration)。
|
||||
另请参阅 [变量声明](#variable-declaration)。
|
||||
|
||||
|
||||
#### grammer_of_an_import_declaration {#grammer-of-an-import-declaration}
|
||||
#### grammer-of-an-import-declaration {#grammer-of-an-import-declaration}
|
||||
> 协议属性声明语法
|
||||
>
|
||||
>
|
||||
#### protocol-property-declaration {#protocol-property-declaration}
|
||||
>
|
||||
> *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型注解*](03_Types.md#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||
> *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型注解*](03-Types.md#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||
>
|
||||
|
||||
### 协议方法声明 {#protocol-method-declaration}
|
||||
协议可以通过在协议声明主体中引入一个协议方法声明,来声明符合的类型必须实现的方法。协议方法声明和函数方法声明有着相同的形式,但有两项例外:它们不包括函数体,也不能包含默认参数。关于如何实现协议中的方法要求的例子,请参阅 [方法要求](../02_language_guide/21_Protocols.md#method_requirements)。
|
||||
协议可以通过在协议声明主体中引入一个协议方法声明,来声明符合的类型必须实现的方法。协议方法声明和函数方法声明有着相同的形式,但有两项例外:它们不包括函数体,也不能包含默认参数。关于如何实现协议中的方法要求的例子,请参阅 [方法要求](../02_language_guide/21_Protocols.md#method-requirements)。
|
||||
|
||||
协议声明中声明一个类型方法,方法声明语句必须用 `static` 声明修饰符。结构体和枚举遵循协议时,必须使用 `static` 关键字修饰,而类遵循协议时,使用 `static` 或 `class` 关键字皆可。当结构体,枚举或类添加扩展遵循协议时,和之前扩展用到的关键字保持一致。扩展为类方法提供默认实现时,必须使用 `static` 关键字修饰。
|
||||
|
||||
另请参阅 [函数声明](#function_declaration)。
|
||||
另请参阅 [函数声明](#function-declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_declaration {#grammer-of-a-protocol-declaration}
|
||||
#### grammer-of-a-protocol-declaration {#grammer-of-a-protocol-declaration}
|
||||
> 协议方法声明语法
|
||||
>
|
||||
>
|
||||
#### protocol-method-declaration {#protocol-method-declaration}
|
||||
>
|
||||
> *协议方法声明* → [*函数头*](#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>
|
||||
> *协议方法声明* → [*函数头*](#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>
|
||||
>
|
||||
|
||||
### 协议构造器声明 {#protocol-initializer-declaration}
|
||||
@ -1117,18 +1117,18 @@ var 属性名: 类型 { get set }
|
||||
|
||||
类在实现一个构造器去满足一个协议的构造器要求时,如果这个类还没有用 `final` 声明修饰符标记,这个构造器必须用 `required` 声明修饰符标记。
|
||||
|
||||
另请参阅 [构造器声明](#initializer_declaration)。
|
||||
另请参阅 [构造器声明](#initializer-declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_initializer_declaration {#grammer-of-a-protocol-initializer-declaration}
|
||||
#### grammer-of-a-protocol-initializer-declaration {#grammer-of-a-protocol-initializer-declaration}
|
||||
> 协议构造器声明语法
|
||||
>
|
||||
>
|
||||
#### protocol-initializer-declaration {#protocol-initializer-declaration}
|
||||
>
|
||||
> *协议构造器声明* → [*构造器头*](#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.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.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows** [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#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>
|
||||
>
|
||||
|
||||
### 协议下标声明 {#protocol-subscript-declaration}
|
||||
@ -1142,20 +1142,20 @@ subscript (参数列表) -> 返回类型 { get set }
|
||||
|
||||
协议声明中声明一个静态下标,下标声明语句必须用 `static` 声明修饰符。当结构体和枚举遵循该协议时,下标声明使用 `static` 关键字修饰,而类遵循该协议时,使用 `static` 或 `class` 关键字皆可。当结构体,枚举或类添加扩展遵循协议时,和之前扩展用到的关键字保持一致。扩展为下标声明提供默认实现时,必须使用 `static` 关键字修饰。
|
||||
|
||||
另请参阅 [下标声明](#subscript_declaration)。
|
||||
另请参阅 [下标声明](#subscript-declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_subscript_declaration {#grammer-of-a-protocol-subscript-declaration}
|
||||
#### grammer-of-a-protocol-subscript-declaration {#grammer-of-a-protocol-subscript-declaration}
|
||||
> 协议下标声明语法
|
||||
>
|
||||
>
|
||||
#### protocol-subscript-declaration {#protocol-subscript-declaration}
|
||||
>
|
||||
> *协议下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#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)
|
||||
>
|
||||
|
||||
### 协议关联类型声明 {#protocol-associated-type-declaration}
|
||||
使用关键字 `associatedtype` 来声明协议关联类型。关联类型为作为协议声明的一部分,为某种类型提供了一个别名。关联类型和泛型参数子句中的类型参数很相似,但是它们和 `Self` 一样,用于协议中。`Self` 指代采纳协议的类型。要获得更多信息和例子,请参阅 [关联类型](../02_language_guide/22_Generics.md#associated_types)。
|
||||
使用关键字 `associatedtype` 来声明协议关联类型。关联类型为作为协议声明的一部分,为某种类型提供了一个别名。关联类型和泛型参数子句中的类型参数很相似,但是它们和 `Self` 一样,用于协议中。`Self` 指代采纳协议的类型。要获得更多信息和例子,请参阅 [关联类型](../02_language_guide/22_Generics.md#associated-types)。
|
||||
|
||||
在协议声明中使用泛型 `where` 子句来为继承的协议关联类型添加约束,且不需要重新声明关联类型。例如下面代码中的 `SubProtocol` 声明。
|
||||
|
||||
@ -1175,16 +1175,16 @@ protocol SubProtocolB: SomeProtocol where SomeType: Equatable { }
|
||||
|
||||
|
||||
|
||||
另请参阅 [类型别名声明](#type_alias_declaration)。
|
||||
另请参阅 [类型别名声明](#type-alias-declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_associated_type_declaration {#grammer-of-a-protocol-associated-type-declaration}
|
||||
#### grammer-of-a-protocol-associated-type-declaration {#grammer-of-a-protocol-associated-type-declaration}
|
||||
> 协议关联类型声明语法
|
||||
>
|
||||
>
|
||||
#### protocol-associated-type-declaration {#protocol-associated-type-declaration}
|
||||
>
|
||||
> *协议关联类型声明* → [*特性列表*](./07_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>
|
||||
> *协议关联类型声明* → [*特性列表*](./07_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>
|
||||
>
|
||||
|
||||
## 构造器声明 {#initializer-declaration}
|
||||
@ -1268,18 +1268,18 @@ if let actualInstance = SomeStruct(input: "Hello") {
|
||||
|
||||
子类可以用任意种类的指定构造器重写超类的可失败指定构造器,但是只能用非可失败指定构造器重写超类的非可失败指定构造器。
|
||||
|
||||
更多关于可失败构造器的信息和例子,请参阅 [可失败构造器](../02_language_guide/14_Initialization.md#failable_initializers)。
|
||||
更多关于可失败构造器的信息和例子,请参阅 [可失败构造器](../02_language_guide/14_Initialization.md#failable-initializers)。
|
||||
|
||||
|
||||
#### grammer_of_an_initializer_declaration {#grammer-of-an-initializer-declaration}
|
||||
#### grammer-of-an-initializer-declaration {#grammer-of-an-initializer-declaration}
|
||||
> 构造器声明语法
|
||||
>
|
||||
>
|
||||
#### initializer-declaration {#initializer-declaration}
|
||||
>
|
||||
> *构造器声明* → [*构造器头*](#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.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.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)
|
||||
> *构造器声明* → [*构造器头*](#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)
|
||||
>
|
||||
>
|
||||
#### initializer-head {#initializer-head}
|
||||
@ -1314,7 +1314,7 @@ deinit {
|
||||
关于如何在类声明中使用析构器的例子,请参阅 [析构过程](../02_language_guide/15_Deinitialization.md)。
|
||||
|
||||
|
||||
#### grammer_of_a_deinitializer_declaration {#grammer-of-a-deinitializer-declaration}
|
||||
#### grammer-of-a-deinitializer-declaration {#grammer-of-a-deinitializer-declaration}
|
||||
> 析构器声明语法
|
||||
>
|
||||
>
|
||||
@ -1495,23 +1495,23 @@ extension Array: Loggable where Element: MarkedLoggable { }
|
||||
```
|
||||
|
||||
|
||||
#### grammer_of_an_extension_declaration {#grammer-of-an-extension-declaration}
|
||||
#### grammer-of-an-extension-declaration {#grammer-of-an-extension-declaration}
|
||||
> 扩展声明语法
|
||||
>
|
||||
>
|
||||
>
|
||||
#### extension-declaration {#extension-declaration}
|
||||
>
|
||||
> *扩展声明* → [特性](./07_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 子句*](./09_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub> [*扩展主体*](#extension-body)
|
||||
> *扩展声明* → [特性](./07_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 子句*](./09-Generic-Parameters-and-Arguments.md#generic-where-clause)<sub>可选</sub> [*扩展主体*](#extension-body)
|
||||
>
|
||||
>
|
||||
#### extension-body {#extension-body}
|
||||
>
|
||||
> *扩展主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
|
||||
>
|
||||
> *多条声明* → [单条声明](#subscript_declaration) [多条声明](#declarations) <sub>可选</sub>
|
||||
> *多条声明* → [单条声明](#subscript-declaration) [多条声明](#declarations) <sub>可选</sub>
|
||||
>
|
||||
> *单条声明* → [声明语句](#declarations) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||
> *单条声明* → [声明语句](#declarations) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
|
||||
## 下标声明 {#subscript-declaration}
|
||||
@ -1540,7 +1540,7 @@ subscript (参数列表) -> 返回类型 {
|
||||
|
||||
下标参数遵循与函数参数相同的规则,但有两个例外。默认情况下,下标中使用的参数不需要指定标签,这与函数,方法和构造器不同。但是你也可以同它们一样,显式地提供参数标签。此外,下标不能有 `In-out` 参数。
|
||||
|
||||
同样可以在协议声明中声明下标,正如 [协议下标声明](#protocol_subscript_declaration) 中所述。
|
||||
同样可以在协议声明中声明下标,正如 [协议下标声明](#protocol-subscript-declaration) 中所述。
|
||||
|
||||
更多关于下标的信息和例子,请参阅 [下标](../02_language_guide/12_Subscripts.md)。
|
||||
|
||||
@ -1550,28 +1550,28 @@ subscript (参数列表) -> 返回类型 {
|
||||
|
||||
|
||||
|
||||
#### grammer_of_a_subscript_declaration {#grammer-of-a-subscript-declaration}
|
||||
#### grammer-of-a-subscript-declaration {#grammer-of-a-subscript-declaration}
|
||||
> 下标声明语法
|
||||
>
|
||||
>
|
||||
>
|
||||
#### subscript-declaration {#subscript-declaration}
|
||||
>
|
||||
> *下标声明* → [*下标头*](#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.md#generic-where-clause)<sub>可选</sub> [*代码块*](#code-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.md#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-keyword-block)
|
||||
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08-Generic-Parameters-and-Arguments.md#generic-where-clause)<sub>可选</sub> [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||
>
|
||||
>
|
||||
#### subscript-head {#subscript-head}
|
||||
>
|
||||
> *下标头* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **subscript** [*泛型参数子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause)
|
||||
> *下标头* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **subscript** [*泛型参数子句*](08-Generic-Parameters-and-Arguments.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause)
|
||||
>
|
||||
>
|
||||
#### subscript-result {#subscript-result}
|
||||
>
|
||||
> *下标结果* → **->** [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*类型*](./03_Types.md#type)
|
||||
> *下标结果* → **->** [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*类型*](./03-Types.md#type)
|
||||
>
|
||||
|
||||
## 运算符声明 {#operator-declaration}
|
||||
@ -1589,7 +1589,7 @@ infix operator 运算符名称: 优先级组
|
||||
|
||||
中缀运算符是二元运算符,置于两个运算对象之间,例如加法运算符(`+`)位于表达式 `1 + 2` 的中间。
|
||||
|
||||
中缀运算符可以选择指定优先级组。如果没有为运算符设置优先级组,Swift 会设置默认优先级组 `DefaultPrecedence`,它的优先级比三目优先级 `TernaryPrecedence` 要高,更多内容参考[*优先级组声明*](#precedence_group_declaration_modifiers)
|
||||
中缀运算符可以选择指定优先级组。如果没有为运算符设置优先级组,Swift 会设置默认优先级组 `DefaultPrecedence`,它的优先级比三目优先级 `TernaryPrecedence` 要高,更多内容参考[*优先级组声明*](#precedence-group-declaration-modifiers)
|
||||
|
||||
下面的形式声明了一个新的前缀运算符:
|
||||
|
||||
@ -1611,10 +1611,10 @@ postfix operator 运算符名称 {}
|
||||
|
||||
和前缀运算符一样,后缀运算符的声明中不指定优先级,而且后缀运算符是非结合的。
|
||||
|
||||
声明了一个新的运算符以后,需要实现一个跟这个运算符同名的函数来实现这个运算符。如果是实现一个前缀或者后缀运算符,也必须使用相符的 `prefix` 或者 `postfix` 声明修饰符标记函数声明。如果是实现中缀运算符,则不需要使用 `infix` 声明修饰符标记函数声明。关于如何实现一个新的运算符的例子,请参阅 [自定义运算符](../02_language_guide/27_Advanced_Operators.md#custom_operators)。
|
||||
声明了一个新的运算符以后,需要实现一个跟这个运算符同名的函数来实现这个运算符。如果是实现一个前缀或者后缀运算符,也必须使用相符的 `prefix` 或者 `postfix` 声明修饰符标记函数声明。如果是实现中缀运算符,则不需要使用 `infix` 声明修饰符标记函数声明。关于如何实现一个新的运算符的例子,请参阅 [自定义运算符](../02_language_guide/27_Advanced_Operators.md#custom-operators)。
|
||||
|
||||
|
||||
#### grammer_of_an_operator_declaration {#grammer-of-an-operator-declaration}
|
||||
#### grammer-of-an-operator-declaration {#grammer-of-an-operator-declaration}
|
||||
> 运算符声明语法
|
||||
>
|
||||
|
||||
@ -1667,7 +1667,7 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
优先级组的赋值性表示在包含可选链操作时的运算符优先级。当设为 true 时,与优先级组对应的运算符在可选链操作中使用和标准库中赋值运算符同样的分组规则,当设为 false 或者不设置,该优先级组的运算符与不赋值的运算符遵循同样的可选链规则。
|
||||
|
||||
|
||||
#### grammer_of_a_precedence_group_declaration {#grammer-of-a-precedence-group-declaration}
|
||||
#### grammer-of-a-precedence-group-declaration {#grammer-of-a-precedence-group-declaration}
|
||||
> 优先级组声明语法
|
||||
>
|
||||
|
||||
@ -1734,13 +1734,13 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
|
||||
`lazy`
|
||||
|
||||
该修饰符用于修饰类或结构体中的存储型变量属性,表示该属性的初始值最多只被计算和存储一次,且发生在它被第一次访问时。关于如何使用 `lazy` 修饰符的例子,请参阅 [惰性存储型属性](../02_language_guide/10_Properties.md#lazy_stored_properties)。
|
||||
该修饰符用于修饰类或结构体中的存储型变量属性,表示该属性的初始值最多只被计算和存储一次,且发生在它被第一次访问时。关于如何使用 `lazy` 修饰符的例子,请参阅 [惰性存储型属性](../02_language_guide/10_Properties.md#lazy-stored-properties)。
|
||||
|
||||
`optional`
|
||||
|
||||
该修饰符用于修饰协议中的属性、方法以及下标成员,表示符合类型可以不实现这些成员要求。
|
||||
|
||||
只能将 `optional` 修饰符用于被 `objc` 特性标记的协议。这样一来,就只有类类型可以采纳并符合拥有可选成员要求的协议。关于如何使用 `optional` 修饰符,以及如何访问可选协议成员(比如,不确定符合类型是否已经实现了这些可选成员)的信息,请参阅 [可选协议要求](../02_language_guide/21_Protocols.md#optional_protocol_requirements)。
|
||||
只能将 `optional` 修饰符用于被 `objc` 特性标记的协议。这样一来,就只有类类型可以采纳并符合拥有可选成员要求的协议。关于如何使用 `optional` 修饰符,以及如何访问可选协议成员(比如,不确定符合类型是否已经实现了这些可选成员)的信息,请参阅 [可选协议要求](../02_language_guide/21_Protocols.md#optional-protocol-requirements)。
|
||||
|
||||
`required`
|
||||
|
||||
@ -1752,7 +1752,7 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
|
||||
`unowned`
|
||||
|
||||
该修饰符用于修饰存储型变量、常量或者存储型变量属性,表示该变量或属性持有其存储对象的无主引用。如果在此存储对象释放后尝试访问该对象,会引发运行时错误。如同弱引用一样,该引用类型的变量或属性必须是类类型。与弱引用不同的是,这种类型的变量或属性是非可选的。关于 `unowned` 更多的信息和例子,请参阅 [无主引用](../02_language_guide/24_Automatic_Reference_Counting.md#unowned_references)
|
||||
该修饰符用于修饰存储型变量、常量或者存储型变量属性,表示该变量或属性持有其存储对象的无主引用。如果在此存储对象释放后尝试访问该对象,会引发运行时错误。如同弱引用一样,该引用类型的变量或属性必须是类类型。与弱引用不同的是,这种类型的变量或属性是非可选的。关于 `unowned` 更多的信息和例子,请参阅 [无主引用](../02_language_guide/24_Automatic_Reference_Counting.md#unowned-references)
|
||||
|
||||
`unowned(safe)`
|
||||
|
||||
@ -1760,11 +1760,11 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
|
||||
`unowned(unsafe)`
|
||||
|
||||
该修饰符用于修饰存储型变量、常量或者存储型变量属性,表示该变量或属性持有其存储对象的无主引用。如果在此存储对象释放后尝试访问该对象,会直接访问该对象释放前存储的内存地址,因此这是非内存安全的操作。如同弱引用一样,该引用类型的变量或属性必须是类类型。与弱引用不同的是,这种类型的变量或属性是非可选的。关于 `unowned` 更多的信息和例子,请参阅 [无主引用](../02_language_guide/24_Automatic_Reference_Counting.md#resolving_strong_reference_cycles_between_class_instances)。
|
||||
该修饰符用于修饰存储型变量、常量或者存储型变量属性,表示该变量或属性持有其存储对象的无主引用。如果在此存储对象释放后尝试访问该对象,会直接访问该对象释放前存储的内存地址,因此这是非内存安全的操作。如同弱引用一样,该引用类型的变量或属性必须是类类型。与弱引用不同的是,这种类型的变量或属性是非可选的。关于 `unowned` 更多的信息和例子,请参阅 [无主引用](../02_language_guide/24_Automatic_Reference_Counting.md#resolving-strong-reference-cycles-between-class-instances)。
|
||||
|
||||
`weak`
|
||||
|
||||
该修饰符用于修饰变量或存储型变量属性,表示该变量或属性持有其存储的对象的弱引用。这种变量或属性的类型必须是可选的类类型。使用 `weak` 修饰符可避免强引用循环。关于 `weak` 修饰符的更多信息和例子,请参阅 [弱引用](../02_language_guide/24_Automatic_Reference_Counting.md#resolving_strong_reference_cycles_between_class_instances)。
|
||||
该修饰符用于修饰变量或存储型变量属性,表示该变量或属性持有其存储的对象的弱引用。这种变量或属性的类型必须是可选的类类型。使用 `weak` 修饰符可避免强引用循环。关于 `weak` 修饰符的更多信息和例子,请参阅 [弱引用](../02_language_guide/24_Automatic_Reference_Counting.md#resolving-strong-reference-cycles-between-class-instances)。
|
||||
|
||||
### 访问控制级别 {#access-control-levels}
|
||||
Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`。可以使用以下任意一种访问级别修饰符来指定声明的访问级别。访问控制在 [访问控制](../02_language_guide/26_Access_Control.md) 中有详细讨论。
|
||||
@ -1781,10 +1781,10 @@ Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`
|
||||
|
||||
该修饰符表示声明只能被所在源文件的代码访问。
|
||||
|
||||
以上访问级别修饰符都可以选择带上一个参数,该参数由一对圆括号和其中的 `set` 关键字组成(例如,`private(set)`)。使用这种形式的访问级别修饰符来限制某个属性或下标的 setter 的访问级别低于其本身的访问级别,正如 [Getter 和 Setter](../02_language_guide/26_Access_Control.md#getters_and_setters) 中所讨论的。
|
||||
以上访问级别修饰符都可以选择带上一个参数,该参数由一对圆括号和其中的 `set` 关键字组成(例如,`private(set)`)。使用这种形式的访问级别修饰符来限制某个属性或下标的 setter 的访问级别低于其本身的访问级别,正如 [Getter 和 Setter](../02_language_guide/26_Access_Control.md#getters-and-setters) 中所讨论的。
|
||||
|
||||
|
||||
#### grammer_of_a_declaration_modifier {#grammer-of-a-declaration-modifier}
|
||||
#### grammer-of-a-declaration-modifier {#grammer-of-a-declaration-modifier}
|
||||
> 声明修饰符的语法
|
||||
>
|
||||
|
||||
|
||||
@ -304,7 +304,7 @@ NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
|
||||
|
||||
如果你将 `objc` 特性应用于枚举,每一个枚举 case 都会以枚举名称和 case 名称组合的方式暴露在 Objective-C 代码中。实例名称的首字母大写。例如,在 Swift 枚举类型 `Planet` 中有一个名为 `Venus` 的 case,该 case 暴露在 Objective-C 代码中时叫做 `PlanetVenus`。
|
||||
|
||||
`objc` 特性可以接受一个可选的特性参数,由标识符构成。当你想把 `objc` 所修饰的实体以一个不同的名字暴露给 Objective-C 时,你就可以使用这个特性参数。你可以使用这个参数来命名类、枚举类型、枚举 case、协议、方法、存取方法以及构造器。如果你要指定类、协议或枚举在 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 时,你就可以使用这个特性参数。你可以使用这个参数来命名类、枚举类型、枚举 case、协议、方法、存取方法以及构造器。如果你要指定类、协议或枚举在 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`,而不是它原来的属性名。
|
||||
|
||||
```swift
|
||||
class ExampleClass: NSObject {
|
||||
@ -387,7 +387,7 @@ s.$x // SomeProjection value
|
||||
s.$x.wrapper // WrapperWithProjection value
|
||||
```
|
||||
|
||||
### `requires_stored_property_inits` {#requires-stored-property-inits}
|
||||
### `requires-stored-property-inits` {#requires-stored-property-inits}
|
||||
|
||||
该特性用于类声明,以要求类中所有存储属性提供默认值作为其定义的一部分。对于从中继承的任何类都推断出 `NSManagedObject` 特性。
|
||||
|
||||
@ -409,7 +409,7 @@ s.$x.wrapper // WrapperWithProjection value
|
||||
|
||||
标记为 `inlinable` 特性的声明,在内联代码中可以隐式使用。虽然 `inlinable` 或 `usableFromInline` 可以用于 `internal` 声明,但这两者不能同时使用。
|
||||
|
||||
### `warn_unqualified_access` {#warn-unqualified-access}
|
||||
### `warn-unqualified-access` {#warn-unqualified-access}
|
||||
|
||||
该特性应用于顶级函数、实例方法、类方法或静态方法,以在没有前置限定符(例如模块名称、类型名称、实例变量或常量)的情况下使用该函数或方法时触发警告。使用该特性可以减少在同一作用域里访问的同名函数之间的歧义。
|
||||
|
||||
@ -429,7 +429,7 @@ Interface Builder 特性是 Interface Builder 用来与 Xcode 同步的声明特
|
||||
|
||||
### `autoclosure` {#autoclosure}
|
||||
|
||||
这个特性通过把表达式自动封装成无参数的闭包来延迟表达式的计算。它可以修饰类型为返回表达式结果类型的无参数函数类型的函数参数。关于如何使用 `autoclosure` 特性的例子,请参阅 [自动闭包](../02_language_guide/07_Closures.md#autoclosures) 和 [函数类型](./03_Types.md#function_type)。
|
||||
这个特性通过把表达式自动封装成无参数的闭包来延迟表达式的计算。它可以修饰类型为返回表达式结果类型的无参数函数类型的函数参数。关于如何使用 `autoclosure` 特性的例子,请参阅 [自动闭包](../02_language_guide/07_Closures.md#autoclosures) 和 [函数类型](./03-Types.md#function-type)。
|
||||
|
||||
### `convention` {#convention}
|
||||
|
||||
@ -447,7 +447,7 @@ Interface Builder 特性是 Interface Builder 用来与 Xcode 同步的声明特
|
||||
|
||||
### `escaping` {#escaping}
|
||||
|
||||
在函数或者方法声明上使用该特性,它表示参数将不会被存储以供延迟执行。这将确保参数不会超出函数调用的生命周期。在使用 `escaping` 特性声明的函数类型中访问属性和方法时需要显式地使用 `self.`。关于如何使用 `escaping` 特性的例子,请参阅 [逃逸闭包](../02_language_guide/07_Closures.md#escaping_closures)。
|
||||
在函数或者方法声明上使用该特性,它表示参数将不会被存储以供延迟执行。这将确保参数不会超出函数调用的生命周期。在使用 `escaping` 特性声明的函数类型中访问属性和方法时需要显式地使用 `self.`。关于如何使用 `escaping` 特性的例子,请参阅 [逃逸闭包](../02_language_guide/07_Closures.md#escaping-closures)。
|
||||
|
||||
## Switch Case 特性 {#switch-case-attributes}
|
||||
|
||||
@ -463,17 +463,17 @@ Interface Builder 特性是 Interface Builder 用来与 Xcode 同步的声明特
|
||||
>
|
||||
#### attribute {#attribute}
|
||||
>
|
||||
> *特性* → @ [特性名](#attribute_name) [特性参数子句](#atribute_argument_clause)<sub>可选</sub>
|
||||
> *特性* → @ [特性名](#attribute-name) [特性参数子句](#atribute-argument-clause)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### attribute_name {#attribute-name}
|
||||
#### attribute-name {#attribute-name}
|
||||
>
|
||||
> *特性名* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
>
|
||||
#### atribute_argument_clause {#atribute-argument-clause}
|
||||
#### atribute-argument-clause {#atribute-argument-clause}
|
||||
>
|
||||
> *特性参数子句* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)**
|
||||
> *特性参数子句* → **(** [均衡令牌列表](#balanced-tokens)<sub>可选</sub> **)**
|
||||
>
|
||||
>
|
||||
#### attributes {#attributes}
|
||||
@ -482,18 +482,18 @@ Interface Builder 特性是 Interface Builder 用来与 Xcode 同步的声明特
|
||||
>
|
||||
>
|
||||
>
|
||||
#### balanced_tokens {#balanced-tokens}
|
||||
#### balanced-tokens {#balanced-tokens}
|
||||
>
|
||||
> *均衡令牌列表* → [均衡令牌](#balanced_token) [均衡令牌列表](#balanced_tokens)<sub>可选</sub>
|
||||
> *均衡令牌列表* → [均衡令牌](#balanced-token) [均衡令牌列表](#balanced-tokens)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### balanced_token {#balanced-token}
|
||||
#### balanced-token {#balanced-token}
|
||||
>
|
||||
> *均衡令牌* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)**
|
||||
> *均衡令牌* → **(** [均衡令牌列表](#balanced-tokens)<sub>可选</sub> **)**
|
||||
>
|
||||
> *均衡令牌* → **\[** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **\]**
|
||||
> *均衡令牌* → **\[** [均衡令牌列表](#balanced-tokens)<sub>可选</sub> **\]**
|
||||
>
|
||||
> *均衡令牌* → **{** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **}**
|
||||
> *均衡令牌* → **{** [均衡令牌列表](#balanced-tokens)<sub>可选</sub> **}**
|
||||
>
|
||||
> *均衡令牌* → 任意标识符,关键字,字面量或运算符
|
||||
>
|
||||
|
||||
@ -12,13 +12,13 @@ Swift 中的模式分为两类:一种能成功匹配任何类型的值,另
|
||||
>
|
||||
|
||||
#### pattern {#pattern}
|
||||
> *模式* → [*通配符模式*](#wildcard_pattern) [*类型注解*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [*通配符模式*](#wildcard-pattern) [*类型注解*](03-Types.md#type-annotation)<sub>可选</sub>
|
||||
>
|
||||
> *模式* → [*标识符模式*](#identifier_pattern) [*类型注解*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [*标识符模式*](#identifier-pattern) [*类型注解*](03-Types.md#type-annotation)<sub>可选</sub>
|
||||
>
|
||||
> *模式* → [*值绑定模式*](#value-binding-pattern)
|
||||
>
|
||||
> *模式* → [*元组模式*](#tuple-pattern) [*类型注解*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [*元组模式*](#tuple-pattern) [*类型注解*](03-Types.md#type-annotation)<sub>可选</sub>
|
||||
>
|
||||
> *模式* → [*枚举用例模式*](#enum-case-pattern)
|
||||
>
|
||||
@ -129,13 +129,13 @@ let (a): Int = 2 // a: Int = 2
|
||||
## 枚举用例模式(Enumeration Case Pattern) {#enumeration-case-pattern}
|
||||
*枚举用例模式*匹配现有的某个枚举类型的某个用例。枚举用例模式出现在 `switch` 语句中的 `case` 标签中,以及 `if`、`while`、`guard` 和 `for-in` 语句的 `case` 条件中。
|
||||
|
||||
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../02_language_guide/08_Enumerations.md#associated_values)。
|
||||
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../02_language_guide/08_Enumerations.md#associated-values)。
|
||||
|
||||
> 枚举用例模式语法
|
||||
>
|
||||
|
||||
#### enum-case-pattern {#enum-case-pattern}
|
||||
> *枚举用例模式* → [*类型标识*](./03_Types.md#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](./06_Declarations.md#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
|
||||
> *枚举用例模式* → [*类型标识*](./03_Types.md#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](./06-Declarations.md#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
|
||||
>
|
||||
|
||||
## 可选模式(Optional Pattern) {#optional-pattern}
|
||||
@ -188,7 +188,7 @@ for case let number? in arrayOfOptinalInts {
|
||||
|
||||
`as` 模式仅当一个值的类型在运行时和 `as` 模式右边的指定类型一致,或者是其子类的情况下,才会匹配这个值。如果匹配成功,被匹配的值的类型被转换成 `as` 模式右边指定的类型。
|
||||
|
||||
关于使用 `switch` 语句配合 `is` 模式和 `as` 模式来匹配值的例子,请参阅 [Any 和 AnyObject 的类型转换](../02_language_guide/18_Type_Casting.md#type_casting_for_any_and_anyobject)。
|
||||
关于使用 `switch` 语句配合 `is` 模式和 `as` 模式来匹配值的例子,请参阅 [Any 和 AnyObject 的类型转换](../02_language_guide/18_Type_Casting.md#type-casting-for-any-and-anyobject)。
|
||||
|
||||
> 类型转换模式语法
|
||||
>
|
||||
@ -202,7 +202,7 @@ for case let number? in arrayOfOptinalInts {
|
||||
>
|
||||
|
||||
#### as-pattern {#as-pattern}
|
||||
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.md#type)
|
||||
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03-Types.md#type)
|
||||
>
|
||||
|
||||
## 表达式模式(Expression Pattern) {#expression-pattern}
|
||||
|
||||
@ -51,7 +51,7 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
|
||||
泛型函数或构造器可以重载,但在泛型形参子句中的类型形参必须有不同的约束或要求,抑或二者皆不同。当调用重载的泛型函数或构造器时,编译器会根据这些约束来决定调用哪个重载函数或构造器。
|
||||
|
||||
更多关于泛型 where 从句的信息和关于泛型函数声明的例子,可以看一看 [泛型 where 子句](../02_language_guide/22_Generics.md#where_clauses)。
|
||||
更多关于泛型 where 从句的信息和关于泛型函数声明的例子,可以看一看 [泛型 where 子句](../02_language_guide/22_Generics.md#where-clauses)。
|
||||
|
||||
> 泛型形参子句语法
|
||||
>
|
||||
@ -67,9 +67,9 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
#### generic-parameter {#generic-parameter}
|
||||
> *泛形形参* → [*类型名称*](./03_Types.md#type-name)
|
||||
>
|
||||
> *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*类型标识符*](./03_Types.md#type-identifier)
|
||||
> *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*类型标识符*](./03-Types.md#type-identifier)
|
||||
>
|
||||
> *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*协议合成类型*](./03_Types.md#protocol-composition-type)
|
||||
> *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*协议合成类型*](./03-Types.md#protocol-composition-type)
|
||||
>
|
||||
>
|
||||
#### requirement-clause {#requirement-clause}
|
||||
@ -87,13 +87,13 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
>
|
||||
#### conformance-requirement {#conformance-requirement}
|
||||
>
|
||||
> *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*类型标识符*](./03_Types.md#type-identifier)
|
||||
> *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*类型标识符*](./03-Types.md#type-identifier)
|
||||
>
|
||||
> *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*协议合成类型*](./03_Types.md#protocol-composition-type)
|
||||
> *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*协议合成类型*](./03-Types.md#protocol-composition-type)
|
||||
>
|
||||
|
||||
#### same-type-requirement {#same-type-requirement}
|
||||
> *同类型约束* → [*类型标识符*](./03_Types.md#type-identifier) **==** [*类型*](./03_Types.md#type)
|
||||
> *同类型约束* → [*类型标识符*](./03_Types.md#type-identifier) **==** [*类型*](./03-Types.md#type)
|
||||
>
|
||||
|
||||
## 泛型实参子句 {#generic-argument}
|
||||
@ -118,7 +118,7 @@ struct Dictionary<Key: Hashable, Value>: CollectionType, DictionaryLiteralConver
|
||||
let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
```
|
||||
|
||||
如 [泛型形参子句](#generic_parameter) 所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。
|
||||
如 [泛型形参子句](#generic-parameter) 所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。
|
||||
|
||||
> 泛型实参子句语法
|
||||
>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -4,7 +4,7 @@
|
||||
|
||||
* 更新至 Swift 5.1。
|
||||
* 在 [不透明类型](../02_language_guide/23_Opaque_Types.md) 篇章中新增了有关函数返回值遵循指定协议,而不需要提供指定返回类型的内容。
|
||||
* 新增 [隐式返回的函数](../02_language_guide/06_Functions.md#functions-with-an-implicit-return) 和 [简化 Getter 声明](../02_language_guide/10_Properties.md#shorthand-getter-declaration) 章节,其中包含函数省略 `return` 的内容。
|
||||
* 新增 [隐式返回的函数](../02_language_guide/06_Functions.md#functions-with-an-implicit-return) 和 [简化 Getter 声明](../02-language-guide/10-Properties.md#shorthand-getter-declaration) 章节,其中包含函数省略 `return` 的内容。
|
||||
* 在 [类型下标](../02_language_guide/12_Subscripts.md#type-subscripts) 章节中新增有关在类型中使用下标的内容。
|
||||
* 更新 [结构体的逐一成员构造器](../02_language_guide/14_Initialization.md#memberwise-initializers-for-structure-types) 章节,现在逐一成员构造器支持在属性有默认值时省略形参。
|
||||
* 在 [动态查找成员](../03_language_reference/07_Attributes.md#dynamicmemberlookup) 章节中新增了有关在运行时用 key path 查找动态成员的内容。
|
||||
@ -13,9 +13,9 @@
|
||||
### 2019-03-25
|
||||
|
||||
* 更新至 Swift 5。
|
||||
* 新增 [拓展字符串分隔符](../02_language_guide/03_Strings_And_Characters.md#extended-string-delimiters) 章节。更新 [字符串字面量](../03_language_reference/03_Lexical_Structure.md#string-literal) 章节,拓展有关字符串分隔符的内容。
|
||||
* 新增 [拓展字符串分隔符](../02_language_guide/03_Strings_And_Characters.md#extended-string-delimiters) 章节。更新 [字符串字面量](../03-language-reference/03-Lexical-Structure.md#string-literal) 章节,拓展有关字符串分隔符的内容。
|
||||
* 新增 [动态调用](../03_language_reference/07_Attributes.md#dynamiccallable) 章节,其中包含使用 `dynamicCallable` 属性动态调用实例作为函数的内容。
|
||||
* 新增 [unknown](../03_language_reference/07_Attributes.md#unknown) 和 [未来枚举匹配](../03_language_reference/05_Statements.md#future-case2) 章节,其中包含了使用 `unknown` 来处理未来枚举可能发生改变的情形。
|
||||
* 新增 [unknown](../03_language_reference/07_Attributes.md#unknown) 和 [未来枚举匹配](../03-language-reference/05-Statements.md#future-case2) 章节,其中包含了使用 `unknown` 来处理未来枚举可能发生改变的情形。
|
||||
* 在 [Key-Path 表达式](../03_language_reference/04_Expressions.md#key-path-expression) 章节新增了有关标示 key path (\\.self) 的内容。
|
||||
* 在 [可选编译块](../03_language_reference/05_Statements.md#Conditional-Compilation-Block) 章节新增了有关小于比较符 `<` 的内容。
|
||||
|
||||
@ -25,7 +25,7 @@
|
||||
* 在 [遍历枚举情形](../02_language_guide/08_Enumerations.md#iterating-over-enumeration-cases) 章节新增了有关访问所有枚举情形的内容。
|
||||
* 在 [编译诊断](../03_language_reference/05_Statements.md#compile-time-diagnostic-statement) 章节新增了有关 `#error` 和 `#warning` 的内容。
|
||||
* 在 [属性声明](../03_language_reference/07_Attributes.md#Ideclaration-attributes) 章节中新增了有关 `inlinable` 和 `usableFromInline` 属性的内容。
|
||||
* 在 [属性声明](../03_language_reference/07_Attributes.md#Ideclaration-attributes) 章节中新增了有关 `requires_stored_property_inits` 和 `warn_unqualified_access` 属性的内容。
|
||||
* 在 [属性声明](../03_language_reference/07_Attributes.md#Ideclaration-attributes) 章节中新增了有关 `requires-stored-property-inits` 和 `warn-unqualified-access` 属性的内容。
|
||||
* 在 [可选编译块](../03_language_reference/05_Statements.md#Conditional-Compilation-Block) 章节新增了有关如何根据 Swift 编译器版本对代码进行对应编译处理的内容。
|
||||
* 在 [字面量语法](../03_language_reference/04_Expressions.md#literal-expression) 章节新增了有关 `#dsohandle` 的内容。
|
||||
|
||||
@ -33,7 +33,7 @@
|
||||
|
||||
* 更新至 Swift 4.1。
|
||||
* 在 [等价运算符](../02_language_guide/27_Advanced_Operators.md#equivalence-operators) 章节新增了有关等价运算符的合成实现的内容。
|
||||
* 在 [声明](../03_language_reference/06_Declarations.md) 篇章中 [申明拓展](../03_language_reference/06_Declarations.md#extension-declaration) 章节和 [协议](../02_language_guide/21_Protocols.md) 篇章中 [有条件地遵循协议](../02_language_guide/21_Protocols.md#Conditionally-Conforming-to-a-Protocol) 章节新增了有关协议有条件遵循的内容。
|
||||
* 在 [声明](../03_language_reference/06_Declarations.md) 篇章中 [申明拓展](../03_language_reference/06_Declarations.md#extension-declaration) 章节和 [协议](../02-language-guide/21-Protocols.md) 篇章中 [有条件地遵循协议](../02-language-guide/21-Protocols.md#Conditionally-Conforming-to-a-Protocol) 章节新增了有关协议有条件遵循的内容。
|
||||
* 在 [关联类型约束中使用协议](../02_language_guide/22_Generics.md#using-a-protocol-in-its-associated-types-constraints) 章节中新增了有关递归协议约束的内容。
|
||||
* 在 [条件编译块](../03_language_reference/05_Statements.md#Conditional-Compilation-Block) 章节中新增了有关 `canImport()` 和 `targetEnvironment()` 平台条件的内容。
|
||||
|
||||
@ -47,10 +47,10 @@
|
||||
* 更新至 Swift 4.0。
|
||||
* 在 [内存安全](../02_language_guide/24_MemorySafety.md) 章节新增了有关内存互斥访问的内容。
|
||||
* 新增 [带有泛型 Where 子句联类型](../02_language_guide/22_Generics.md#associated-types-with-a-generic-where-clause) 章节,现在可以使用泛型 `where` 子句约束关联类型。
|
||||
* 在 [字符串和字符](../02_language_guide/03_Strings_And_Characters.md) 篇章中 [字面量](../02_language_guide/03_Strings_And_Characters.md#string-literals) 章节以及 [词法结构](../03_language_reference/02_Lexical_Structure.md) 篇章的 [字符串字面量](../03_language_reference/02_Lexical_Structure.md#string-literal) 章节中新增了有关多行字符串字面量的内容。
|
||||
* 在 [字符串和字符](../02_language_guide/03_Strings_And_Characters.md) 篇章中 [字面量](../02_language_guide/03_Strings_And_Characters.md#string-literals) 章节以及 [词法结构](../03-language-reference/02-Lexical-Structure.md) 篇章的 [字符串字面量](../03-language-reference/02-Lexical-Structure.md#string-literal) 章节中新增了有关多行字符串字面量的内容。
|
||||
* 更新 [声明属性](../03_language_reference/07_Attributes.md#Ideclaration-attributes) 中 `objc` 属性的讨论,现在该属性会在更少的位置被推断出来。
|
||||
* 新增 [范型下标](../02_language_guide/22_Generics.md#generic-subscripts) 章节,现在下标也支持范型特性了。
|
||||
* 更新 [协议](../02_language_guide/21_Protocols.md) 篇章中 [协议组合](../02_language_guide/21_Protocols.md#protocol-composition) 章节和 [类型](../03_language_reference/03_Types.md) 篇章中 [协议组合类型](../03_language_reference/03_Types.md#protocol-composition-type-h) 章节的讨论,现在协议组合类型支持进行父类约束了。
|
||||
* 更新 [协议](../02_language_guide/21_Protocols.md) 篇章中 [协议组合](../02_language_guide/21_Protocols.md#protocol-composition) 章节和 [类型](../03-language-reference/03-Types.md) 篇章中 [协议组合类型](../03-language-reference/03-Types.md#protocol-composition-type-h) 章节的讨论,现在协议组合类型支持进行父类约束了。
|
||||
* 更新 [拓展声明](../03_language_reference/06_Declarations.md#extension-declaration) 中有关协议扩展的讨论,现在它们不支持 `final` 特性了。
|
||||
* 在 [断言和前置条件](../02_language_guide/01_TheBasics.md#assertions-and-preconditions) 章节中新增了部分前置条件和致命错误的内容。
|
||||
|
||||
@ -81,20 +81,20 @@
|
||||
* 更新 [高级操作符](../02_language_guide/27_Advanced_Operators.md) 篇章中有关操作符的讨论,现在你可以作为类型函数来实现,替代之前的全局函数实现方式。
|
||||
* 在 [访问控制](../02_language_guide/26_Access_Control.md) 章节中新增有关对新的访问级别描述符 `open` 和 `fileprivate` 的内容。
|
||||
* 更新 [函数声明](../03_language_reference/06_Declarations.md#function-declaration) 章节中有关 `inout` 的讨论,注意它现在出现在参数类型的前面,而不是在参数名称的前面。
|
||||
* 更新 [逃逸闭包](../02_language_guide/07_Closures.md#escaping-closures) 和 [自动闭包](../02_language_guide/07_Closures.md#autoclosures) 章节还有 [属性](../03_language_reference/07_Attributes.md) 篇章中有关 `@noescape` 和 `@autoclosure` 的讨论,现在他们是类型属性,而不是定义属性。
|
||||
* 在 [高级操作符](../02_language_guide/27_Advanced_Operators.md) 篇章中 [自定义中缀操作符的优先级](./02_language_guide/27_Advanced_Operators.md#precedence-and-associativity-for-custom-infix-operators) 章节和 [定义](../03_language_reference/06_Declarations.md) 篇章中 [优先级组声明](../03_language_reference/06_Declarations.md#precedence-group-declaration-modifiers) 章节中新增了有关操作符优先级组的内容。
|
||||
* 更新 [逃逸闭包](../02_language_guide/07_Closures.md#escaping-closures) 和 [自动闭包](../02-language-guide/07-Closures.md#autoclosures) 章节还有 [属性](../03-language-reference/07-Attributes.md) 篇章中有关 `@noescape` 和 `@autoclosure` 的讨论,现在他们是类型属性,而不是定义属性。
|
||||
* 在 [高级操作符](../02_language_guide/27_Advanced_Operators.md) 篇章中 [自定义中缀操作符的优先级](./02_language_guide/27_Advanced_Operators.md#precedence-and-associativity-for-custom-infix-operators) 章节和 [定义](../03-language-reference/06-Declarations.md) 篇章中 [优先级组声明](../03-language-reference/06-Declarations.md#precedence-group-declaration-modifiers) 章节中新增了有关操作符优先级组的内容。
|
||||
* 更新一些讨论,使用 macOS 替换掉 OS X, Error 替换掉 ErrorProtocol。更新一些协议名称,比如使用 ExpressibleByStringLiteral 替换掉 StringLiteralConvertible。
|
||||
* 更新 [泛型](../02_language_guide/22_Generics.md) 篇章中 [泛型 Where 语句](../02_language_guide/22_Generics.md#extensions-with-a-generic-where-clause) 章节和 [泛型形参和实参](../03_language_reference/09_Generic_Parameters_And_Arguments.md) 篇章的讨论,现在泛型的 where 语句写在一个声明的最后。
|
||||
* 更新 [泛型](../02_language_guide/22_Generics.md) 篇章中 [泛型 Where 语句](../02_language_guide/22_Generics.md#extensions-with-a-generic-where-clause) 章节和 [泛型形参和实参](../03-language-reference/09-Generic-Parameters-And-Arguments.md) 篇章的讨论,现在泛型的 where 语句写在一个声明的最后。
|
||||
* 更新 [逃逸闭包](../02_language_guide/07_Closures.md#escaping-closures) 章节中的讨论,现在闭包默认为非逃逸的。
|
||||
* 更新 [基础部分](../02_language_guide/01_TheBasics.md) 篇章中 [可选绑定](../02_language_guide/01_TheBasics.md#optional-binding) 章节和 [语句](../03_language_reference/05_Statements.md) 篇章中 [While 语句](../03_language_reference/05_Statements.md#while-statement) 章节中的讨论,现在 if,`while` 和 `guard` 语句使用逗号分隔条件列表,不需要使用 `where` 语句。
|
||||
* 在 [控制流](../02_language_guide/05_Control_Flow.md) 篇章中 [Switch](../02_language_guide/05_Control_Flow.md#switch) 章节和 [语句](../03_language_reference/05_Statements.md) 篇章中 [Switch 语句](../03_language_reference/05_Statements.md#switch-statement) 章节中新增了 switch cases 可以使用多模式的内容。
|
||||
* 更新 [基础部分](../02_language_guide/01_TheBasics.md) 篇章中 [可选绑定](../02_language_guide/01_TheBasics.md#optional-binding) 章节和 [语句](../03-language-reference/05-Statements.md) 篇章中 [While 语句](../03-language-reference/05-Statements.md#while-statement) 章节中的讨论,现在 if,`while` 和 `guard` 语句使用逗号分隔条件列表,不需要使用 `where` 语句。
|
||||
* 在 [控制流](../02_language_guide/05_Control_Flow.md) 篇章中 [Switch](../02_language_guide/05_Control_Flow.md#switch) 章节和 [语句](../03-language-reference/05-Statements.md) 篇章中 [Switch 语句](../03-language-reference/05-Statements.md#switch-statement) 章节中新增了 switch cases 可以使用多模式的内容。
|
||||
* 更新 [函数类型](../03_language_reference/03_Types.md#function-type-h) 章节有关现在函数参数标签不包含在函数类型中的讨论。
|
||||
* 更新 [协议](../02_language_guide/21_Protocols.md) 篇章中 [协议组合](../02_language_guide/21_Protocols.md#protocol-composition) 章节和 [类型](../03_language_reference/03_Types.md) 篇章中 [协议组合类型](../03_language_reference/03_Types.md#protocol-composition-type-h) 章节中有关使用新的 Protocol1 & Protocol2 语法的内容。
|
||||
* 更新 [协议](../02_language_guide/21_Protocols.md) 篇章中 [协议组合](../02_language_guide/21_Protocols.md#protocol-composition) 章节和 [类型](../03-language-reference/03-Types.md) 篇章中 [协议组合类型](../03-language-reference/03-Types.md#protocol-composition-type-h) 章节中有关使用新的 Protocol1 & Protocol2 语法的内容。
|
||||
* 更新动态类型表达式章节中使用新的 `type(of:)` 表达式的讨论。
|
||||
* 更新 [行控制表达式](../03_language_reference/05_Statements.md#line-control-statement) 章节中使用 `#sourceLocation(file:line:)` 表达式的讨论。
|
||||
* 更新 [永不返回函数](../03_language_reference/06_Declarations.md#functions-that-never-return) 章节中使用 新的 `Never` 类型的讨论。
|
||||
* 在 [字面量表达式](../03_language_reference/04_Expressions.md#literal-expression) 章节中新增了有关 `playground` 字面量的内容。
|
||||
* 更新 [In-Out 参数](../03_language_reference/06_Declarations.md#in-out_parameters) 章节,标明只有非逃逸闭包能捕获 `in-out` 参数。
|
||||
* 更新 [In-Out 参数](../03_language_reference/06_Declarations.md#in-out-parameters) 章节,标明只有非逃逸闭包能捕获 `in-out` 参数。
|
||||
* 更新 [默认参数值](../02_language_guide/06_Functions.md#default-parameter-values) 章节,现在默认参数不能在调用时候重新排序。
|
||||
* 更新 [属性](../03_language_reference/07_Attributes.md) 篇章中有关属性参数使用分号的说明。
|
||||
* 在 [重新抛出函数和方法](../03_language_reference/06_Declarations.md#rethrowing-functions-and-methods) 章节中新增了有关在 catch 代码块中抛出错误的重新抛出函数的内容。
|
||||
@ -114,7 +114,7 @@
|
||||
* 在 [编译配置语句](../03_language_reference/05_Statements.md#Conditional-Compilation-Block) 章节新增了中有关如何根据 Swift 版本进行条件编译。
|
||||
* 在 [显示成员表达式](../03_language_reference/04_Expressions.md#explicit-member-expression) 章节中新增了有关如何区分只有参数名不同的方法和构造器的内容。
|
||||
* 在 [选择器表达式](../03_language_reference/04_Expressions.md#selector-expression7) 章节中新增了了针对 Objective-C 选择器的 `#selector` 语法。
|
||||
* 更新 [关联类型](../02_language_guide/22_Generics.md#associated-types) 和 [协议关联类型声明](../03_language_reference/06_Declarations.md#protocol_associated_type_declaration) 章节中有关使用 `associatedtype` 关键词修饰关联类型的讨论。
|
||||
* 更新 [关联类型](../02_language_guide/22_Generics.md#associated-types) 和 [协议关联类型声明](../03-language-reference/06-Declarations.md#protocol-associated-type-declaration) 章节中有关使用 `associatedtype` 关键词修饰关联类型的讨论。
|
||||
* 更新 [可失败构造器](../02_language_guide/14_Initialization.md#failable-initializers) 章节中有关当构造器在实例完全初始化之前返回 `nil` 的相关内容。
|
||||
* 在 [比较运算符](../02_language_guide/BasicOperators.md#comparison-operators) 章节中新增了比较元组的内容。
|
||||
* 在 [关键字和标点符号](../03_language_reference/02_Lexical_Structure.md#keywords-and-punctuation) 章节中新增了使用关键字作为外部参数名的内容。
|
||||
@ -130,10 +130,10 @@
|
||||
### 2015-10-20
|
||||
|
||||
* 更新至 Swift 2.1。
|
||||
* 更新 [字符串插值](../02_language_guide/03_Strings_And_Characters.md#string-interpolation) 和 [字符串字面量](../03_language_reference/02_Lexical_Structure.md#string-literal) 章节,现在字符串插值可包含字符串字面量。
|
||||
* 更新 [字符串插值](../02_language_guide/03_Strings_And_Characters.md#string-interpolation) 和 [字符串字面量](../03-language-reference/02-Lexical-Structure.md#string-literal) 章节,现在字符串插值可包含字符串字面量。
|
||||
* 在 [逃逸闭包](../02_language_guide/07_Closures.md#escaping-closures) 章节中新增了有关 `@noescape` 属性的相关内容。
|
||||
* 更新 [声明特性](../03_language_reference/07_Attributes.md#Ideclaration-attributes) 和 [编译配置语句](../03_language_reference/05_Statements.md#Conditional-Compilation-Block) 章节中与 tvOS 相关的内容。
|
||||
* 在 [In-Out 参数](../03_language_reference/06_Declarations.md#in-out_parameters) 章节中新增了与 in-out 参数行为相关的内容。
|
||||
* 更新 [声明特性](../03_language_reference/07_Attributes.md#Ideclaration-attributes) 和 [编译配置语句](../03-language-reference/05-Statements.md#Conditional-Compilation-Block) 章节中与 tvOS 相关的内容。
|
||||
* 在 [In-Out 参数](../03_language_reference/06_Declarations.md#in-out-parameters) 章节中新增了与 in-out 参数行为相关的内容。
|
||||
* 在 [捕获列表](../03_language_reference/04_Expressions.md#capture-lists) 章节新增了有关指定闭包捕获列表被捕获时捕获值的相关内容。
|
||||
* 更新 [可选链式调用访问属性](../02_language_guide/16_Optional_Chaining.md#accessing-properties-through-optional-chaining) 章节,阐明了如何通过可选链式调用进行赋值。
|
||||
* 改进 [自动闭包](../02_language_guide/07_Closures.md#autoclosures) 章节中对自闭包的讨论。
|
||||
@ -142,21 +142,21 @@
|
||||
### 2015-09-16
|
||||
|
||||
* 更新至 Swift 2.0。
|
||||
* 在 [错误处理](../02_language_guide/17_Error_Handling.md) 篇章中新增了有关错误处理的相关内容,包括 [Do 语句](../03_language_reference/05_Statements.md#do-statement)、 [Throw 语句](../03_language_reference/05_Statements.md#throw-statement)、 [Defer 语句](../03_language_reference/05_Statements.md##defer-statements) 以及 [try 运算符](../03_language_reference/04_Expressions.md#try-operator)。
|
||||
* 在 [错误处理](../02_language_guide/17_Error_Handling.md) 篇章中新增了有关错误处理的相关内容,包括 [Do 语句](../03_language_reference/05_Statements.md#do-statement)、 [Throw 语句](../03-language-reference/05-Statements.md#throw-statement)、 [Defer 语句](../03-language-reference/05-Statements.md##defer-statements) 以及 [try 运算符](../03-language-reference/04-Expressions.md#try-operator)。
|
||||
* 更新 [错误表示和抛出](../02_language_guide/17_Error_Handling.md#representing-and-throwing-errors) 章节,现在所有类型都可以遵循 `ErrorType` 协议了。
|
||||
* 在 [将错误装换成可选值](../02_language_guide/17_Error_Handling.md#converting_errors_to_optional_values) 篇章增加了 `try?` 关键字相关内容。
|
||||
* 在 [枚举](../02_language_guide/08_Enumerations.md) 篇章的 [递归枚举](../02_language_guide/08_Enumerations.md#recursive-enumerations) 章节以及以及 [声明](../03_language_reference/06_Declarations.md) 篇章的 [任意类型用例的枚举](../03_language_reference/06_Declarations.md#enumerations-with-cases-of-any-type) 章节中新增了递归枚举相关内容。
|
||||
* 在 [控制流](../02_language_guide/05_Control_Flow.md) 篇章的 [API 可用性检查](../02_language_guide/05_Control_Flow.md#checking-api-availability) 章节和 [语句](../03_language_reference/05_Statements.md) 篇章的 [可用性条件](../03_language_reference/05_Statements.md#availability-condition) 章节中新增了有关 API 可用性检查相关的内容。
|
||||
* 在 [控制流](../02_language_guide/05_Control_Flow.md) 篇章的 [尽早退出](../02_language_guide/05_Control_Flow.md#early-exit) 章节和 [语句](../03_language_reference/05_Statements.md) 篇章的 [Guard 语句](../03_language_reference/05_Statements.md#guard-statement) 章节新增了与 `guard` 语句相关的内容。
|
||||
* 在 [将错误装换成可选值](../02_language_guide/17_Error_Handling.md#converting-errors-to-optional-values) 篇章增加了 `try?` 关键字相关内容。
|
||||
* 在 [枚举](../02_language_guide/08_Enumerations.md) 篇章的 [递归枚举](../02_language_guide/08_Enumerations.md#recursive-enumerations) 章节以及以及 [声明](../03-language-reference/06-Declarations.md) 篇章的 [任意类型用例的枚举](../03-language-reference/06-Declarations.md#enumerations-with-cases-of-any-type) 章节中新增了递归枚举相关内容。
|
||||
* 在 [控制流](../02_language_guide/05_Control_Flow.md) 篇章的 [API 可用性检查](../02_language_guide/05_Control_Flow.md#checking-api-availability) 章节和 [语句](../03-language-reference/05-Statements.md) 篇章的 [可用性条件](../03-language-reference/05-Statements.md#availability-condition) 章节中新增了有关 API 可用性检查相关的内容。
|
||||
* 在 [控制流](../02_language_guide/05_Control_Flow.md) 篇章的 [尽早退出](../02_language_guide/05_Control_Flow.md#early-exit) 章节和 [语句](../03-language-reference/05-Statements.md) 篇章的 [Guard 语句](../03-language-reference/05-Statements.md#guard-statement) 章节新增了与 `guard` 语句相关的内容。
|
||||
* 在 [协议](../02_language_guide/21_Protocols.md) 篇章中 [协议扩展](../02_language_guide/21_Protocols.md#protocol-extensions) 章节中新增了有关协议扩展的内容。
|
||||
* 在 [访问控制](../02_language_guide/26_Access_Control.md) 篇章的 [单元测试 target 的访问级别](../02_language_guide/26_Access_Control.md#access-levels-for-unit-test-targets) 章节中新增了有关单元测试访问控制相关的内容。
|
||||
* 在 [模式](../03_language_reference/08_Patterns.md) 篇章的 [可选模式](../03_language_reference/08_Patterns.md#optional-pattern) 章节中新增了可选模式相关内容。
|
||||
* 更新 [Repeat-While](../02_language_guide/05_Control_Flow.md#repeat-while) 章节中有关 `repeat-while` 循环相关的内容。
|
||||
* 更新 [字符串和字符](../02_language_guide/03_Strings_And_Characters.md) 章节,现在 `String` 类型在 Swift 标准库中不再遵循 `CollectionType` 协议。
|
||||
* 在 [常量与变量打印](../02_language_guide/01_TheBasics.md#printing) 章节中新增了新 Swift 标准库中有关 `print(_:separator:terminator) ` 相关内容。
|
||||
* 在 [枚举](../02_language_guide/08_Enumerations.md) 篇章的 [原始值的隐式赋值](../02_language_guide/08_Enumerations.md#implicitly-assigned-raw-values) 章节和 [声明](../03_language_reference/06_Declarations.md) 篇章的 [包含原始值类型的枚举](../03_language_reference/06_Declarations.md#enumerations-with-cases-of-a-raw-value-type) 章节中新增了有关包含 `String` 原始值的枚举用例的行为相关内容。
|
||||
* 在 [常量与变量打印](../02_language_guide/01_TheBasics.md#printing) 章节中新增了新 Swift 标准库中有关 `print(-:separator:terminator) ` 相关内容。
|
||||
* 在 [枚举](../02_language_guide/08_Enumerations.md) 篇章的 [原始值的隐式赋值](../02_language_guide/08_Enumerations.md#implicitly-assigned-raw-values) 章节和 [声明](../03-language-reference/06-Declarations.md) 篇章的 [包含原始值类型的枚举](../03-language-reference/06-Declarations.md#enumerations-with-cases-of-a-raw-value-type) 章节中新增了有关包含 `String` 原始值的枚举用例的行为相关内容。
|
||||
* 在 [自动闭包](../02_language_guide/07_Closures.md#autoclosures) 章节中新增了有关 `@autoclosure` 特性的相关内容,包括它的 `@autoclosure(escaping)` 形式。
|
||||
* 更新 [声明特性](../03_language_reference/07_Attributes.md#Ideclaration-attributes) 章节中有关 `@avaliable` 和 `warn_unused_result` 特性的相关内容。
|
||||
* 更新 [声明特性](../03_language_reference/07_Attributes.md#Ideclaration-attributes) 章节中有关 `@avaliable` 和 `warn-unused-result` 特性的相关内容。
|
||||
* 更新 [类型特性](../03_language_reference/07_Attributes.md#type-attributes) 章节中有关 `@convention` 特性的相关内容。
|
||||
* 在 [可选绑定](../02_language_guide/01_TheBasics.md#optional-binding) 章节中新增了有关使用 `where` 子句进行多可选绑定的相关内容。
|
||||
* 在 [字符串字面量](../03_language_reference/02_Lexical_Structure.md#string-literal) 章节中新增了有关在编译时使用 `+` 运算符拼接字符串字面量的相关内容。
|
||||
@ -171,11 +171,11 @@
|
||||
* 在 [元类型](../03_language_reference/03_Types.md#metatype-type-h) 章节新增了一处说明,有关如何从元类型值中构造类实例相关内容。
|
||||
* 在 [弱引用](../02_language_guide/24_Automatic_Reference_Counting.md#weak-references) 章节新增了一处说明,有关弱引用作为缓存所存在的不足。
|
||||
* 更新 [类型特性](../02_language_guide/10_Properties.md#type-properties) 章节,提到了存储型特性其实是懒加载。
|
||||
* 更新 [捕获类型](../02_language_guide/07_Closures.md#capturing_values) 章节,阐明了变量和常量在闭包中如何被捕获。
|
||||
* 更新 [捕获类型](../02_language_guide/07_Closures.md#capturing-values) 章节,阐明了变量和常量在闭包中如何被捕获。
|
||||
* 更新 [声明特性](../03_language_reference/07_Attributes.md#Ideclaration-attributes) 章节,用以描述何时在类中使用 `@objc` 关键字。
|
||||
* 在 [错误处理](../02_language_guide/17_Error_Handling.md#handling-errors) 章节中新增了一处说明,有关执行 `throw` 语句的性能。在 [Do 语句](../03_language_reference/05_Statements.md#do-statement) 章节的 do 语句部分也新增了类似内容。
|
||||
* 在 [错误处理](../02_language_guide/17_Error_Handling.md#handling-errors) 章节中新增了一处说明,有关执行 `throw` 语句的性能。在 [Do 语句](../03-language-reference/05-Statements.md#do-statement) 章节的 do 语句部分也新增了类似内容。
|
||||
* 更新 [类型特性](../02_language_guide/10_Properties.md#type-properties) 章节中有关类、结构体和枚举的存储型和计算型特性相关的内容。
|
||||
* 更新 [Break 语句](../03_language_reference/05_Statements.md#break_statement) 章节中有关带标签的 break 语句相关内容。
|
||||
* 更新 [Break 语句](../03_language_reference/05_Statements.md#break-statement) 章节中有关带标签的 break 语句相关内容。
|
||||
* 在 [属性观察器](../02_language_guide/10_Properties.md#property-observers) 章节更新了一处说明,用来明确 `willSet` 和 `didSet` 观察器的行为。
|
||||
* 在 [访问级别](../02_language_guide/26_Access_Control.md#access-levels) 章节新增了有关 `private` 作用域的相关内容说明。
|
||||
* 在 [弱引用](../02_language_guide/24_Automatic_Reference_Counting.md#weak-references) 章节新增了有关弱应用在垃圾回收系统和 ARC 之间的区别的说明。
|
||||
@ -212,9 +212,9 @@
|
||||
* 新增 [失败构造器](../02_language_guide/14_Initialization.md#failable-initializers) 的完整指引。
|
||||
* 在协议中新增了 [失败构造器要求](../02_language_guide/21_Protocols.md#failable-initializer-requirements) 的描述。
|
||||
* 常量和变量的 `Any` 类型现可以包含函数实例。更新了有关 `Any` 相关的示例来展示如何在 `switch` 语句中如何检查并转换到一个函数类型。
|
||||
* 带有原始值的枚举类型增加了一个 `rawValue` 属性替代 `toRaw()` 方法,同时使用了一个以 `rawValue` 为参数的失败构造器来替代 `fromRaw()` 方法。更多的内容,请看 [原始值](../02_language_guide/08_Enumerations.md#raw-values) 和 [带原始值的枚举类型](../03_language_reference/06_Declarations.md#enumerations-with-cases-of-a-raw-value-type)。
|
||||
* 带有原始值的枚举类型增加了一个 `rawValue` 属性替代 `toRaw()` 方法,同时使用了一个以 `rawValue` 为参数的失败构造器来替代 `fromRaw()` 方法。更多的内容,请看 [原始值](../02_language_guide/08_Enumerations.md#raw-values) 和 [带原始值的枚举类型](../03-language-reference/06-Declarations.md#enumerations-with-cases-of-a-raw-value-type)。
|
||||
* 新增 [Failable Initializer](../03_language_reference/06_Declarations.md#failable-initializers) 的参考章节,它可以触发初始化失败。
|
||||
* 自定义运算符现在可以包含 `?` 字符,更新了 [运算符](../03_language_reference/02_Lexical_Structure.md#operator) 涉及改进后的规则的部分,并且在 [自定义运算符](../02_language_guide/27_Advanced_Operators.md#custom-operators) 章节中删除了重复的运算符有效字符集合。
|
||||
* 自定义运算符现在可以包含 `?` 字符,更新了 [运算符](../03_language_reference/02_Lexical_Structure.md#operator) 涉及改进后的规则的部分,并且在 [自定义运算符](../02-language-guide/27-Advanced-Operators.md#custom-operators) 章节中删除了重复的运算符有效字符集合。
|
||||
|
||||
### 2014-08-18
|
||||
|
||||
@ -227,11 +227,11 @@
|
||||
* [可选类型](../02_language_guide/01_TheBasics.md#optionals) 若有值时,不再隐式的转换为 `true`,同样,若无值时,也不再隐式的转换为 `false`,这是为了避免在判别 optional `Bool` 的值时产生困惑。 替代的方案是,用`==` 或 `!=` 运算符显式地去判断 Optinal 是否是 `nil`,以确认其是否包含值。
|
||||
* Swift 新增了一个 [Nil 合并运算符](../02_language_guide/BasicOperators.md#nil-coalescing-operator) (`a ?? b`) , 该表达式中,如果 Optional `a` 的值存在,则取得它并返回,若 Optional `a` 为 `nil`,则返回默认值 `b`
|
||||
* 更新和扩展 [字符串的比较](../02_language_guide/03_Strings_And_Characters.md#comparing-strings) ,用以反映和展示'字符串和字符的比较',以及'前缀(prefix)/后缀(postfix)比较'都开始基于扩展字符集(extended grapheme clusters)规范的等价比较。
|
||||
* 现在,你可以通过下标赋值或者 [可选调用链](../02_language_guide/16_Optional_Chaining.md) 中的可变方法和操作符来给属性设值。相应地更新了有关 [通过可选链接访问属性](../02_language_guide/16_Optional_Chaining.md#accessing-properties-through-optional-chaining) 的内容,并扩展了 [通过可选链接调用方法](../02_language_guide/16_Optional_Chaining.md#calling-methods-through-optional-chaining) 时检查方法调用成功的示例,以显示如何检查属性设置是否成功。
|
||||
* 现在,你可以通过下标赋值或者 [可选调用链](../02_language_guide/16_Optional_Chaining.md) 中的可变方法和操作符来给属性设值。相应地更新了有关 [通过可选链接访问属性](../02_language_guide/16_Optional_Chaining.md#accessing-properties-through-optional-chaining) 的内容,并扩展了 [通过可选链接调用方法](../02-language-guide/16-Optional-Chaining.md#calling-methods-through-optional-chaining) 时检查方法调用成功的示例,以显示如何检查属性设置是否成功。
|
||||
* 在可选链中新增了 [访问可选类型的下标脚注](../02_language_guide/16_Optional_Chaining.md#accessing-subscripts-through-optional-chaining) 章节。
|
||||
* 更新 [访问和修改数组](../02_language_guide/CollectionTypes.md#accessing-and-modifying-a-dictionary) 章节以标示,从该版本起,不能再通过 `+=` 运算符给一个数组新增一个新的项。对应的替代方案是,使 `append` 方法,或者通过 `+=` 运算符来新增一个只有一个项的数组。
|
||||
* 新增一处说明,在 [范围运算符](../02_language_guide/BasicOperators.md#range-operators) 中,比如, `a..b` 和 `a..<b` ,起始值 `a` 不能大于结束值 `b`。
|
||||
* 重写 [继承](../02_language_guide/13_Inheritance.md) 篇章:删除了本章中有关构造器重写的介绍性报道;转而将更多的注意力放到新增的部分——子类的新功能,以及如何通过重写(overrides)修改已有的功能。另外, [重写属性的 Getters 和 Setters](../02_language_guide/13_Inheritance.md#overriding-property-etters-and-setters) 中的例子已经被替换为展示如何重写一个 `description` 属性。 (而有关如何在子类的构造器中修改继承属性的默认值的例子,已经被移到 [构造过程](../02_language_guide/14_Initialization.md) 篇章。)
|
||||
* 重写 [继承](../02_language_guide/13_Inheritance.md) 篇章:删除了本章中有关构造器重写的介绍性报道;转而将更多的注意力放到新增的部分——子类的新功能,以及如何通过重写(overrides)修改已有的功能。另外, [重写属性的 Getters 和 Setters](../02_language_guide/13_Inheritance.md#overriding-property-etters-and-setters) 中的例子已经被替换为展示如何重写一个 `description` 属性。 (而有关如何在子类的构造器中修改继承属性的默认值的例子,已经被移到 [构造过程](../02-language-guide/14-Initialization.md) 篇章。)
|
||||
* 更新 [构造器的继承与重写](../02_language_guide/14_Initialization.md#initializer-inheritance-and-overriding) 章节以标示: 重写一个特定的构造器必须使用 `override` 修饰符。
|
||||
* 更新 [Required 构造器](../02_language_guide/14_Initialization.md#required-initializers) 章节以标示:`required` 修饰符现在需要出现在所有子类的 required 构造器的声明中,而 required 构造器的实现,现在可以仅从父类自动继承。
|
||||
* 中置(Infix)的 [运算符函数](../02_language_guide/27_Advanced_Operators.md#operator-functions) 不再需要 `@infix` 属性。
|
||||
@ -243,7 +243,7 @@
|
||||
* 新增有关 [字面量](../03_language_reference/02_Lexical_Structure.md#literal) 类型推导内容的内容。
|
||||
* 新增更多有关柯里化函数的内容。
|
||||
* 新增 [权限控制](../02_language_guide/26_Access_Control.md) 篇章。
|
||||
* 更新 [字符串和字符](../02_language_guide/03_Strings_And_Characters.md) 章节,在 Swift 中现在 `Character` 类型代表的是扩展字符集(extended grapheme cluster)中的一个 Unicode,为此,新增了 [Extended Grapheme Clusters](../02_language_guide/03_Strings_And_Characters.md#extended-grapheme-clusters) 章节。同时,[Unicode 标量](../02_language_guide/03_Strings_And_Characters.md#unicode-scalars-representation) 和 [字符串比较](../02_language_guide/03_Strings_And_Characters.md#comparing-strings) 章节新增了更多内容。
|
||||
* 更新 [字符串和字符](../02_language_guide/03_Strings_And_Characters.md) 章节,在 Swift 中现在 `Character` 类型代表的是扩展字符集(extended grapheme cluster)中的一个 Unicode,为此,新增了 [Extended Grapheme Clusters](../02_language_guide/03_Strings_And_Characters.md#extended-grapheme-clusters) 章节。同时,[Unicode 标量](../02-language-guide/03-Strings-And-Characters.md#unicode-scalars-representation) 和 [字符串比较](../02-language-guide/03-Strings-And-Characters.md#comparing-strings) 章节新增了更多内容。
|
||||
* 更新 [字符串字面量](../02_language_guide/03_Strings_And_Characters.md#string-literals) 章节,在一个字符串中,Unicode 标量(Unicode scalars)以 `\u{n}`的形式来表示,`n` 是一个最大可以有8位的16进制数。
|
||||
* `NSString` `length` 属性已被映射到 Swift 的内建 `String`类型。(注意,这两属性的类型是`utf16Count`,而非 `utf16count`)。
|
||||
* Swift 的内建 `String` 类型不再拥有 `uppercaseString` 和 `lowercaseString` 属性。在 [字符串和字符](../02_language_guide/03_Strings_And_Characters.md) 章节中删除了对应部分,并更新了各种对应的代码用例。
|
||||
@ -256,7 +256,7 @@
|
||||
* 更新 [读取和修改字典](../02_language_guide/CollectionTypes.md#accessing-and-modifying-a-dictionary) 章节, `Dictionary` 现在增加了一个 Boolean 型的属性:`isEmpty`。
|
||||
* 解释了哪些字符(集)可被用来定义 [自定义操作符](../02_language_guide/27_Advanced_Operators.md#custom-operators)。
|
||||
* `nil` 和布尔运算中的 `true` 和 `false` 现在被定义为 [字面量](../03_language_reference/02_Lexical_Structure.md#literal)。
|
||||
* Swift 中的数组 (`Array`) 类型从现在起具备了完整的值语义。具体内容被更新到 [集合的可变性](../02_language_guide/CollectionTypes.md#mutability-of-collections) 和 [数组](../02_language_guide/CollectionTypes.md#arrays) 两小节,以反映这个新的变化。 此外,还解释了如何给 Strings, Arrays 和 Dictionaries 进行赋值和拷贝。
|
||||
* Swift 中的数组 (`Array`) 类型从现在起具备了完整的值语义。具体内容被更新到 [集合的可变性](../02_language_guide/CollectionTypes.md#mutability-of-collections) 和 [数组](../02-language-guide/CollectionTypes.md#arrays) 两小节,以反映这个新的变化。 此外,还解释了如何给 Strings, Arrays 和 Dictionaries 进行赋值和拷贝。
|
||||
* [数组类型速记语法](../02_language_guide/CollectionTypes.md#array-type-shorthand-syntax) 从 `SomeType []` 更新为 ` [SomeType]`。
|
||||
* 新增 [字典类型的速记语法](../02_language_guide/CollectionTypes.md#dictionary-type-shorthand-syntax) 章节,现在书写格式为: ` [KeyType: ValueType]`。
|
||||
* 新增 [字典键类型的哈希值](../02_language_guide/CollectionTypes.md#hash-values-for-set-types) 章节。
|
||||
|
||||
Reference in New Issue
Block a user