Merge branch 'gh-pages' of github.com:SwiftGGTeam/the-swift-programming-language-in-chinese into gh-pages
This commit is contained in:
@ -2,10 +2,10 @@
|
||||
|
||||
本书描述的是在 Xcode 11 中的默认 Swift 版本 Swift 5.1。你可以使用 Xcode11 来构建 Swift 5.1、Swift 4.2 或 Swift 4 写的项目。
|
||||
|
||||
当您使用 Xcode 11 构建 Swift 4 和 Swift 4.2 代码时,除了下面的功能仅支持 Swift 5.1,其他大多数功能都依然可用。
|
||||
当您使用 Xcode 11 构建 Swift 4 和 Swift 4.2 代码时,Swift 5.1 的大多数功能都适用。但以下功能仅支持 Swift 5.1 或更高版本:
|
||||
|
||||
* 返回值是不透明类型的函数依赖 Swift 5.1 运行时。
|
||||
* **try?** 表达式不会为已返回可选类型的代码引入额外的可选类型层级。
|
||||
* 大数字的整型字面量初始化代码的类型将会被正确推导,例如 **UInt64(0xffff_ffff_ffff_ffff)** 将会被推导为整型类型而非溢出。
|
||||
|
||||
用 Swift 5.1 写的项目可以依赖用 Swift 4.2 或 Swift 4 写的项目,反之亦然。这意味着,如果你将一个大的项目分解成多个框架(framework),你可以每次一个框架地迁移 Swift 4 代码到 Swift 5.1。
|
||||
用 Swift 5.1 写的项目可以依赖用 Swift 4.2 或 Swift 4 写的项目,反之亦然。这意味着,如果你将一个大的项目分解成多个框架(framework),你可以逐个地将框架从 Swift 4 代码迁移到 Swift 5.1。
|
||||
|
||||
@ -65,7 +65,7 @@ let fruitSummary = "I have \(apples + oranges) pieces of fruit."
|
||||
>
|
||||
> 使用 `\()` 来把一个浮点计算转换成字符串,并加上某人的名字,和他打个招呼。
|
||||
|
||||
使用三个双引号(`"""`)来包含多行字符串内容,字符串中的内容(包括引号、空格、换行符等)都会保留下来。举个例子:
|
||||
使用三个双引号(`"""`)来包含多行字符串内容。每行行首的缩进会被去除,直到和结尾引号的缩进相匹配。举个例子:
|
||||
|
||||
```swift
|
||||
let quotation = """
|
||||
@ -87,6 +87,13 @@ var occupations = [
|
||||
occupations["Jayne"] = "Public Relations"
|
||||
```
|
||||
|
||||
数组在添加元素时会自动变大。
|
||||
|
||||
```swift
|
||||
shoppingList.append("blue paint")
|
||||
print(shoppingList)
|
||||
```
|
||||
|
||||
使用初始化语法来创建一个空数组或者空字典。
|
||||
|
||||
```swift
|
||||
@ -94,7 +101,7 @@ let emptyArray = [String]()
|
||||
let emptyDictionary = [String: Float]()
|
||||
```
|
||||
|
||||
如果类型信息可以被推断出来,你可以用 `[]` 和 `[:]` 来创建空数组和空字典——就像你声明变量或者给函数传参数的时候一样。
|
||||
如果类型信息可以被推断出来,你可以用 `[]` 和 `[:]` 来创建空数组和空字典——比如,在给变量赋新值或者给函数传参数的时候。
|
||||
|
||||
```swift
|
||||
shoppingList = []
|
||||
@ -138,6 +145,7 @@ if let name = optionalName {
|
||||
> 把 `optionalName` 改成 `nil`,greeting 会是什么?添加一个 `else` 语句,当 `optionalName` 是 `nil` 时给 greeting 赋一个不同的值。
|
||||
|
||||
如果变量的可选值是 `nil`,条件会判断为 `false`,大括号中的代码会被跳过。如果不是 `nil`,会将值解包并赋给 `let` 后面的常量,这样代码块中就可以使用这个值了。
|
||||
|
||||
另一种处理可选值的方法是通过使用 `??` 操作符来提供一个默认值。如果可选值缺失的话,可以使用默认值来代替。
|
||||
|
||||
```swift
|
||||
@ -234,7 +242,7 @@ greet(person:"Bob", day: "Tuesday")
|
||||
|
||||
> 练习
|
||||
>
|
||||
> 删除 `day` 参数,添加一个参数来表示今天吃了什么午饭。
|
||||
> 删除 `day` 参数,在这个欢迎语中添加一个参数来表示今天的特价菜。
|
||||
|
||||
默认情况下,函数使用它们的参数名称作为它们参数的标签,在参数名称前可以自定义参数标签,或者使用 `_` 表示不使用参数标签。
|
||||
|
||||
@ -314,7 +322,7 @@ var numbers = [20, 19, 7, 12]
|
||||
hasAnyMatches(list: numbers, condition: lessThanTen)
|
||||
```
|
||||
|
||||
函数实际上是一种特殊的闭包:它是一段能之后被调取的代码。闭包中的代码能访问闭包作用域中的变量和函数,即使闭包是在一个不同的作用域被执行的 - 你已经在嵌套函数的例子中看过了。你可以使用 `{}` 来创建一个匿名闭包。使用 `in` 将参数和返回值类型的声明与闭包函数体进行分离。
|
||||
函数实际上是一种特殊的闭包:它是一段能之后被调取的代码。闭包中的代码能访问闭包作用域中的变量和函数,即使闭包是在一个不同的作用域被执行的——你已经在嵌套函数的例子中看过了。你可以使用 `{}` 来创建一个匿名闭包。使用 `in` 将参数和返回值类型的声明与闭包函数体进行分离。
|
||||
|
||||
```swift
|
||||
numbers.map({
|
||||
@ -335,7 +343,7 @@ let mappedNumbers = numbers.map({ number in 3 * number })
|
||||
print(mappedNumbers)
|
||||
```
|
||||
|
||||
你可以通过参数位置而不是参数名字来引用参数——这个方法在非常短的闭包中非常有用。当一个闭包作为最后一个参数传给一个函数的时候,它可以直接跟在括号后面。当一个闭包是传给函数的唯一参数,你可以完全忽略括号。
|
||||
你可以通过参数位置而不是参数名字来引用参数——这个方法在非常短的闭包中非常有用。当一个闭包作为最后一个参数传给一个函数的时候,它可以直接跟在圆括号后面。当一个闭包是传给函数的唯一参数,你可以完全忽略圆括号。
|
||||
|
||||
```swift
|
||||
let sortedNumbers = numbers.sorted { $0 > $1 }
|
||||
@ -419,7 +427,7 @@ test.simpleDescription()
|
||||
>
|
||||
> 创建 `NamedShape` 的另一个子类 `Circle`,构造器接收两个参数,一个是半径一个是名称,在子类 `Circle` 中实现 `area()` 和 `simpleDescription()` 方法。
|
||||
|
||||
除了储存简单的属性之外,属性可以有 getter 和 setter 。
|
||||
除了简单的存储属性,还有使用 getter 和 setter 的计算属性。
|
||||
|
||||
```swift
|
||||
class EquilateralTriangle: NamedShape {
|
||||
@ -450,7 +458,7 @@ triangle.perimeter = 9.9
|
||||
print(triangle.sideLength)
|
||||
```
|
||||
|
||||
在 `perimeter` 的 setter 中,新值的名字是 `newValue`。你可以在 `set` 之后显式的设置一个名字。
|
||||
在 `perimeter` 的 setter 中,新值的名字是 `newValue`。你可以在 `set` 之后的圆括号中显式地设置一个名字。
|
||||
|
||||
注意 `EquilateralTriangle` 类的构造器执行了三步:
|
||||
|
||||
@ -484,7 +492,7 @@ triangleAndSquare.square = Square(sideLength: 50, name: "larger square")
|
||||
print(triangleAndSquare.triangle.sideLength)
|
||||
```
|
||||
|
||||
处理变量的可选值时,你可以在操作(比如方法、属性和子脚本)之前加 `?`。如果 `?` 之前的值是 `nil`,`?` 后面的东西都会被忽略,并且整个表达式返回 `nil`。否则,`?` 之后的东西都会被运行。在这两种情况下,整个表达式的值也是一个可选值。
|
||||
处理变量的可选值时,你可以在操作(比如方法、属性和子脚本)之前加 `?`。如果 `?` 之前的值是 `nil`,`?` 后面的东西都会被忽略,并且整个表达式返回 `nil`。否则,可选值会被解包,之后的所有代码都会按照解包后的值运行。在这两种情况下,整个表达式的值也是一个可选值。
|
||||
|
||||
```swift
|
||||
let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square")
|
||||
@ -525,7 +533,7 @@ let aceRawValue = ace.rawValue
|
||||
|
||||
默认情况下,Swift 按照从 0 开始每次加 1 的方式为原始值进行赋值,不过你可以通过显式赋值进行改变。在上面的例子中,`Ace` 被显式赋值为 1,并且剩下的原始值会按照顺序赋值。你也可以使用字符串或者浮点数作为枚举的原始值。使用 `rawValue` 属性来访问一个枚举成员的原始值。
|
||||
|
||||
使用 `init?(rawValue:)` 初始化构造器来创建一个带有原始值的枚举成员。如果存在与原始值相应的枚举成员就返回该枚举成员,否则就返回 `nil`。
|
||||
使用 `init?(rawValue:)` 初始化构造器来从原始值创建一个枚举实例。如果存在与原始值相应的枚举成员就返回该枚举成员,否则就返回 `nil`。
|
||||
|
||||
```swift
|
||||
if let convertedRank = Rank(rawValue: 3) {
|
||||
@ -559,9 +567,9 @@ let heartsDescription = hearts.simpleDescription()
|
||||
>
|
||||
> 给 `Suit` 添加一个 `color()` 方法,对 `spades` 和 `clubs` 返回 “black” ,对 `hearts` 和 `diamonds` 返回 “red” 。
|
||||
|
||||
注意在上面的例子中用了两种方式引用 `hearts` 枚举成员:给 `hearts` 常量赋值时,枚举成员 `Suit.hearts` 需要用全名来引用,因为常量没有显式指定类型。在 `switch` 里,枚举成员使用缩写 `.hearts` 来引用,因为 `self` 的值已经是一个 `suit` 类型,在已知变量类型的情况下可以使用缩写。
|
||||
注意在上面的例子中用了两种方式引用 `hearts` 枚举成员:给 `hearts` 常量赋值时,枚举成员 `Suit.hearts` 需要用全名来引用,因为常量没有显式指定类型。在 `switch` 里,枚举成员使用缩写 `.hearts` 来引用,因为 `self` 的值已经是一个 `suit` 类型。在任何已知变量类型的情况下都可以使用缩写。
|
||||
|
||||
如果枚举成员的实例有原始值,那么这些值是在声明的时候就已经决定了,这意味着不同枚举实例的枚举成员总会有一个相同的原始值。当然我们也可以为枚举成员设定关联值,关联值是在创建实例时决定的。这意味着不同的枚举成员的关联值都可以不同。你可以把关联值想象成枚举成员的寄存属性。例如,考虑从服务器获取日出和日落的时间的情况。服务器会返回正常结果或者错误信息。
|
||||
如果枚举成员的实例有原始值,那么这些值是在声明的时候就已经决定了,这意味着不同枚举实例的枚举成员总会有一个相同的原始值。当然我们也可以为枚举成员设定关联值,关联值是在创建实例时决定的。这意味着同一枚举成员不同实例的关联值可以不相同。你可以把关联值想象成枚举成员实例的存储属性。例如,考虑从服务器获取日出和日落的时间的情况。服务器会返回正常结果或者错误信息。
|
||||
|
||||
```swift
|
||||
enum ServerResponse {
|
||||
@ -584,7 +592,7 @@ case let .failure(message):
|
||||
>
|
||||
> 给 `ServerResponse` 和 switch 添加第三种情况。
|
||||
|
||||
注意日升和日落时间是如何从 `ServerResponse` 中提取到并与 `switch` 的 `case` 相匹配的。
|
||||
注意 `ServerResponse` 的值在与 `switch` 的分支匹配时,日升和日落时间是如何从该值中提取出来的。
|
||||
|
||||
使用 `struct` 来创建一个结构体。结构体和类有很多相同的地方,包括方法和构造器。它们之间最大的一个区别就是结构体是传值,类是传引用。
|
||||
|
||||
@ -602,7 +610,7 @@ let threeOfSpadesDescription = threeOfSpades.simpleDescription()
|
||||
|
||||
> 练习
|
||||
>
|
||||
> 给 `Card` 添加一个方法,创建一副完整的扑克牌并把每张牌的 rank 和 suit 对应起来。
|
||||
> 写一个方法,创建一副完整的扑克牌,这些牌是所有 rank 和 suit 的组合。
|
||||
|
||||
## 协议和扩展 {#protocols-and-extensions}
|
||||
|
||||
@ -642,7 +650,7 @@ let bDescription = b.simpleDescription
|
||||
|
||||
> 练习
|
||||
>
|
||||
> 写一个实现这个协议的枚举。
|
||||
> 给 `ExampleProtocol` 再增加一个要求。你需要怎么改 `SimpleClass` 和 `SimpleStructure` 才能保证它们仍旧遵循这个协议?
|
||||
|
||||
注意声明 `SimpleStructure` 时候 `mutating` 关键字用来标记一个会修改结构体的方法。`SimpleClass` 的声明不需要标记任何方法,因为类中的方法通常可以修改类属性(类的性质)。
|
||||
|
||||
|
||||
@ -273,7 +273,7 @@ let possiblePlanet = Planet(rawValue: 7)
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 原始值构造器是一个可失败构造器,因为并不是每一个原始值都有与之对应的枚举成员。更多信息请参见 [可失败构造器](../03_language_reference/05_Declarations.html#failable-initializers)。
|
||||
> 原始值构造器是一个可失败构造器,因为并不是每一个原始值都有与之对应的枚举成员。更多信息请参见 [可失败构造器](../03_language_reference/06_Declarations.md#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}
|
||||
|
||||
|
||||
@ -184,7 +184,7 @@ print("Car: \(car.description)")
|
||||
|
||||
#### 重写属性观察器 {#overriding-property-observers}
|
||||
|
||||
你可以通过重写属性为一个继承来的属性添加属性观察器。这样一来,无论被继承属性原本是如何实现的,当其属性值发生改变时,你就会被通知到。关于属性观察器的更多内容,请看 [属性观察器](../02_language_guide/10_Properties.html#property-observers)。
|
||||
你可以通过重写属性为一个继承来的属性添加属性观察器。这样一来,无论被继承属性原本是如何实现的,当其属性值发生改变时,你就会被通知到。关于属性观察器的更多内容,请看 [属性观察器](../02_language_guide/10_Properties.md#property-observers)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
|
||||
@ -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) }
|
||||
>
|
||||
|
||||
|
||||
@ -2,25 +2,27 @@
|
||||
|
||||
Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言中有效符号(token)的字符序列。这些合法符号组成了语言中最底层的构建基块,并在之后的章节中用于描述语言的其他部分。一个合法符号由一个标识符(identifier)、关键字(keyword)、标点符号(punctuation)、字面量(literal)或运算符(operator)组成。
|
||||
|
||||
通常情况下,通过考虑输入文本当中可能的最长子串,并且在随后将介绍的语法约束之下,根据随后将介绍的语法约束生成的,根据 Swift 源文件当中的字符来生成相应的“符号”。这种方法称为*“最长匹配(longest match)”*,或者*“最大适合(maximal munch)”*。
|
||||
通常情况下,符号是考虑了输入文本中最长可能的子字符串,并被随后将介绍的语法约束,根据 Swift 源文件的字符生成的。这种方法称为*“最长匹配(longest match)”*,或者*“最大适合(maximal munch)”*。
|
||||
|
||||
## 空白与注释 {#whitespace}
|
||||
|
||||
空白(whitespace)有两个用途:分隔源文件中的符号以及帮助区分运算符属于前缀还是后缀(参见 [运算符](#operators)),在其他情况下空白则会被忽略。以下的字符会被当作空白:空格(U+0020)、换行符(U+000A)、回车符(U+000D)、水平制表符(U+0009)、垂直制表符(U+000B)、换页符(U+000C)以及空字符(U+0000)。
|
||||
|
||||
注释被编译器当作空白处理。单行注释由 `//` 开始直至遇到换行符(U+000A)或者回车符(U+000D)。多行注释由 `/*` 开始,以 `*/` 结束。注释允许嵌套,但注释标记必须匹配。
|
||||
注释被编译器当作空白处理。单行注释由 `//` 开始直至遇到换行符(U+000A)或者回车符(U+000D)。多行注释由 `/*` 开始,以 `*/` 结束。多行注释允许嵌套,但注释标记必须成对出现。
|
||||
|
||||
注释可以包含其他的格式和标记,如 [标记格式参考](https://developer.apple.com/library/archive/documentation/Xcode/Reference/xcode_markup_formatting_ref/index.html)中所述。
|
||||
|
||||
> 空白语法
|
||||
>
|
||||
> *空白* → [*空白项*](#whitespace-item) [*空白*](#whitespace)<sub>可选</sub>
|
||||
> *空白* → [空白项](#whitespace-item) [空白](#whitespace)<sub>可选</sub>
|
||||
>
|
||||
#### whitespace-item {#whitespace-item}
|
||||
>
|
||||
> *空白项* → [*断行符*](#line-break)
|
||||
> *空白项* → [断行符](#line-break)
|
||||
>
|
||||
> *空白项* → [*注释*](#comment)
|
||||
> *空白项* → [注释](#comment)
|
||||
>
|
||||
> *空白项* → [*多行注释*](#multiline-comment)
|
||||
> *空白项* → [多行注释](#multiline-comment)
|
||||
>
|
||||
> *空白项* → U+0000,U+0009,U+000B,U+000C 或者 U+0020
|
||||
>
|
||||
@ -36,58 +38,60 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
>
|
||||
#### comment {#comment}
|
||||
>
|
||||
> *注释* → // [*注释内容*](#comment-text) [断行符*](#line-break)
|
||||
> *注释* → // [注释内容](#comment-text) [断行符](#line-break)
|
||||
>
|
||||
>
|
||||
#### multiline-comment {#multiline-comment}
|
||||
>
|
||||
> *多行注释* → `/*` [*多行注释内容*](#multiline-commnet-text) `*/`
|
||||
> *多行注释* → `/*` [多行注释内容](#multiline-commnet-text) `*/`
|
||||
>
|
||||
>
|
||||
#### comment-text {#comment-text}
|
||||
>
|
||||
> *注释内容* → [*注释内容项*](#comment-text-item) [*注释内容*](#comment-text)<sub>可选</sub>
|
||||
> *注释内容* → [注释内容项](#comment-text-item) [注释内容](#comment-text)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### comment-text-item {#comment-text-item}
|
||||
>
|
||||
> *注释内容项* → 任何 Unicode 标量值, 除了 U+000A 或者 U+000D
|
||||
> *注释内容项* → 任何 Unicode 标量值,除了 U+000A 或者 U+000D
|
||||
>
|
||||
>
|
||||
#### multiline-commnet-text {#multiline-commnet-text}
|
||||
>
|
||||
> *多行注释内容* → [*多行注释内容项*](#multiline-comment-text-item) [*多行注释内容*](#multiline-comment-text)<sub>可选</sub>
|
||||
> *多行注释内容* → [多行注释内容项](#multiline-comment-text-item) [多行注释内容](#multiline-comment-text)<sub>可选</sub>
|
||||
>
|
||||
> *多行注释内容项* → [*多行注释*](#multiline-comment).
|
||||
> *多行注释内容项* → [多行注释](#multiline-comment).
|
||||
>
|
||||
> *多行注释内容项* → [*注释内容项*](#comment-text-item)
|
||||
> *多行注释内容项* → [注释内容项](#comment-text-item)
|
||||
>
|
||||
> *多行注释内容项* → 任何 Unicode 标量值, 除了 `/*` 或者 `*/`
|
||||
> *多行注释内容项* → 任何 Unicode 标量值,除了 `/*` 或者 `*/`
|
||||
|
||||
## 标识符 {#identifiers}
|
||||
|
||||
*标识符(identifier)* 可以由以下的字符开始:大写或小写的字母 `A` 到 `Z`、下划线(`_`)、基本多文种平面(Basic Multilingual Plane)中非字符数字组合的 Unicode 字符以及基本多文种平面以外的非个人专用区字符。在首字符之后,允许使用数字和组合 Unicode 字符。
|
||||
*标识符(identifier)* 可以由以下的字符开始:大写或小写的字母 `A` 到 `Z`、下划线(`_`)、基本多文种平面(Basic Multilingual Plane)中非字符数字组合的 Unicode 字符以及基本多文种平面以外的非个人专用区字符。在首字符之后,允许使用数字和组合 Unicode 字符。
|
||||
|
||||
使用保留字作为标识符,需要在其前后增加反引号(`` ` ``)。例如,`class` 不是合法的标识符,但可以使用 `` `class` ``。反引号不属于标识符的一部分,`` `x` `` 和 `x` 表示同一标识符。
|
||||
|
||||
闭包中如果没有明确指定参数名称,参数将被隐式命名为 `$0`、`$1`、`$2` 等等。这些命名在闭包作用域范围内是合法的标识符。
|
||||
|
||||
编译器给含有属性包装器呈现值的属性自动合成以美元符号(*$*)开头的标识符。你的代码可以与这些标识符进行交互,,但是不能使用该前缀声明标识符。更详细的介绍,请查看 [特性](./07_Attributes.md) 章节中的 [属性包装器](./07_Attributes.md#propertywrapper) 部分。
|
||||
|
||||
> 标识符语法
|
||||
>
|
||||
> *标识符* → [*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub>
|
||||
> *标识符* → [头部标识符](#identifier-head) [标识符字符组](#identifier-characters)<sub>可选</sub>
|
||||
>
|
||||
> *标识符* → \`[*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub>\`
|
||||
> *标识符* → \`[头部标识符](#identifier-head) [标识符字符组](#identifier-characters)<sub>可选</sub>\`
|
||||
>
|
||||
> *标识符* → [*隐式参数名*](#implicit-parameter-name)
|
||||
> *标识符* → [隐式参数名](#implicit-parameter-name)
|
||||
>
|
||||
> *标识符列表* → [*标识符*](#identifier) | [*标识符*](#identifier) **,** [*标识符列表*](#identifier)
|
||||
> *标识符列表* → [标识符](#identifier) | [标识符](#identifier) **,** [标识符列表](#identifier)
|
||||
>
|
||||
>
|
||||
#### identifier-head {#identifier-head}
|
||||
>
|
||||
> *头部标识符* → 大写或小写字母 A - Z
|
||||
>
|
||||
> *头部标识符* → _
|
||||
> *头部标识符* → **_**
|
||||
>
|
||||
> *头部标识符* → U+00A8,U+00AA,U+00AD,U+00AF,U+00B2–U+00B5,或者 U+00B7–U+00BA
|
||||
>
|
||||
@ -116,38 +120,42 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
> *头部标识符* → U+90000–U+9FFFD,U+A0000–U+AFFFD,U+B0000–U+BFFFD,或者 U+C0000–U+CFFFD
|
||||
>
|
||||
> *头部标识符* → U+D0000–U+DFFFD 或者 U+E0000–U+EFFFD
|
||||
>
|
||||
> *标识符字符* → 数值 0 - 9
|
||||
>
|
||||
>
|
||||
>
|
||||
#### identifier-character {#identifier-character}
|
||||
>
|
||||
> *标识符字符* → 数值 0 - 9
|
||||
>
|
||||
> *标识符字符* → U+0300–U+036F,U+1DC0–U+1DFF,U+20D0–U+20FF,或者 U+FE20–U+FE2F
|
||||
>
|
||||
> *标识符字符* → [*头部标识符*](#identifier-head)
|
||||
> *标识符字符* → [头部标识符](#identifier-head)
|
||||
>
|
||||
>
|
||||
#### identifier-characters {#identifier-characters}
|
||||
>
|
||||
> *标识符字符组* → [*标识符字符*](#identifier-character) [*标识符字符组*](#identifier-characters)<sub>可选</sub>
|
||||
> *标识符字符组* → [标识符字符](#identifier-character) [标识符字符组](#identifier-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### implicit-parameter-name {#implicit-parameter-name}
|
||||
>
|
||||
> *隐式参数名* → **$** [*十进制数字列表*](#decimal-digit)
|
||||
> *隐式参数名* → **$** [十进制数字列表](#decimal-digit)
|
||||
>
|
||||
#### property-wrapper-projection {#property-wrapper-projection}
|
||||
>
|
||||
> *属性包装器呈现值* → **$** [标识符字符组](#identifier-characters)
|
||||
>
|
||||
|
||||
## 关键字和标点符号 {#keywords-and-punctuation}
|
||||
|
||||
下面这些被保留的关键字不允许用作标识符,除非使用反引号转义,具体描述请参考 [标识符](#identifiers)。除了 `inout`、`var` 以及 `let` 之外的关键字可以用作某个函数声明或者函数调用当中的外部参数名,无需添加反引号转义。当一个成员与一个关键字具有相同的名称时,不需要使用反引号来转义对该成员的引用,除非在引用该成员和使用该关键字之间存在歧义 - 例如,`self`,`Type` 和 `Protocol` 在显式的成员表达式中具有特殊的含义,因此它们必须在该上下文中使用反引号进行转义。
|
||||
|
||||
* 用在声明中的关键字: `associatedtype`、`class`、`deinit`、`enum`、`extension`、`fileprivate `、`func`、`import`、`init`、`inout`、`internal`、`let`、`open`、`operator`、`private`、`protocol`、`public`、`static`、`struct`、`subscript`、`typealias` 以及 `var`。
|
||||
* 用在声明中的关键字:`associatedtype`、`class`、`deinit`、`enum`、`extension`、`fileprivate `、`func`、`import`、`init`、`inout`、`internal`、`let`、`open`、`operator`、`private`、`protocol`、`public`、`rethrows`、`static`、`struct`、`subscript`、`typealias` 以及 `var`。
|
||||
* 用在语句中的关键字:`break`、`case`、`continue`、`default`、`defer`、`do`、`else`、`fallthrough`、`for`、`guard`、`if`、`in`、`repeat`、`return`、`switch`、`where` 以及 `while`。
|
||||
* 用在表达式和类型中的关键字:`as`、`Any`、`catch`、`false`、`is`、`nil`、`rethrows`、`super`、`self`、`Self`、`throw`、`throws`、`true` 以及 `try `。
|
||||
* 用在表达式和类型中的关键字:`as`、`Any`、`catch`、`false`、`is`、`nil`、`super`、`self`、`Self`、`throw`、`throws`、`true` 以及 `try `。
|
||||
* 用在模式中的关键字:`_`。
|
||||
* 以井字号(`#`)开头的关键字:`#available`、`#colorLiteral`、`#column`、`#else`、`#elseif`、`#endif`、`#error`、`#file`、`#fileLiteral`、`#function`、`#if`、`#imageLiteral `、`#line`、`#selector`、`#sourceLocation`以及 `#warning`。
|
||||
* 特定上下文中被保留的关键字: `associativity`、`convenience`、`dynamic`、`didSet`、`final`、`get`、`infix`、`indirect`、`lazy`、`left`、`mutating`、`none`、`nonmutating`、`optional`、`override`、`postfix`、`precedence`、`prefix`、`Protocol`、`required`、`right`、`set`、`Type`、`unowned`、`weak` 以及 `willSet`。这些关键字在特定上下文之外可以被用做标识符。
|
||||
* 以井字号(`#`)开头的关键字:`#available`、`#colorLiteral`、`#column`、`#else`、`#elseif`、`#endif`、`#error`、`#file`、`#fileLiteral`、`#function`、`#if`、`#imageLiteral`、`#line`、`#selector`、`#sourceLocation`以及 `#warning`。
|
||||
* 特定上下文中被保留的关键字:`associativity`、`convenience`、`dynamic`、`didSet`、`final`、`get`、`infix`、`indirect`、`lazy`、`left`、`mutating`、`none`、`nonmutating`、`optional`、`override`、`postfix`、`precedence`、`prefix`、`Protocol`、`required`、`right`、`set`、`Type`、`unowned`、`weak` 以及 `willSet`。这些关键字在特定上下文之外可以被用做标识符。
|
||||
|
||||
以下符号被当作保留符号,不能用于自定义运算符: `(`、`)`、`{`、`}`、`[`、`]`、`.`、`,`、`:`、`;`、`=`、`@`、`#`、`&`(作为前缀运算符)、`->`、`` ` ``、`?`、`!`(作为后缀运算符)。
|
||||
以下符号被保留为标点符号,不能用于自定义运算符:`(`、`)`、`{`、`}`、`[`、`]`、`.`、`,`、`:`、`;`、`=`、`@`、`#`、`&`(作为前缀运算符)、`->`、`` ` ``、`?`、以及 `!`(作为后缀运算符)。
|
||||
|
||||
## 字面量 {#literal}
|
||||
|
||||
@ -162,15 +170,16 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
true // 布尔值字面量
|
||||
```
|
||||
|
||||
字面量本身并不包含类型信息。事实上,一个字面量会被解析为拥有无限的精度,然后 Swift 的类型推导会尝试去推导出这个字面量的类型。比如,在 `let x: Int8 = 42` 这个声明中,Swift 使用了显式类型注解(`: Int8`)来推导出 `42` 这个整数字面量的类型是 `Int8`。如果没有可用的类型信息, Swift 则会从标准库中定义的字面量类型中推导出一个默认的类型。整数字面量的默认类型是 `Int`,浮点数字面量的默认类型是 `Double`,字符串字面量的默认类型是 `String`,布尔值字面量的默认类型是 `Bool`。比如,在 `let str = "Hello, world"` 这个声明中,字符串 `"Hello, world"` 的默认推导类型就是 `String`。
|
||||
字面量本身并不包含类型信息。事实上,一个字面量会被解析为拥有无限的精度,然后 Swift 的类型推导会尝试去推导出这个字面量的类型。比如,在 `let x: Int8 = 42` 这个声明中,Swift 使用了显式类型注解(`: Int8`)来推导出 `42` 这个整数字面量的类型是 `Int8`。如果没有可用的类型信息,Swift 则会从标准库中定义的字面量类型中推导出一个默认的类型。整数字面量的默认类型是 `Int`,浮点数字面量的默认类型是 `Double`,字符串字面量的默认类型是 `String`,布尔值字面量的默认类型是 `Bool`。比如,在 `let str = "Hello, world"` 这个声明中,字符串 `"Hello, world"` 的默认推导类型就是 `String`。
|
||||
|
||||
当为一个字面量值指定了类型注解的时候,这个注解类型必须能通过这个字面量值实例化。也就是说,这个类型必须符合这些 Swift 标准库协议中的一个:整数字面量的 `ExpressibleByIntegerLiteral` 协议、浮点数字面量的 `ExpressibleByFloatLiteral` 协议、字符串字面量的 `ExpressibleByStringLiteral` 协议、布尔值字面量的 `ExpressibleByBooleanLiteral` 协议、只包含单个 Unicode 标量字符串文本的 `ExpressibleByUnicodeScalarLiteral` 协议以及只包含单个扩展字形簇(grapheme cluster)字符串文字的 `ExpressibleByExtendedGraphemeClusterLiteral` 协议。比如,`Int8` 符合 `ExpressibleByIntegerLiteral` 协议,因此它能在 `let x: Int8 = 42` 这个声明中作为整数字面量 `42` 的类型注解。
|
||||
|
||||
当为一个字面量值指定了类型注解的时候,这个标注的类型必须能通过这个字面量值实例化。也就是说,这个类型必须符合这些 Swift 标准库协议中的一个:整数字面量的 `IntegerLiteralConvertible` 协议、浮点数字面量的 `FloatingPointLiteralConvertible` 协议、字符串字面量的 `StringLiteralConvertible` 协议以及布尔值字面量的 `BooleanLiteralConvertible` 协议。比如,`Int8` 符合 `IntegerLiteralConvertible` 协议,因此它能在 `let x: Int8 = 42` 这个声明中作为整数字面量 `42` 的类型注解。
|
||||
|
||||
> 字面量语法
|
||||
>
|
||||
> *字面量* → [*数值字面量*](#integer-literal) | [*字符串字面量*](#string-literal) | [*布尔值字面量*](#integer-literal) | [*nil 字面量*](#integer-literal)
|
||||
> *字面量* → [数值字面量](#integer-literal) | [字符串字面量](#string-literal) | [布尔值字面量](#integer-literal) | [nil 字面量](#integer-literal)
|
||||
>
|
||||
> *数值字面量* → **-**<sub>可选</sub> [*整数字面量*](#integer-literal) | **-**<sub>可选</sub> [*浮点数字面量*](#floating-point-literal)
|
||||
> *数值字面量* → **-**<sub>可选</sub> [整数字面量](#integer-literal) | **-**<sub>可选</sub> [浮点数字面量](#floating-point-literal)
|
||||
>
|
||||
> *布尔值字面量* → **true** | **false**
|
||||
>
|
||||
@ -179,11 +188,11 @@ true // 布尔值字面量
|
||||
|
||||
### 整数字面量{#integer-literal}
|
||||
|
||||
*整数字面量(Integer Literals)* 表示未指定精度整数的值。整数字面量默认用十进制表示,可以加前缀来指定其他的进制。二进制字面量加 `0b`,八进制字面量加 `0o`,十六进制字面量加 `0x`。
|
||||
*整数字面量(Integer Literals)* 表示未指定精度的整数值。整数字面量默认用十进制表示;可以加前缀来指定其他的进制。二进制字面量加 `0b`,八进制字面量加 `0o`,十六进制字面量加 `0x`。
|
||||
|
||||
十进制字面量包含数字 `0` 至 `9`。二进制字面量只包含 `0` 或 `1`,八进制字面量包含数字 `0` 至 `7`,十六进制字面量包含数字 `0` 至 `9` 以及字母 `A` 至 `F`(大小写均可)。
|
||||
十进制字面量包含数字 `0` 至 `9`。二进制字面量包含 `0` 和 `1`,八进制字面量包含数字 `0` 至 `7`,十六进制字面量包含数字 `0` 至 `9` 以及字母 `A` 至 `F`(大小写均可)。
|
||||
|
||||
负整数的字面量在整数字面量前加负号 `-`,比如 `-42`。
|
||||
负整数字面量的表示方式为在整数字面量前加负号 `-`,比如 `-42`。
|
||||
|
||||
整型字面面可以使用下划线(`_`)来增加数字的可读性,下划线会被系统忽略,因此不会影响字面量的值。同样地,也可以在数字前加 `0`,这同样也会被系统所忽略,并不会影响字面量的值。
|
||||
|
||||
@ -194,52 +203,52 @@ true // 布尔值字面量
|
||||
>
|
||||
#### integer-literal {#integer-literal}
|
||||
>
|
||||
> *整数字面量* → [*二进制字面量*](#binary-literal)
|
||||
> *整数字面量* → [二进制字面量](#binary-literal)
|
||||
>
|
||||
> *整数字面量* → [*八进制字面量*](#octal-literal)
|
||||
> *整数字面量* → [八进制字面量](#octal-literal)
|
||||
>
|
||||
> *整数字面量* → [*十进制字面量*](#decimal-literal)
|
||||
> *整数字面量* → [十进制字面量](#decimal-literal)
|
||||
>
|
||||
> *整数字面量* → [*十六进制字面量*](#hexadecimal-literal)
|
||||
> *整数字面量* → [十六进制字面量](#hexadecimal-literal)
|
||||
>
|
||||
>
|
||||
#### binary-literal {#binary-literal}
|
||||
>
|
||||
> *二进制字面量* → **0b** [*二进制数字*](#binary-digit) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
|
||||
> *二进制字面量* → **0b** [二进制数字](#binary-digit) [二进制字面量字符组](#binary-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### binary-digit {#binary-digit}
|
||||
>
|
||||
> *二进制数字* → 数值 0 到 1
|
||||
> *二进制数字* → 数值 0 或 1
|
||||
>
|
||||
> *二进制字面量字符* → [*二进制数字*](#binary-digit) | -
|
||||
> *二进制字面量字符* → [二进制数字](#binary-digit) | **_**
|
||||
>
|
||||
>
|
||||
#### binary-literal-characters {#binary-literal-characters}
|
||||
>
|
||||
> *二进制字面量字符组* → [*二进制字面量字符*](#binary-literal-character) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
|
||||
> *二进制字面量字符组* → [二进制字面量字符](#binary-literal-character) [二进制字面量字符组](#binary-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### octal-literal {#octal-literal}
|
||||
>
|
||||
> *八进制字面量* → **0o** [*八进字数字*](#octal-digit) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
|
||||
> *八进制字面量* → **0o** [八进字数字](#octal-digit) [八进制字符组](#octal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### octal-digit {#octal-digit}
|
||||
>
|
||||
> *八进字数字* → 数值 0 到 7
|
||||
>
|
||||
> *八进制字符* → [*八进字数字*](#octal-digit) | -
|
||||
> *八进制字符* → [八进字数字](#octal-digit) | **_**
|
||||
>
|
||||
>
|
||||
#### octal-literal-characters {#octal-literal-characters}
|
||||
>
|
||||
> *八进制字符组* → [*八进制字符*](#octal-literal-character) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
|
||||
> *八进制字符组* → [八进制字符](#octal-literal-character) [八进制字符组](#octal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### decimal-literal {#decimal-literal}
|
||||
>
|
||||
> *十进制字面量* → [*十进制数字*](#decimal-digit) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
|
||||
> *十进制字面量* → [十进制数字](#decimal-digit) [十进制字符组](#decimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### decimal-digit {#decimal-digit}
|
||||
@ -249,38 +258,38 @@ true // 布尔值字面量
|
||||
>
|
||||
#### decimal-literal-characters {#decimal-literal-characters}
|
||||
>
|
||||
> *十进制数字组* → [*十进制数字*](#decimal-digit) [*十进制数字组*](#decimal-literal-characters)<sub>可选</sub>
|
||||
> *十进制数字组* → [十进制数字](#decimal-digit) [十进制数字组](#decimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
> *十进制字符* → [*十进制数字*](#decimal-digit) | -
|
||||
> *十进制字符* → [十进制数字](#decimal-digit) | **_**
|
||||
>
|
||||
> *十进制字符组* → [*十进制字符*](#decimal-literal-characters) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
|
||||
> *十进制字符组* → [十进制字符](#decimal-literal-characters) [十进制字符组](#decimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### hexadecimal-literal {#hexadecimal-literal}
|
||||
>
|
||||
> *十六进制字面量* → **0x** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
|
||||
> *十六进制字面量* → **0x** [十六进制数字](#hexadecimal-digit) [十六进制字面量字符组](#hexadecimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### hexadecimal-digit {#hexadecimal-digit}
|
||||
>
|
||||
> *十六进制数字* → 数值 0 到 9, 字母 a 到 f, 或 A 到 F
|
||||
>
|
||||
> *十六进制字符* → [*十六进制数字*](#hexadecimal-digit) | -
|
||||
> *十六进制字符* → [十六进制数字](#hexadecimal-digit) | **-**
|
||||
>
|
||||
>
|
||||
#### hexadecimal-literal-characters {#hexadecimal-literal-characters}
|
||||
>
|
||||
> *十六进制字面量字符组* → [*十六进制字符*](#hexadecimal-literal-characters) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
|
||||
> *十六进制字面量字符组* → [十六进制字符](#hexadecimal-literal-characters) [十六进制字面量字符组](#hexadecimal-literal-characters)<sub>可选</sub>
|
||||
|
||||
### 浮点数字面量{#floating-point-literal}
|
||||
|
||||
*浮点数字面量(Floating-point literals)* 表示未指定精度浮点数的值。
|
||||
*浮点数字面量(Floating-point literals)*表示未指定精度浮点数的值。
|
||||
|
||||
浮点数字面量默认用十进制表示(无前缀),也可以用十六进制表示(加前缀 `0x`)。
|
||||
|
||||
十进制浮点数字面量由十进制数字串后跟小数部分或指数部分(或两者皆有)组成。十进制小数部分由小数点(`.`)后跟十进制数字串组成。指数部分由大写或小写字母 `e` 为前缀后跟十进制数字串组成,这串数字表示 `e` 之前的数量乘以 10 的几次方。例如:`1.25e2` 表示 1.25 x 10²,也就是 `125.0`;同样,`1.25e-2` 表示 1.25 x 10¯²,也就是 `0.0125`。
|
||||
十进制浮点数字面量由十进制数字串后跟十进制小数部分或十进制指数部分(或两者皆有)组成。十进制小数部分由小数点(`.`)后跟十进制数字串组成。指数部分由大写或小写字母 `e` 为前缀后跟十进制数字串组成,这串数字表示 `e` 之前的数值乘以 10 的几次方。例如:`1.25e2` 表示 1.25 x 10²,也就是 `125.0`。同样,`1.25e-2` 表示 1.25 x 10¯²,也就是 `0.0125`。
|
||||
|
||||
十六进制浮点数字面量由前缀 `0x` 后跟可选的十六进制小数部分以及十六进制指数部分组成。十六进制小数部分由小数点后跟十六进制数字串组成。指数部分由大写或小写字母 `p` 为前缀后跟十进制数字串组成,这串数字表示 `p` 之前的数量乘以 2 的几次方。例如:`0xFp2` 表示 15 x 2²,也就是 `60`;同样,`0xFp-2` 表示 15 x 2¯²,也就是 `3.75`。
|
||||
十六进制浮点数字面量由前缀 `0x` 后跟可选的十六进制小数部分以及十六进制指数部分组成。十六进制小数部分由小数点后跟十六进制数字串组成。指数部分由大写或小写字母 `p` 为前缀后跟十进制数字串组成,这串数字表示 `p` 之前的数量乘以 2 的几次方。例如:`0xFp2` 表示 15 x 2²,也就是 `60`。同样,`0xFp-2` 表示 15 x 2¯²,也就是 `3.75`。
|
||||
|
||||
负数的浮点数字面量由负号(`-`)和浮点数字面量组成,例如 `-42.5`。
|
||||
|
||||
@ -293,29 +302,29 @@ true // 布尔值字面量
|
||||
>
|
||||
#### floating-point-literal {#floating-point-literal}
|
||||
>
|
||||
> *浮点数字面量* → [*十进制字面量*](#decimal-literal) [*十进制分数*](#decimal-fraction)<sub>可选</sub> [*十进制指数*](#decimal-exponent)<sub>可选</sub>
|
||||
> *浮点数字面量* → [十进制字面量](#decimal-literal) [十进制分数](#decimal-fraction)<sub>可选</sub> [十进制指数](#decimal-exponent)<sub>可选</sub>
|
||||
>
|
||||
> *浮点数字面量* → [*十六进制字面量*](#hexadecimal-literal) [*十六进制分数*](#hexadecimal-fraction)<sub>可选</sub> [*十六进制指数*](#hexadecimal-exponent)
|
||||
> *浮点数字面量* → [十六进制字面量](#hexadecimal-literal) [十六进制分数](#hexadecimal-fraction)<sub>可选</sub> [十六进制指数](#hexadecimal-exponent)
|
||||
>
|
||||
>
|
||||
#### decimal-fraction {#decimal-fraction}
|
||||
>
|
||||
> *十进制分数* → **.** [*十进制字面量*](#decimal-literal)
|
||||
> *十进制分数* → **.** [十进制字面量](#decimal-literal)
|
||||
>
|
||||
>
|
||||
#### decimal-exponent {#decimal-exponent}
|
||||
>
|
||||
> *十进制指数* → [*十进制指数 e*](#floating-point-e) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal)
|
||||
> *十进制指数* → [十进制指数 e](#floating-point-e) [正负号](#sign)<sub>可选</sub> [十进制字面量](#decimal-literal)
|
||||
>
|
||||
>
|
||||
#### hexadecimal-fraction {#hexadecimal-fraction}
|
||||
>
|
||||
> *十六进制分数* → **.** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
|
||||
> *十六进制分数* → **.** [十六进制数字](#hexadecimal-digit) [十六进制字面量字符组](#hexadecimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### hexadecimal-exponent {#hexadecimal-exponent}
|
||||
>
|
||||
> *十六进制指数* → [*十六进制指数 p*](#floating-point-p) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal)
|
||||
> *十六进制指数* → [十六进制指数 p](#floating-point-p) [正负号](#sign)<sub>可选</sub> [十进制字面量](#decimal-literal)
|
||||
>
|
||||
>
|
||||
#### floating-point-e {#floating-point-e}
|
||||
@ -334,7 +343,7 @@ true // 布尔值字面量
|
||||
|
||||
### 字符串字面量 {#string-literal}
|
||||
|
||||
字符串字面量是被引号包括的一串字符组成。 单行字符串字面量被包在双引号中的一串字符组成,形式如下:
|
||||
字符串字面量是被引号包括的一串字符组成。单行字符串字面量是被包在双引号中的一串字符组成,形式如下:
|
||||
|
||||
> "`字符`"
|
||||
|
||||
@ -347,24 +356,24 @@ true // 布尔值字面量
|
||||
|
||||
与单行字符串字面量不同的是,多行字符串字面量可以包含不转义的双引号("),回车以及换行。它不能包含三个未转义的连续双引号。
|
||||
|
||||
""" 之后的回车或者换行开始多行字符串字面量,不是字符串的一部分。 """ 之前回车或者换行结束字面量,也不是字符串的一部分。要让多行字符串字面量的开始或结束带有换行,就在第一行或者最后一行写一个空行。
|
||||
`"""` 之后的回车或者换行开始多行字符串字面量,它们不是字符串的一部分。结束部分的 `"""` 以及它之前的回车或者换行,也不是字符串的一部分。要让多行字符串字面量的开始或结束带有换行,就在第一行或者最后一行写一个空行。
|
||||
|
||||
多行字符串字面量可以使用任何空格或制表符组合进行缩进;这些缩进不会包含在字符串中。 """ 的结束符号决定了缩进:字面量中的任何一个非空行必须起始于多行字符串字面量结束符号的前面;空格和制表符不会被转换。你可以包在缩进后含额外的空格和制表符;这些空格和制表符会在字符串中出现。
|
||||
多行字符串字面量可以使用任何空格或制表符组合进行缩进;这些缩进不会包含在字符串中。`"""` 的结束符号决定了缩进:字面量中的每一个非空行开头都必须与结束符 `"""` 之前出现的缩进完全一致;空格和制表符不会被转换。你可以在缩进后包含额外的空格和制表符;这些空格和制表符会在字符串中出现。
|
||||
|
||||
多行字符串字面量中的一行结束使用规范化的换行符号。尽管你的源代码混用了回车和换行符,字符串中所有的行结束都必须一样.
|
||||
|
||||
在多行字符串字面量里, 在行末用反斜线(`\`)可以省略字符串行间中断。 反斜线之间的空白和行间中断也可以省略。 你可以在你的代码里用这种语法硬包裹多行字符串字面量,不需要改变产生的字符串的值。
|
||||
在多行字符串字面量里,在行末用反斜线(`\`)可以省略字符串行间中断。反斜线和换行符之间的空白也将被忽略。你可以在你的代码里用这种语法硬包裹多行字符串字面量,而不改变结果字符串的值。
|
||||
|
||||
可以在字符串字面量中使用的转义特殊符号如下:
|
||||
|
||||
* 空字符 `\0`
|
||||
* 反斜线 `\\`
|
||||
* 水平制表符 `\t`
|
||||
* 换行符 `\n`
|
||||
* 回车符 `\r`
|
||||
* 双引号 `\"`
|
||||
* 单引号 `\'`
|
||||
* Unicode 标量 `\u{`n`}`,n 为一到八位的十六进制数字
|
||||
* 空字符 (`\0`)
|
||||
* 反斜线 (`\\`)
|
||||
* 水平制表符 (`\t`)
|
||||
* 换行符 (`\n`)
|
||||
* 回车符 (`\r`)
|
||||
* 双引号 (`\"`)
|
||||
* 单引号 (`\'`)
|
||||
* Unicode 标量 (`\u{`n`}`),n 为一到八位的十六进制数字
|
||||
|
||||
字符串字面量允许在反斜杠(`\`)后的括号 `()` 中插入表达式的值。插入表达式可以包含字符串字面量,但不能包含未转义的反斜线(`\`)、回车符以及换行符。
|
||||
|
||||
@ -378,7 +387,7 @@ true // 布尔值字面量
|
||||
let x = 3; "1 2 \(x)"
|
||||
```
|
||||
|
||||
可以使用一对或多对扩展分隔符(#)包裹字符串进行分隔,被分隔的字符串的形式如下所示:
|
||||
由扩展分隔符包裹的字符串,它是由引号以及成对出现的数字符号(`#`)包裹的字符串序列。由扩展分隔符包裹的字符串形式如下所示:
|
||||
|
||||
> \#"`characters`"#
|
||||
>
|
||||
@ -388,7 +397,7 @@ let x = 3; "1 2 \(x)"
|
||||
>
|
||||
> """#
|
||||
|
||||
特殊字符在被分隔符分隔的结果字符串中会展示为普通字符,而不是特殊字符。你可以使用扩展分隔符来创建一些具有特殊效果的字符串。例如,生成字符串插值,启动或终止转义序列(字符串)。
|
||||
特殊字符在被扩展分隔符分隔的结果字符串中会展示为普通字符,而不是特殊字符。你可以使用扩展分隔符来创建一些通常情况下具有特殊效果的字符串。例如,生成字符串插值,启动转义序列或终止字符串。
|
||||
|
||||
以下所示,由字符串字面量和扩展分隔符所创建的字符串是等价的:
|
||||
|
||||
@ -423,75 +432,75 @@ let textB = "Hello world"
|
||||
|
||||
> 字符串字面量语法
|
||||
>
|
||||
> *字符串字面量* → [*静态字符串字面量*](#static-string-literal) | [*插值字符串字面量*](#interpolated-string-literal)
|
||||
> *字符串字面量* → [静态字符串字面量](#static-string-literal) | [插值字符串字面量](#interpolated-string-literal)
|
||||
>
|
||||
> *字符串开分隔定界符* → [*字符串扩展分隔符*](#extended-string-literal-delimiter) **"**
|
||||
> *字符串开分隔定界符* → [字符串扩展分隔符](#extended-string-literal-delimiter) **"**
|
||||
>
|
||||
> *字符串闭分隔定界符* → **"** [*字符串扩展分隔符*](#extended-string-literal-delimiter)<sub>可选</sub>
|
||||
> *字符串闭分隔定界符* → **"** [字符串扩展分隔符](#extended-string-literal-delimiter)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### static-string-literal {#static-string-literal}
|
||||
>
|
||||
> *静态字符串字面量* → [*字符串开分隔定界符*](#extended-string-literal-delimiter) [*引用文本*](#quoted-text)<sub>可选</sub> [*字符串闭分隔定界符*](#extended-string-literal-delimiter)
|
||||
> *静态字符串字面量* → [字符串开分隔定界符](#extended-string-literal-delimiter) [引用文本](#quoted-text)<sub>可选</sub> [字符串闭分隔定界符](#extended-string-literal-delimiter)
|
||||
>
|
||||
> *静态字符串字面量* → [*多行字符串开分隔定界符*](#extended-string-literal-delimiter) [*多行引用文本*](#multiline-quoted-text)<sub>可选</sub> [*多行字符串闭分隔定界符*](#extended-string-literal-delimiter)
|
||||
> *静态字符串字面量* → [多行字符串开分隔定界符](#extended-string-literal-delimiter) [多行引用文本](#multiline-quoted-text)<sub>可选</sub> [多行字符串闭分隔定界符](#extended-string-literal-delimiter)
|
||||
>
|
||||
> *多行字符串开分隔定界符* → [*字符串扩展分隔符*](#extended-string-literal-delimiter) **"""**
|
||||
> *多行字符串开分隔定界符* → [字符串扩展分隔符](#extended-string-literal-delimiter) **"""**
|
||||
>
|
||||
> *多行字符串闭分隔定界符* → **"""** [*字符串扩展分隔符*](#extended-string-literal-delimiter)
|
||||
> *多行字符串闭分隔定界符* → **"""** [字符串扩展分隔符](#extended-string-literal-delimiter)
|
||||
>
|
||||
>
|
||||
#### extended-string-literal-delimiter {#extended-string-literal-delimiter}
|
||||
>
|
||||
> *字符串扩展分隔符* → **#** [*字符串扩展分隔符*](#extended-string-literal-delimiter)<sub>可选</sub>
|
||||
> *字符串扩展分隔符* → **#** [字符串扩展分隔符](#extended-string-literal-delimiter)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### quoted-text {#quoted-text}
|
||||
>
|
||||
> *引用文本* → [*引用文本项*](#quoted-text-item) [*引用文本*](#quoted-text)<sub>可选</sub>
|
||||
> *引用文本* → [引用文本项](#quoted-text-item) [引用文本](#quoted-text)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### quoted-text-item {#quoted-text-item}
|
||||
>
|
||||
> *引用文本项* → [*转义字符*](#escaped-character)
|
||||
> *引用文本项* → [转义字符](#escaped-character)
|
||||
>
|
||||
> *引用文本项* → 除了 **"**、**\\**、U+000A、U+000D 以外的所有 Unicode 字符
|
||||
>
|
||||
>
|
||||
#### multiline-quoted-text {#multiline-quoted-text}
|
||||
>
|
||||
> *多行引用文本* → [*多行引用文本项*](#multiline-quoted-text-item) [*多行引用文本*](#multiline-quoted-text)<sub>可选</sub>
|
||||
> *多行引用文本* → [多行引用文本项](#multiline-quoted-text-item) [多行引用文本](#multiline-quoted-text)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### multiline-quoted-text-item {#multiline-quoted-text-item}
|
||||
>
|
||||
> *多行引用文本项* [*转义字符*](#escaped-character)<sub>可选</sub>
|
||||
> *多行引用文本项* [转义字符](#escaped-character)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### multiline-quoted-text {#multiline-quoted-text}
|
||||
>
|
||||
> *多行引用文本* → 除了 **\** 以外的任何Unicode标量值
|
||||
> *多行引用文本* → 除了 **\\** 以外的任何 Unicode 标量值
|
||||
>
|
||||
> *多行引用文本* → [*转义换行*](#escaped-newline)
|
||||
> *多行引用文本* → [转义换行](#escaped-newline)
|
||||
>
|
||||
>
|
||||
#### interpolated-string-literal {#interpolated-string-literal}
|
||||
>
|
||||
> *插值字符串字面量* → [*字符串开分隔定界符*](#extended-string-literal-delimiter) [*插值文本*](#interpolated-text)<sub>可选</sub> [*字符串闭分隔定界符*](#extended-string-literal-delimiter)
|
||||
> *插值字符串字面量* → [字符串开分隔定界符](#extended-string-literal-delimiter) [插值文本](#interpolated-text)<sub>可选</sub> [字符串闭分隔定界符](#extended-string-literal-delimiter)
|
||||
>
|
||||
> *插值字符串字面量* → [*多行字符串开分隔定界符*](#extended-string-literal-delimiter) [*插值文本*](#interpolated-text)<sub>可选</sub> [*多行字符串闭分隔定界符*](#extended-string-literal-delimiter)
|
||||
> *插值字符串字面量* → [多行字符串开分隔定界符](#extended-string-literal-delimiter) [插值文本](#interpolated-text)<sub>可选</sub> [多行字符串闭分隔定界符](#extended-string-literal-delimiter)
|
||||
>
|
||||
>
|
||||
#### interpolated-text {#interpolated-text}
|
||||
>
|
||||
> *插值文本* → [*插值文本项*](#interpolated-text-item) [*插值文本*](#interpolated-text)<sub>可选</sub>
|
||||
> *插值文本* → [插值文本项](#interpolated-text-item) [插值文本](#interpolated-text)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### interpolated-text-item {#interpolated-text-item}
|
||||
>
|
||||
> *插值文本项* → **\\****(**[*表达式*](./04_Expressions.md)**)** | [*引用文本项*](#quoted-text-item)
|
||||
> *插值文本项* → **\\(**[ 表达式 ](./04_Expressions.md)**)** | [引用文本项](#quoted-text-item)
|
||||
>
|
||||
> *多行插值文本* → [*多行插值文本项*](#multiline-quoted-text-item) [*多行插值文本*](#multiline-quoted-text)<sub>可选</sub>
|
||||
> *多行插值文本* → [多行插值文本项](#multiline-quoted-text-item) [多行插值文本](#multiline-quoted-text)<sub>可选</sub>
|
||||
>
|
||||
> *多行插值文本项* → **\\(** [表达式](./04_Expressions.md) **)** | [多行引用文本项](#multiline-quoted-text-item)
|
||||
>
|
||||
@ -503,9 +512,9 @@ let textB = "Hello world"
|
||||
>
|
||||
#### escaped-character {#escaped-character}
|
||||
>
|
||||
> *转义字符* → [*转义序列*](#escape-sequence) **0** | [*转义序列*](#escape-sequence) **\\** | [*转义序列*](#escape-sequence) **t** | [*转义序列*](#escape-sequence) **n** | [*转义序列*](#escape-sequence) **r** | [*转义序列*](#escape-sequence) **\"** | [*转义序列*](#escape-sequence) **'**
|
||||
> *转义字符* → [转义序列](#escape-sequence) **0** | [转义序列](#escape-sequence) **\\** | [转义序列](#escape-sequence) **t** | [转义序列](#escape-sequence) **n** | [转义序列](#escape-sequence) **r** | [转义序列](#escape-sequence) **\"** | [转义序列](#escape-sequence) **'**
|
||||
>
|
||||
> *转义字符* → [*转义序列*](#escape-sequence) **u {** [*unicode 标量数字*](#unicode-scalar-digits) **}**
|
||||
> *转义字符* → [转义序列](#escape-sequence) **u {** [unicode 标量数字](#unicode-scalar-digits) **}**
|
||||
>
|
||||
>
|
||||
#### unicode-scalar-digits {#unicode-scalar-digits}
|
||||
@ -515,43 +524,43 @@ let textB = "Hello world"
|
||||
>
|
||||
#### escaped-newline {#escaped-newline}
|
||||
>
|
||||
> *转义换行符* → [*转义序列*](#escape-sequence) [*空白*](#whitespace)<sub>可选</sub> [*断行符*](#line-break)
|
||||
> *转义换行符* → [转义序列](#escape-sequence) [空白](#whitespace)<sub>可选</sub> [断行符](#line-break)
|
||||
|
||||
|
||||
## 运算符 {#operator}
|
||||
|
||||
Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基础运算符](../02_language_guide/02_Basic_Operators.md) 和 [高级运算符](../02_language_guide/27_Advanced_Operators.md) 中进行了阐述。这一小节将描述哪些字符能用于自定义运算符。
|
||||
|
||||
自定义运算符可以由以下其中之一的 ASCII 字符 `/`、`=`、`-`、`+`、`!`、`*`、`%`、`<`、`>`、`&`、`|`、`^`、`?` 以及 `~`,或者后面语法中规定的任一个 Unicode 字符(其中包含了*数学运算符*、*零散符号(Miscellaneous Symbols)* 以及印刷符号(Dingbats)之类的 Unicode 块)开始。在第一个字符之后,允许使用组合型 Unicode 字符。
|
||||
自定义运算符可以由以下其中之一的 ASCII 字符 `/`、`=`、`-`、`+`、`!`、`*`、`%`、`<`、`>`、`&`、`|`、`^`、`?` 以及 `~`,或者后面语法中规定的任一个 Unicode 字符(其中包含了*数学运算符*、*零散符号(Miscellaneous Symbols)* 以及*印刷符号(Dingbats)*之类的 Unicode 块)开始。在第一个字符之后,允许使用组合型 Unicode 字符。
|
||||
|
||||
您也可以以点号(`.`)开头来定义自定义运算符。这些运算符可以包含额外的点,例如 `.+.`。如果某个运算符不是以点号开头的,那么它就无法再包含另外的点号了。例如,`+.+` 就会被看作为一个 `+` 运算符后面跟着一个 `.+` 运算符。
|
||||
您也可以以点号(`.`)开头来定义自定义运算符。这些运算符可以包含额外的点。例如 `.+.` 会被看作一个单独的运算符。如果某个运算符不是以点号开头的,那么它就无法再包含另外的点号了。例如,`+.+` 就会被看作为一个 `+` 运算符后面跟着一个 `.+` 运算符。
|
||||
|
||||
虽然您可以用问号 `?` 来自定义运算符,但是这个运算符不能只包含单独的一个问号。此外,虽然运算符可以包含一个惊叹号 `!`,但是前缀运算符不能够以问号或者惊叹号开头。
|
||||
虽然您可以用问号 `(?)` 来自定义运算符,但是这个运算符不能只包含单独的一个问号。此外,虽然运算符可以包含一个惊叹号 `(!)`,但是前缀运算符不能够以问号或者惊叹号开头。
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 以下这些标记 `=`、`->`、`//`、`/*`、`*/`、`.`、`<`(前缀运算符)、`&`、`?`、`?`(中缀运算符)、`>`(后缀运算符)、`!` 、`?` 是被系统保留的。这些符号不能被重载,也不能用于自定义运算符。
|
||||
> 以下这些标记 `=`、`->`、`//`、`/*`、`*/`、`.`,前缀运算符 `<`、`&` 和 `?`,中缀运算符 `?`,后缀运算符 `>`、`!` 和 `?` 是被系统保留的。这些符号不能被重载,也不能用作自定义运算符。
|
||||
|
||||
运算符两侧的空白被用来区分该运算符是否为前缀运算符、后缀运算符或二元运算符。规则总结如下:
|
||||
|
||||
* 如果运算符两侧都有空白或两侧都无空白,将被看作二元运算符。例如:`a+++b` 和 `a +++ b` 当中的 `+++` 运算符会被看作二元运算符。
|
||||
* 如果运算符只有左侧空白,将被看作一元前缀运算符。例如 `a +++b` 中的 `+++` 运算符会被看做是一元前缀运算符。
|
||||
* 如果运算符只有右侧空白,将被看作一元后缀运算符。例如 `a+++ b` 中的 `+++` 运算符会被看作是一元后缀运算符。
|
||||
* 如果运算符左侧没有空白并紧跟 `.`,将被看作一元后缀运算符。例如 `a+++.b` 中的 `+++` 运算符会被视为一元后缀运算符(即上式被视为 `a+++ .b` 而不是 `a +++ .b`)。
|
||||
* 如果运算符左侧没有空白并紧跟 `(.)`,将被看作一元后缀运算符。例如 `a+++.b` 中的 `+++` 运算符会被视为一元后缀运算符(即上式被视为 `a+++ .b` 而不是 `a +++ .b`)。
|
||||
|
||||
鉴于这些规则,运算符前的字符 `(`、`[` 和 `{`,运算符后的字符 `)`、`]` 和 `}`,以及字符 `,`、`;` 和 `:` 都被视为空白。
|
||||
鉴于这些规则,`(`、`[` 和 `{` 是在运算符前面,`)`、`]` 和 `}` 是在运算符后面,以及字符 `,`、`;` 和 `:` 都被视为空白。
|
||||
|
||||
以上规则需注意一点,如果预定义运算符 `!` 或 `?` 左侧没有空白,则不管右侧是否有空白都将被看作后缀运算符。如果将 `?` 用作可选链式调用运算符,左侧必须无空白。如果用于条件运算符 `? :`,必须两侧都有空白。
|
||||
以上规则需注意一点。如果预定义运算符 `!` 或 `?` 左侧没有空白,则不管右侧是否有空白都将被看作后缀运算符。如果将 `?` 用作可选链式调用运算符,左侧必须无空白。如果用于条件运算符 `(? :)`,必须两侧都有空白。
|
||||
|
||||
在某些特定的设计中 ,以 `<` 或 `>` 开头的运算符会被分离成两个或多个符号,剩余部分可能会以同样的方式被再次分离。因此,在 `Dictionary<String, Array<Int>>` 中没有必要添加空白来消除闭合字符 `>` 的歧义。在这个例子中, 闭合字符 `>` 不会被视为单独的符号,因而不会被错误解析为 `>>` 运算符。
|
||||
在某些特定的设计中,以 `<` 或 `>` 开头的运算符会被分离成两个或多个符号。剩余部分可能会以同样的方式被再次分离。因此,在 `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)。
|
||||
|
||||
> 运算符语法
|
||||
>
|
||||
> *运算符* → [*头部运算符*](#operator-head) [*运算符字符组*](#operator-characters)<sub>可选</sub>
|
||||
> *运算符* → [头部运算符](#operator-head) [运算符字符组](#operator-characters)<sub>可选</sub>
|
||||
>
|
||||
> *运算符* → [*头部点运算符*](#dot-operator-head) [*点运算符字符组*](#dot-operator-characters)
|
||||
> *运算符* → [头部点运算符](#dot-operator-head) [点运算符字符组](#dot-operator-characters)
|
||||
>
|
||||
>
|
||||
#### operator-head {#operator-head}
|
||||
@ -564,9 +573,13 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
|
||||
>
|
||||
> *头部运算符* → U+00AC 或 U+00AE
|
||||
>
|
||||
> *头部运算符* → U+00B0–U+00B1,U+00B6,U+00BB,U+00BF,U+00D7,或 U+00F7
|
||||
> *头部运算符* → U+00B0–U+00B1
|
||||
>
|
||||
> *头部运算符* → U+00B6,U+00BB,U+00BF,U+00D7,或 U+00F7
|
||||
>
|
||||
> *头部运算符* → U+2016–U+2017 或 U+2020–U+2027
|
||||
> *头部运算符* → U+2016–U+2017
|
||||
>
|
||||
> *头部运算符* → U+2020–U+2027
|
||||
>
|
||||
> *头部运算符* → U+2030–U+203E
|
||||
>
|
||||
@ -584,12 +597,14 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
|
||||
>
|
||||
> *头部运算符* → U+3001–U+3003
|
||||
>
|
||||
> *头部运算符* → U+3008–U+3030
|
||||
> *头部运算符* → U+3008–U+3020
|
||||
>
|
||||
> *头部运算符* → U+3030
|
||||
>
|
||||
>
|
||||
#### operator-character {#operator-character}
|
||||
>
|
||||
> *运算符字符* → [*头部运算符*](#operator-head)
|
||||
> *运算符字符* → [头部运算符](#operator-head)
|
||||
>
|
||||
> *运算符字符* → U+0300–U+036F
|
||||
>
|
||||
@ -606,25 +621,25 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
|
||||
>
|
||||
#### operator-characters {#operator-characters}
|
||||
>
|
||||
> *运算符字符组* → [*运算符字符*](#operator-character) [*运算符字符组*](#operator-characters)<sub>可选</sub>
|
||||
> *运算符字符组* → [运算符字符](#operator-character) [运算符字符组](#operator-characters)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### dot-operator-head {#dot-operator-head}
|
||||
>
|
||||
> *头部点运算符* → **..**
|
||||
> *头部点运算符* → **.**
|
||||
>
|
||||
>
|
||||
#### dot-operator-character {#dot-operator-character}
|
||||
>
|
||||
> *点运算符字符* → **.** | [*运算符字符*](#operator-character)
|
||||
> *点运算符字符* → **.** | [运算符字符](#operator-character)
|
||||
>
|
||||
>
|
||||
#### dot-operator-characters {#dot-operator-characters}
|
||||
>
|
||||
> *点运算符字符组* → [*点运算符字符*](#dot-operator-character) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub>
|
||||
> *点运算符字符组* → [点运算符字符](#dot-operator-character) [点运算符字符组](#dot-operator-characters)<sub>可选</sub>
|
||||
>
|
||||
> *二元运算符* → [*运算符*](#operator)
|
||||
> *二元运算符* → [运算符](#operator)
|
||||
>
|
||||
> *前缀运算符* → [*运算符*](#operator)
|
||||
> *前缀运算符* → [运算符](#operator)
|
||||
>
|
||||
> *后缀运算符* → [*运算符*](#operator)
|
||||
> *后缀运算符* → [运算符](#operator)
|
||||
|
||||
@ -56,7 +56,7 @@ func someFunction(a: Int) { /* ... */ }
|
||||
>
|
||||
|
||||
#### type-annotation {#type-annotation}
|
||||
> *类型注解* → **:** [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type)
|
||||
> *类型注解* → **:** [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [类型](#type)
|
||||
|
||||
## 类型标识符 {#type-identifier-h}
|
||||
*类型标识符*可以引用命名型类型,还可引用命名型或复合型类型的别名。
|
||||
@ -80,10 +80,10 @@ 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)
|
||||
> *类型名称* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
|
||||
## 元组类型 {#tuple-type-h}
|
||||
*元组类型*是使用括号括起来的零个或多个类型,类型间用逗号隔开。
|
||||
@ -105,19 +105,19 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配
|
||||
>
|
||||
|
||||
#### tuple-type {#tuple-type}
|
||||
> *元组类型* → **(** **)** | **(** [*元组类型元素*](#tuple-type-element) **,** [*元组类型元素列表*](#tuple-type-element-list) **)**
|
||||
> *元组类型* → **(** **)** | **(** [元组类型元素](#tuple-type-element) **,** [元组类型元素列表](#tuple-type-element-list) **)**
|
||||
>
|
||||
|
||||
#### tuple-type-element-list {#tuple-type-element-list}
|
||||
> *元组类型元素列表* → [*元组类型元素*](#tuple-type-element) | [*元组类型元素*](#tuple-type-element) **,** [*元组类型元素列表*](#tuple-type-element-list)
|
||||
> *元组类型元素列表* → [元组类型元素](#tuple-type-element) | [元组类型元素](#tuple-type-element) **,** [元组类型元素列表](#tuple-type-element-list)
|
||||
>
|
||||
|
||||
#### tuple-type-element {#tuple-type-element}
|
||||
> *元组类型元素* → [*元素名*](#element-name) [*类型注解*](#type-annotation) | [*类型*](#type)
|
||||
> *元组类型元素* → [元素名](#element-name) [类型注解](#type-annotation) | [类型](#type)
|
||||
>
|
||||
|
||||
#### element-name {#element-name}
|
||||
> *元素名* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *元素名* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 函数类型 {#function-type-h}
|
||||
@ -195,21 +195,21 @@ func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) {
|
||||
>
|
||||
|
||||
#### function-type {#function-type}
|
||||
> *函数类型* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*函数类型子句*](#function-type-argument-clause) **throws**<sub>可选</sub> **->** [*类型*](#type)
|
||||
> *函数类型* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> [函数类型子句](#function-type-argument-clause) **throws**<sub>可选</sub> **->** [类型](#type)
|
||||
|
||||
#### function-type-argument-clause {#function-type-argument-clause}
|
||||
> *函数类型子句* → **(** **)**
|
||||
> *函数类型子句* → **(** [*函数类型实参列表*](#function-type-argument-list) *...* <sub>可选</sub> **)**
|
||||
> *函数类型子句* → **(** [函数类型实参列表](#function-type-argument-list) *...* <sub>可选</sub> **)**
|
||||
|
||||
#### function-type-argument-list {#function-type-argument-list}
|
||||
> *函数类型实参列表* → [*函数类型实参*](function-type-argument) | [*函数类型实参*](function-type-argument), [*函数类型实参列表*](#function-type-argument-list)
|
||||
> *函数类型实参列表* → [函数类型实参](function-type-argument) | [函数类型实参](function-type-argument), [函数类型实参列表](#function-type-argument-list)
|
||||
|
||||
#### function-type-argument {#function-type-argument}
|
||||
|
||||
> *函数类型实参* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type) | [*实参标签*](#argument-label) [*类型注解*](#type-annotation)
|
||||
> *函数类型实参* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [类型](#type) | [实参标签](#argument-label) [类型注解](#type-annotation)
|
||||
|
||||
#### argument-label {#argument-label}
|
||||
> *形参标签* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *形参标签* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
|
||||
## 数组类型 {#array-type-h}
|
||||
Swift 语言为标准库中定义的 `Array<Element>` 类型提供了如下语法糖:
|
||||
@ -240,7 +240,7 @@ var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
|
||||
>
|
||||
|
||||
#### array-type {#array-type}
|
||||
> *数组类型* → **[** [*类型*](#type) **]**
|
||||
> *数组类型* → **[** [类型](#type) **]**
|
||||
>
|
||||
|
||||
## 字典类型 {#dictionary-type-h}
|
||||
@ -268,7 +268,7 @@ let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39]
|
||||
>
|
||||
|
||||
#### dictionary-type {#dictionary-type}
|
||||
> *字典类型* → **[** [*类型*](#type) **:** [*类型*](#type) **]**
|
||||
> *字典类型* → **[** [类型](#type) **:** [类型](#type) **]**
|
||||
>
|
||||
|
||||
## 可选类型 {#optional-type-h}
|
||||
@ -300,7 +300,7 @@ optionalInteger! // 42
|
||||
>
|
||||
|
||||
#### optional-type {#optional-type}
|
||||
> *可选类型* → [*类型*](#type) **?**
|
||||
> *可选类型* → [类型](#type) **?**
|
||||
>
|
||||
|
||||
## 隐式解析可选类型 {#implicitly-unwrapped-optional-type-h}
|
||||
@ -335,7 +335,7 @@ let implicitlyUnwrappedArray: [Int]! // 正确
|
||||
>
|
||||
|
||||
#### implicitly-unwrapped-optional-type {#implicitly-unwrapped-optional-type}
|
||||
> *隐式解析可选类型* → [*类型*](#type) **!**
|
||||
> *隐式解析可选类型* → [类型](#type) **!**
|
||||
>
|
||||
|
||||
## 协议合成类型 {#protocol-composition-type-h}
|
||||
@ -364,11 +364,11 @@ typealias PQR = PQ & Q & R
|
||||
>
|
||||
|
||||
#### protocol-composition-type {#protocol-composition-type}
|
||||
> *协议合成类型* → [*协议标识符*](#protocol-identifier) & [*协议合成延续*](#protocol-composition-continuation)
|
||||
> *协议合成类型* → [协议标识符](#protocol-identifier) & [协议合成延续](#protocol-composition-continuation)
|
||||
>
|
||||
|
||||
#### protocol-composition-continuation {#protocol-composition-continuation}
|
||||
> *协议合成延续* → [*协议标识符*](#protocol-identifier) | [*协议合成类型*](#protocol-composition-type)
|
||||
> *协议合成延续* → [协议标识符](#protocol-identifier) | [协议合成类型](#protocol-composition-type)
|
||||
|
||||
## 不透明类型 {#opaque-type-h}
|
||||
|
||||
@ -442,7 +442,7 @@ let anotherInstance = metatype.init(string: "some string")
|
||||
>
|
||||
|
||||
#### metatype-type {#metatype-type}
|
||||
> *元类型* → [*类型*](#type) **.** **Type** | [*类型*](#type) **.** **Protocol**
|
||||
> *元类型* → [类型](#type) **.** **Type** | [类型](#type) **.** **Protocol**
|
||||
|
||||
## 自身类型 {#self-type-h}
|
||||
|
||||
@ -496,15 +496,14 @@ print(type(of: z.f()))
|
||||
>
|
||||
|
||||
#### type-inheritance-clause {#type-inheritance-clause}
|
||||
> *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list)
|
||||
> *类型继承子句* → **:** [类型继承列表](#type-inheritance-list)
|
||||
>
|
||||
|
||||
#### type-inheritance-list {#type-inheritance-list}
|
||||
> *类型继承列表* → [*类型标识符*](#type-identifier) | [*类型标识符*](#type-identifier) **,** [*类型继承列表*](#type-inheritance-list)
|
||||
>
|
||||
|
||||
> *类型继承列表* → [类型标识符](#type-identifier) | [类型标识符](#type-identifier) **,** [类型继承列表](#type-inheritance-list)
|
||||
|
||||
## 类型推断
|
||||
|
||||
Swift 广泛使用*类型推断*,从而允许你省略代码中很多变量和表达式的类型或部分类型。比如,对于 `var x: Int = 0`,你可以完全省略类型而简写成 `var x = 0`,编译器会正确推断出 `x` 的类型 `Int`。类似的,当完整的类型可以从上下文推断出来时,你也可以省略类型的一部分。比如,如果你写了 `let dict: Dictionary = ["A" : 1]`,编译器能推断出 `dict` 的类型是 `Dictionary<String, Int>`。
|
||||
|
||||
在上面的两个例子中,类型信息从表达式树的叶子节点传向根节点。也就是说,`var x: Int = 0` 中 `x` 的类型首先根据 `0` 的类型进行推断,然后将该类型信息传递到根节点(变量 `x`)。
|
||||
|
||||
@ -8,11 +8,12 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
>
|
||||
|
||||
#### expression {#expression}
|
||||
> *表达式* → [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
|
||||
> *表达式* → [try 运算符](#try-operator)<sub>可选</sub> [前缀表达式](#prefix-expression) [二元表达式列表](#binary-expressions)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### expression-list {#expression-list}
|
||||
> *表达式列表* → [*表达式*](#expression) | [*表达式*](#expression) **,** [*表达式列表*](#expression-list)
|
||||
|
||||
> *表达式列表* → [表达式](#expression) | [表达式](#expression) **,** [表达式列表](#expression-list)
|
||||
>
|
||||
|
||||
## 前缀表达式 {#prefix-expressions}
|
||||
@ -28,13 +29,13 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
>
|
||||
|
||||
#### prefix-expression {#prefix-expression}
|
||||
> *前缀表达式* → [*前缀运算符*](./02_Lexical_Structure.md#prefix-operator)<sub>可选</sub> [*后缀表达式*](#postfix-expression)
|
||||
> *前缀表达式* → [前缀运算符](./02_Lexical_Structure.md#prefix-operator)<sub>可选</sub> [后缀表达式](#postfix-expression)
|
||||
>
|
||||
> *前缀表达式* → [*输入输出表达式*](#in-out-expression)
|
||||
> *前缀表达式* → [输入输出表达式](#in-out-expression)
|
||||
>
|
||||
|
||||
#### in-out-expression {#in-out-expression}
|
||||
> *输入输出表达式* → **&** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *输入输出表达式* → **&** [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
### Try 运算符 {#try-operator}
|
||||
@ -94,17 +95,17 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
#### binary-expression {#binary-expression}
|
||||
> 二元表达式语法
|
||||
>
|
||||
> *二元表达式* → [*二元运算符*](./02_Lexical_Structure.md#binary-operator) [*前缀表达式*](#prefix-expression)
|
||||
> *二元表达式* → [二元运算符](./02_Lexical_Structure.md#binary-operator) [前缀表达式](#prefix-expression)
|
||||
>
|
||||
> *二元表达式* → [*赋值运算符*](#assignment-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression)
|
||||
> *二元表达式* → [赋值运算符](#assignment-operator) [try 运算符](#try-operator)<sub>可选</sub> [前缀表达式](#prefix-expression)
|
||||
>
|
||||
> *二元表达式* → [*条件运算符*](#conditional-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression)
|
||||
> *二元表达式* → [条件运算符](#conditional-operator) [try 运算符](#try-operator)<sub>可选</sub> [前缀表达式](#prefix-expression)
|
||||
>
|
||||
> *二元表达式* → [*类型转换运算符*](#type-casting-operator)
|
||||
> *二元表达式* → [类型转换运算符](#type-casting-operator)
|
||||
>
|
||||
|
||||
#### binary-expressions {#binary-expressions}
|
||||
> *二元表达式列表* → [*二元表达式*](#binary-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
|
||||
> *二元表达式列表* → [二元表达式](#binary-expression) [二元表达式列表](#binary-expressions)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### 赋值表达式 {#assignment-operator}
|
||||
@ -143,7 +144,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
>
|
||||
|
||||
#### conditional-operator {#conditional-operator}
|
||||
> *三元条件运算符* → **?** [*表达式*](#expression) **:**
|
||||
> *三元条件运算符* → **?** [表达式](#expression) **:**
|
||||
>
|
||||
|
||||
### 类型转换运算符 {#type-casting-operators}
|
||||
@ -189,13 +190,13 @@ f(x as Any)
|
||||
#### type-casting-operator {#type-casting-operator}
|
||||
> 类型转换运算符语法
|
||||
>
|
||||
> *类型转换运算符* → **is** [*类型*](./03_Types.md#type)
|
||||
> *类型转换运算符* → **is** [类型](./03_Types.md#type)
|
||||
>
|
||||
> *类型转换运算符* → **as** [*类型*](./03_Types.md#type)
|
||||
> *类型转换运算符* → **as** [类型](./03_Types.md#type)
|
||||
>
|
||||
> *类型转换运算符* → **as** **?** [*类型*](./03_Types.md#type)
|
||||
> *类型转换运算符* → **as** **?** [类型](./03_Types.md#type)
|
||||
>
|
||||
> *类型转换运算符* → **as** **!** [*类型*](./03_Types.md#type)
|
||||
> *类型转换运算符* → **as** **!** [类型](./03_Types.md#type)
|
||||
>
|
||||
|
||||
## 基本表达式 {#primary-expressions}
|
||||
@ -205,25 +206,25 @@ 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)
|
||||
> *基本表达式* → [字面量表达式](#literal-expression)
|
||||
>
|
||||
> *基本表达式* → [*self 表达式*](#self-expression)
|
||||
> *基本表达式* → [self 表达式](#self-expression)
|
||||
>
|
||||
> *基本表达式* → [*父类表达式*](#superclass-expression)
|
||||
> *基本表达式* → [父类表达式](#superclass-expression)
|
||||
>
|
||||
> *基本表达式* → [*闭包表达式*](#closure-expression)
|
||||
> *基本表达式* → [闭包表达式](#closure-expression)
|
||||
>
|
||||
> *基本表达式* → [*圆括号表达式*](#parenthesized-expression)
|
||||
> *基本表达式* → [圆括号表达式](#parenthesized-expression)
|
||||
>
|
||||
> *基本表达式* → [*隐式成员表达式*](#implicit-member-expression)
|
||||
> *基本表达式* → [隐式成员表达式](#implicit-member-expression)
|
||||
>
|
||||
> *基本表达式* → [*通配符表达式*](#wildcard-expression)
|
||||
> *基本表达式* → [通配符表达式](#wildcard-expression)
|
||||
>
|
||||
> *基本表达式* → [*选择器表达式*](#selector-expression)
|
||||
> *基本表达式* → [选择器表达式](#selector-expression)
|
||||
>
|
||||
> *基本表达式* → [*key-path字符串表达式*](#key-patch-string-expression)
|
||||
> *基本表达式* → [key-path字符串表达式](#key-patch-string-expression)
|
||||
>
|
||||
|
||||
### 字面量表达式 {#literal-expression}
|
||||
@ -283,9 +284,9 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者
|
||||
>
|
||||
#### literal-expression {#literal-expression}
|
||||
>
|
||||
> *字面量表达式* → [*字面量*](./02_Lexical_Structure.md#literal)
|
||||
> *字面量表达式* → [字面量](./02_Lexical_Structure.md#literal)
|
||||
>
|
||||
> *字面量表达式* → [*数组字面量*](#array-literal) | [*字典字面量*](#dictionary-literal) | [*练习场字面量*](#playground-literal)
|
||||
> *字面量表达式* → [数组字面量](#array-literal) | [字典字面量](#dictionary-literal) | [练习场字面量](#playground-literal)
|
||||
>
|
||||
> *字面量表达式* → **#file** | **#line** | **#column** | **#function**
|
||||
>
|
||||
@ -294,44 +295,42 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者
|
||||
>
|
||||
#### array-literal {#array-literal}
|
||||
>
|
||||
> *数组字面量* → [[*数组字面量项列表*](#array-literal-items)<sub>可选</sub> **]**
|
||||
> *数组字面量* → [[数组字面量项列表](#array-literal-items)<sub>可选</sub> **]**
|
||||
>
|
||||
>
|
||||
#### array-literal-items {#array-literal-items}
|
||||
>
|
||||
> *数组字面量项列表* → [*数组字面量项*](#array-literal-item) **,**<sub>可选</sub> | [*数组字面量项*](#array-literal-item) **,** [*数组字面量项列表*](#array-literal-items)
|
||||
> *数组字面量项列表* → [数组字面量项](#array-literal-item) **,**<sub>可选</sub> | [数组字面量项](#array-literal-item) **,** [数组字面量项列表](#array-literal-items)
|
||||
>
|
||||
>
|
||||
#### array-literal-item {#array-literal-item}
|
||||
>
|
||||
> *数组字面量项* → [*表达式*](#expression)
|
||||
> *数组字面量项* → [表达式](#expression)
|
||||
>
|
||||
>
|
||||
>
|
||||
#### dictionary-literal {#dictionary-literal}
|
||||
>
|
||||
> *字典字面量* → [[*字典字面量项列表*](#dictionary-literal-items) **]** | **[** **:** **]**
|
||||
> *字典字面量* → [[字典字面量项列表](#dictionary-literal-items) **]** | **[** **:** **]**
|
||||
>
|
||||
>
|
||||
#### dictionary-literal-items {#dictionary-literal-items}
|
||||
>
|
||||
> *字典字面量项列表* → [*字典字面量项*](#dictionary-literal-item) **,**<sub>可选</sub> | [*字典字面量项*](#dictionary-literal-item) **,** [*字典字面量项列表*](#dictionary-literal-items)
|
||||
> *字典字面量项列表* → [字典字面量项](#dictionary-literal-item) **,**<sub>可选</sub> | [字典字面量项](#dictionary-literal-item) **,** [字典字面量项列表](#dictionary-literal-items)
|
||||
>
|
||||
>
|
||||
#### dictionary-literal-item {#dictionary-literal-item}
|
||||
>
|
||||
> *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)。
|
||||
> *字典字面量项* → [表达式](#expression) **:** [表达式](#expression)。
|
||||
>
|
||||
>
|
||||
#### playground-literal {#playground-literal}
|
||||
>
|
||||
> *playground 字面量* → **#colorLiteral ( red : [*表达式*](#expression) , green :[*表达式*](#expression) [*表达式*](#e[*表达式*](#expression) xpression) , blue :[*表达式*](#expression) , alpha : [*表达式*](#expression) )**
|
||||
> *playground 字面量* → **#colorLiteral ( red : [表达式](#expression) , green :[表达式](#expression) [表达式](#e[*表达式*](#expression) xpression) , blue :[表达式](#expression) , alpha : [表达式](#expression) )**
|
||||
>
|
||||
> *playground 字面量* → **#fileLiteral ( resourceName : [*表达式*](#expression) )**
|
||||
>
|
||||
>
|
||||
#### playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**self-expression {#self-expression}
|
||||
> *playground 字面量* → **#fileLiteral ( resourceName : [表达式](#expression) )**
|
||||
>
|
||||
> #### playground 字面量* → **#imageLiteral ( resourceName : [表达式](#expression) )**self-expression {#self-expression}
|
||||
|
||||
### Self 表达式
|
||||
|
||||
@ -376,16 +375,16 @@ struct Point {
|
||||
>
|
||||
|
||||
#### self-expression {#self-expression}
|
||||
> *self 表达式* → **self** | [*self 方法表达式*](#self-method-expression) | [*self 下标表达式*](#self-subscript-expression) | [*self 构造器表达式*](#self-initializer-expression)
|
||||
> *self 表达式* → **self** | [self 方法表达式](#self-method-expression) | [self 下标表达式](#self-subscript-expression) | [self 构造器表达式](#self-initializer-expression)
|
||||
>
|
||||
>
|
||||
|
||||
#### self-method-expression {#self-method-expression}
|
||||
> *self 方法表达式* → **self** **.** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *self 方法表达式* → **self** **.** [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
#### self-subscript-expression {#self-subscript-expression}
|
||||
> *self 下标表达式* → **self** **[** [*函数调用参数表*](#function-call-argument-list) **]**
|
||||
> *self 下标表达式* → **self** **[** [函数调用参数表](#function-call-argument-list) **]**
|
||||
>
|
||||
|
||||
#### self-initializer-expression {#self-initializer-expression}
|
||||
@ -410,15 +409,15 @@ struct Point {
|
||||
>
|
||||
|
||||
#### superclass-expression {#superclass-expression}
|
||||
> *父类表达式* → [*父类方法表达式*](#superclass-method-expression) | [*父类下标表达式*](#superclass-subscript-expression) | [*父类构造器表达式*](#superclass-initializer-expression)
|
||||
> *父类表达式* → [父类方法表达式](#superclass-method-expression) | [父类下标表达式](#superclass-subscript-expression) | [父类构造器表达式](#superclass-initializer-expression)
|
||||
>
|
||||
|
||||
#### superclass-method-expression {#superclass-method-expression}
|
||||
> *父类方法表达式* → **super** **.** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *父类方法表达式* → **super** **.** [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
#### superclass-subscript-expression {#superclass-subscript-expression}
|
||||
> *父类下标表达式* → **super** [[*函数调用参数表*](#function-call-argument-list) **]**
|
||||
> *父类下标表达式* → **super** [[函数调用参数表](#function-call-argument-list) **]**
|
||||
>
|
||||
|
||||
#### superclass-initializer-expression {#superclass-initializer-expression}
|
||||
@ -530,39 +529,39 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
|
||||
>
|
||||
#### closure-expression {#closure-expression}
|
||||
>
|
||||
> *闭包表达式* → **{** [*闭包签名*](#closure-signature)<sub>可选</sub> [*语句*](#statements) **}**
|
||||
> *闭包表达式* → **{** [闭包签名](#closure-signature)<sub>可选</sub> [语句](#statements) **}**
|
||||
>
|
||||
>
|
||||
>
|
||||
#### 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**
|
||||
> *闭包签名* → [捕获列表](#capture-list) **in**
|
||||
>
|
||||
>
|
||||
>
|
||||
#### capture-list {#capture-list}
|
||||
>
|
||||
>
|
||||
> 捕获列表* → [ [*捕获列表项列表*](#capture-list-items) **]**
|
||||
> 捕获列表* → [ [捕获列表项列表](#capture-list-items) **]**
|
||||
>
|
||||
>
|
||||
#### capture-list-items {#capture-list-items}
|
||||
>
|
||||
> *捕获列表项列表* → [*捕获列表项*](#capture-list-item) | [*捕获列表项*](#capture-list-item) **,** [*捕获列表项列表*](#capture-list-items)
|
||||
> *捕获列表项列表* → [捕获列表项](#capture-list-item) | [捕获列表项](#capture-list-item) **,** [捕获列表项列表](#capture-list-items)
|
||||
>
|
||||
>
|
||||
#### capture-list-item {#capture-list-item}
|
||||
>
|
||||
> *捕获列表项* → [*捕获说明符*](#capture-specifier)<sub>可选</sub> [*表达式*](#expression)
|
||||
> *捕获列表项* → [捕获说明符](#capture-specifier)<sub>可选</sub> [表达式](#expression)
|
||||
>
|
||||
>
|
||||
#### capture-specifier {#capture-specifier}
|
||||
@ -587,7 +586,7 @@ x = .AnotherValue
|
||||
>
|
||||
|
||||
#### implicit-member-expression {#implicit-member-expression}
|
||||
> *隐式成员表达式* → **.** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *隐式成员表达式* → **.** [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
### 圆括号表达式 {#parenthesized-expression}
|
||||
@ -597,7 +596,7 @@ x = .AnotherValue
|
||||
>
|
||||
|
||||
#### parenthesized-expression {#parenthesized-expression}
|
||||
> *圆括号表达式* → **( [*表达式*](#expression) )**
|
||||
> *圆括号表达式* → **( [表达式](#expression) )**
|
||||
>
|
||||
|
||||
### 元组表达式 {#Tuple-Expression}
|
||||
@ -606,6 +605,8 @@ x = .AnotherValue
|
||||
> (`标识符 1` : `表达式 1`, `标识符 2` : `表达式 2`, `...`)
|
||||
>
|
||||
|
||||
元组表达式里的每一个标识符在表达式作用域里必须是唯一的。在嵌套的元组表达式中,同嵌套层级里的标识符也必须是唯一的。例如,`(a: 10, a: 20)` 是不合法的,因为标签 `a` 在同一层级出现了两次。然而,`(a: 10, b: (a: 1, x: 2))` 是合法的,尽管 `a` 出现了两次,但有一次在外层元组里,一次在内层元组里。
|
||||
|
||||
元组表达式可以一个表达式都没有,也可以包含两个或是更多的表达式。单个表达式用括号括起来就是括号表达式了。
|
||||
|
||||
> 注意
|
||||
@ -619,15 +620,15 @@ x = .AnotherValue
|
||||
>
|
||||
|
||||
#### tuple-expression {#tuple-expression}
|
||||
> *元组表达式* → **( )** | **(**[*元组元素*](#tuple-element), [*元组元素列表*](#tuple-element-list) **)**
|
||||
> *元组表达式* → **( )** | **(**[元组元素](#tuple-element), [元组元素列表](#tuple-element-list) **)**
|
||||
>
|
||||
|
||||
#### tuple-element-list {#tuple-element-list}
|
||||
> *元组元素列表* → [*元组元素*](#tuple-element) | [*元组元素*](#tuple-element) **,** [*元组元素列表*](#tuple-element-list)
|
||||
> *元组元素列表* → [元组元素](#tuple-element) | [元组元素](#tuple-element) **,** [元组元素列表](#tuple-element-list)
|
||||
>
|
||||
|
||||
#### tuple-element {#tuple-element}
|
||||
> *元组元素* → [*表达式*](#expression) | [*标识符*](identifier) **:** [*表达式*](#expression)
|
||||
> *元组元素* → [表达式](#expression) | [标识符](identifier) **:** [表达式](#expression)
|
||||
>
|
||||
|
||||
### 通配符表达式 {#wildcard-expression}
|
||||
@ -852,11 +853,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}
|
||||
@ -926,25 +927,25 @@ print(keyPath == c.getSomeKeyPath())
|
||||
>
|
||||
|
||||
#### postfix-expression {#postfix-expression}
|
||||
> *后缀表达式* → [*基本表达式*](#primary-expression)
|
||||
> *后缀表达式* → [基本表达式](#primary-expression)
|
||||
>
|
||||
> *后缀表达式* → [*后缀表达式*](#postfix-expression) [*后缀运算符*](02-Lexical-Structure.md#postfix-operator)
|
||||
> *后缀表达式* → [后缀表达式](#postfix-expression) [后缀运算符](02-Lexical-Structure.md#postfix-operator)
|
||||
>
|
||||
> *后缀表达式* → [*函数调用表达式*](#function-call-expression)
|
||||
> *后缀表达式* → [函数调用表达式](#function-call-expression)
|
||||
>
|
||||
> *后缀表达式* → [*构造器表达式*](#initializer-expression)
|
||||
> *后缀表达式* → [构造器表达式](#initializer-expression)
|
||||
>
|
||||
> *后缀表达式* → [*显式成员表达式*](#explicit-member-expression)
|
||||
> *后缀表达式* → [显式成员表达式](#explicit-member-expression)
|
||||
>
|
||||
> *后缀表达式* → [*后缀 self 表达式*](#postfix-self-expression)
|
||||
> *后缀表达式* → [后缀 self 表达式](#postfix-self-expression)
|
||||
>
|
||||
> *后缀表达式* → [*dynamicType 表达式*](#dynamic-type-expression)
|
||||
> *后缀表达式* → [dynamicType 表达式](#dynamic-type-expression)
|
||||
>
|
||||
> *后缀表达式* → [*下标表达式*](#subscript-expression)
|
||||
> *后缀表达式* → [下标表达式](#subscript-expression)
|
||||
>
|
||||
> *后缀表达式* → [*强制取值表达式*](#forced-value-expression)
|
||||
> *后缀表达式* → [强制取值表达式](#forced-value-expression)
|
||||
>
|
||||
> *后缀表达式* → [*可选链表达式*](#optional-chaining-expression)
|
||||
> *后缀表达式* → [可选链表达式](#optional-chaining-expression)
|
||||
>
|
||||
|
||||
### 函数调用表达式 {#function-call-expression}
|
||||
@ -981,15 +982,15 @@ myData.someMethod {$0 == 13}
|
||||
>
|
||||
#### function-call-expression {#function-call-expression}
|
||||
>
|
||||
> *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause)
|
||||
> *函数调用表达式* → [后缀表达式](#postfix-expression) [函数调用参数子句](#function-call-argument-clause)
|
||||
>
|
||||
> *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause)<sub>可选</sub> [*尾随闭包*](#trailing-closure)
|
||||
> *函数调用表达式* → [后缀表达式](#postfix-expression) [函数调用参数子句](#function-call-argument-clause)<sub>可选</sub> [尾随闭包](#trailing-closure)
|
||||
>
|
||||
>
|
||||
>
|
||||
#### function-call-argument-clause {#function-call-argument-clause}
|
||||
>
|
||||
> *函数调用参数子句* → **(** **)** | **(** [*函数调用参数表*](#function-call-argument-list) **)**
|
||||
> *函数调用参数子句* → **(** **)** | **(** [函数调用参数表](#function-call-argument-list) **)**
|
||||
>
|
||||
>
|
||||
#### function-call-argument-list {#function-call-argument-list}
|
||||
@ -999,15 +1000,15 @@ 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)
|
||||
>
|
||||
>
|
||||
>
|
||||
#### trailing-closure {#trailing-closure}
|
||||
>
|
||||
> *尾随闭包* → [*闭包表达式*](#closure-expression)
|
||||
> *尾随闭包* → [闭包表达式](#closure-expression)
|
||||
>
|
||||
|
||||
### 构造器表达式 {#initializer-expression}
|
||||
@ -1051,9 +1052,9 @@ let s3 = someValue.dynamicType.init(data: 7) // 有效
|
||||
>
|
||||
|
||||
#### initializer-expression {#initializer-expression}
|
||||
> *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init**
|
||||
> *构造器表达式* → [后缀表达式](#postfix-expression) **.** **init**
|
||||
>
|
||||
> *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** **(** [*参数名称*](#argument-names) **)**
|
||||
> *构造器表达式* → [后缀表达式](#postfix-expression) **.** **init** **(** [参数名称](#argument-names) **)**
|
||||
>
|
||||
|
||||
### 显式成员表达式 {#explicit-member-expression}
|
||||
@ -1116,19 +1117,17 @@ 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) **(** [*参数名称*](#argument-names) **)**
|
||||
> *显式成员表达式* → [后缀表达式](#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) **)**
|
||||
|
||||
#### argument-names {#argument-names}
|
||||
> *参数名称* → [*参数名*](#argument-name) [*参数名称*](#argument-names)<sub>可选</sub><br/>
|
||||
>
|
||||
> *参数名称* → [参数名](#argument-name) [参数名称](#argument-names)<sub>可选</sub><br/>
|
||||
|
||||
#### argument-name {#argument-name}
|
||||
> *参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) **:**
|
||||
> *参数名* → [标识符](./02_Lexical_Structure.md#identifier) **:**
|
||||
>
|
||||
|
||||
### 后缀 self 表达式 {#postfix-self-expression}
|
||||
@ -1147,7 +1146,7 @@ let x = [10, 3, 20, 15, 4]
|
||||
>
|
||||
|
||||
#### postfix-self-expression {#postfix-self-expression}
|
||||
> *后缀 self 表达式* → [*后缀表达式*](#postfix-expression) **.** **self**
|
||||
> *后缀 self 表达式* → [后缀表达式](#postfix-expression) **.** **self**
|
||||
>
|
||||
|
||||
|
||||
@ -1165,7 +1164,7 @@ let x = [10, 3, 20, 15, 4]
|
||||
>
|
||||
|
||||
#### subscript-expression {#subscript-expression}
|
||||
> *下标表达式* → [*后缀表达式*](#postfix-expression) **[** [*表达式列表*](#expression-list) **]**
|
||||
> *下标表达式* → [后缀表达式](#postfix-expression) **[** [表达式列表](#expression-list) **]**
|
||||
>
|
||||
|
||||
### 强制取值表达式 {#forced-Value-expression}
|
||||
@ -1192,7 +1191,7 @@ someDictionary["a"]![0] = 100
|
||||
>
|
||||
|
||||
#### forced-value-expression {#forced-value-expression}
|
||||
> *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!**
|
||||
> *强制取值表达式* → [后缀表达式](#postfix-expression) **!**
|
||||
>
|
||||
|
||||
### 可选链表达式 {#optional-chaining-expression}
|
||||
@ -1244,5 +1243,4 @@ someDictionary["a"]?[0] = someFunctionWithSideEffects()
|
||||
>
|
||||
|
||||
#### optional-chaining-expression {#optional-chaining-expression}
|
||||
> *可选链表达式* → [*后缀表达式*](#postfix-expression) **?**
|
||||
>
|
||||
> *可选链表达式* → [后缀表达式](#postfix-expression) **?**
|
||||
|
||||
@ -8,42 +8,41 @@
|
||||
|
||||
> 语句语法
|
||||
>
|
||||
> *语句* → [*表达式*](./04_Expressions.md#expression) **;**<sub>可选</sub>
|
||||
> *语句* → [表达式](./04_Expressions.md#expression) **;**<sub>可选</sub>
|
||||
>
|
||||
> *语句* → [*声明*](./06_Declarations.md#declaration) **;**<sub>可选</sub>
|
||||
> *语句* → [声明](./06_Declarations.md#declaration) **;**<sub>可选</sub>
|
||||
>
|
||||
> *语句* → [*循环语句*](#loop-statement) **;**<sub>可选</sub>
|
||||
> *语句* → [循环语句](#loop-statement) **;**<sub>可选</sub>
|
||||
>
|
||||
> *语句* → [*分支语句*](#branch-statement) **;**<sub>可选</sub>
|
||||
> *语句* → [分支语句](#branch-statement) **;**<sub>可选</sub>
|
||||
>
|
||||
> *语句* → [*带标签的语句*](#labeled-statement) **;**<sub>可选</sub>
|
||||
> *语句* → [带标签的语句](#labeled-statement) **;**<sub>可选</sub>
|
||||
>
|
||||
> *语句* → [*控制转移语句*](#control-transfer-statement) **;**<sub>可选</sub>
|
||||
> *语句* → [控制转移语句](#control-transfer-statement) **;**<sub>可选</sub>
|
||||
>
|
||||
> *语句* → [*defer 语句*](#defer-statement) **;**<sub>可选</sub>
|
||||
> *语句* → [defer 语句](#defer-statement) **;**<sub>可选</sub>
|
||||
>
|
||||
> *语句* → [*do 语句*](#do-statement) **:**<sub>可选</sub>
|
||||
> *语句* → [do 语句](#do-statement) **:**<sub>可选</sub>
|
||||
>
|
||||
> *语句* → [*编译器控制语句*](#compiler-control-statement)
|
||||
>
|
||||
> *多条语句* → [*语句*](#statement) [*多条语句*](#statements)<sub>可选</sub>
|
||||
> *语句* → [编译器控制语句](#compiler-control-statement)
|
||||
>
|
||||
> *多条语句* → [语句](#statement) [多条语句](#statements)<sub>可选</sub>
|
||||
|
||||
## 循环语句 {#loop-statements}
|
||||
循环语句会根据特定的循环条件来重复执行代码块。Swift 提供三种类型的循环语句:`for-in` 语句、`while` 语句和 `repeat-while` 语句。
|
||||
|
||||
通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break-statement) 和 [Continue 语句](#continue-statement)。
|
||||
通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参阅 [Break 语句](#break-statement) 和 [Continue 语句](#continue-statement)。
|
||||
|
||||
> 循环语句语法
|
||||
>
|
||||
>
|
||||
|
||||
#### loop-statement {#loop-statement}
|
||||
> *循环语句* → [*for-in 语句*](#for-in-statement)
|
||||
> *循环语句* → [for-in 语句](#for-in-statement)
|
||||
>
|
||||
> *循环语句* → [*while 语句*](#while-statement)
|
||||
> *循环语句* → [while 语句](#while-statement)
|
||||
>
|
||||
> *循环语句* → [*repeat-while 语句*](#repeat-while-statement)
|
||||
> *循环语句* → [repeat-while 语句](#repeat-while-statement)
|
||||
>
|
||||
|
||||
### For-In 语句 {#for-in-statements}
|
||||
@ -65,7 +64,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}
|
||||
@ -93,25 +92,25 @@ while condition {
|
||||
>
|
||||
|
||||
#### 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}
|
||||
> *条件* → [*表达式*](./04_Expressions.md#expression) |[*可用性条件*](#availability-condition) | [*case 条件*](#case-condition) | [*可选绑定条件*](#optional-binding-condition)
|
||||
> *条件* → [表达式](./04_Expressions.md#expression) |[可用性条件](#availability-condition) | [case 条件](#case-condition) | [可选绑定条件](#optional-binding-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}
|
||||
@ -139,7 +138,7 @@ repeat {
|
||||
>
|
||||
|
||||
#### 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}
|
||||
@ -152,11 +151,11 @@ repeat {
|
||||
>
|
||||
|
||||
#### branch-statement {#branch-statement}
|
||||
> *分支语句* → [*if 语句*](#if-statement)
|
||||
> *分支语句* → [if 语句](#if-statement)
|
||||
>
|
||||
> *分支语句* → [*guard 语句*](#guard-statement)
|
||||
> *分支语句* → [guard 语句](#guard-statement)
|
||||
>
|
||||
> *分支语句* → [*switch 语句*](#switch-statement)
|
||||
> *分支语句* → [switch 语句](#switch-statement)
|
||||
>
|
||||
|
||||
### If 语句 {#if-statements}
|
||||
@ -201,12 +200,11 @@ if condition 1 {
|
||||
>
|
||||
|
||||
#### 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}
|
||||
> *else 子句* → **else** [*代码块*](./06_Declarations.md#code-block) | **else** [*if 语句*](#if-statement)
|
||||
>
|
||||
> *else 子句* → **else** [代码块](./06_Declarations.md#code-block) | **else** [if 语句](#if-statement)
|
||||
|
||||
### Guard 语句 {#guard-statements}
|
||||
如果一个或者多个条件不成立,可用 `guard` 语句来退出当前作用域。
|
||||
@ -237,10 +235,10 @@ guard condition else {
|
||||
>
|
||||
|
||||
#### 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}
|
||||
|
||||
`switch` 语句会根据控制表达式的值来决定执行哪部分代码。
|
||||
|
||||
`switch` 语句的形式如下:
|
||||
@ -280,7 +278,7 @@ case let (x, y) where x == y:
|
||||
在 Swift 中,`switch` 语句中控制表达式的每一个可能的值都必须至少有一个 `case` 与之对应。在某些无法面面俱到的情况下(例如,表达式的类型是 `Int`),你可以使用 `default` 分支满足该要求。
|
||||
|
||||
#### 对未来枚举的 `case` 进行 `switch` {#future-case}
|
||||
非冻结枚举(`nonfronzen enumeration`)是一种特殊的枚举类型,它可能在未来会增加新的枚举 `case`,即使这时候你已经编译并且发布了你的应用,所以在 switch 非冻结枚举前需要深思熟虑。当一个库的作者们把一个枚举标记为非冻结的,这意味着他们保留了增加新的枚举 `case` 的权利,并且任何和这个枚举交互的代码都要在不需要重新编译的条件下能够处理那些未来可能新加入的 `case` 。只有那些标准库,比如用 Swift 实现的苹果的一些框架,C 以及 Objective-C 代码才能够声明非冻结枚举。你在 Swift 中声明的枚举不能是非冻结的。
|
||||
非冻结枚举(`nonfronzen enumeration`)是一种特殊的枚举类型,它可能在未来会增加新的枚举 `case`,即使这时候你已经编译并且发布了你的应用,所以在 switch 非冻结枚举前需要深思熟虑。当一个库的作者们把一个枚举标记为非冻结的,这意味着他们保留了增加新的枚举 `case` 的权利,并且任何和这个枚举交互的代码都*必须*在无需重新编译的条件下能够处理那些未来可能新加入的 `case` 。只有演进模式的库代码、标准库代码、用 Swift 实现的 Apple 框架、C 以及 Objective-C 代码才能够声明非冻结枚举。更多关于冻结和非冻结枚举的内容,请参阅 [冻结](./07_Attributes.md#frozen)。
|
||||
|
||||
当你对未来枚举进行 switch 时,你总是需要有一个 `default case`,即使每种枚举类型都已经有对应的 `case` 了。你可以在 default 前标注 `@unknown`,意思是这个 `case` 应该只匹配未来加入的枚举 `case`。如果你的 `default case` 中匹配了任何在编译时就能确定的枚举 `case`,Swift 会抛出一个警告。这可以很好地提醒你库的作者已经新增了一种 `case`,并且你还没有去处理。
|
||||
|
||||
@ -310,58 +308,58 @@ case .suppressed:
|
||||
>
|
||||
|
||||
#### switch-statement {#switch-statement}
|
||||
> *switch 语句* → **switch** [*表达式*](./04_Expressions.md#expression) **{** [*switch-case 列表*](#switch-cases)<sub>可选</sub> **}**
|
||||
> *switch 语句* → **switch** [表达式](./04_Expressions.md#expression) **{** [switch-case 列表](#switch-cases)<sub>可选</sub> **}**
|
||||
>
|
||||
|
||||
#### switch-cases {#switch-cases}
|
||||
> *switch case 列表* → [*switch-case*](#switch-case) [*switch-case 列表*](#switch-cases)<sub>可选</sub>
|
||||
> *switch case 列表* → [switch-case](#switch-case) [switch-case 列表](#switch-cases)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### switch-case {#switch-case}
|
||||
> *switch case* → [*case 标签*](#case-label) [*多条语句*](#statements) | [*default 标签*](#default-label) [*多条语句*](#statements) | [*conditional-switch-case*](#conditional-switch-case-label)
|
||||
> *switch case* → [case 标签](#case-label) [多条语句](#statements) | [default 标签](#default-label) [多条语句](#statements) | [conditional-switch-case](#conditional-switch-case-label)
|
||||
>
|
||||
|
||||
|
||||
#### case-label {#case-label}
|
||||
> *case 标签* → [*属性*](#switch-case-attributes-label)<sub>可选</sub> **case** [*case 项列表*](#case-item-list) **:**
|
||||
> *case 标签* → [属性](#switch-case-attributes-label)<sub>可选</sub> **case** [case 项列表](#case-item-list) **:**
|
||||
>
|
||||
|
||||
#### 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}
|
||||
> *default 标签* → [*属性*](#switch-case-attributes-label)<sub>可选</sub> **default** **:**
|
||||
> *default 标签* → [属性](#switch-case-attributes-label)<sub>可选</sub> **default** **:**
|
||||
>
|
||||
>
|
||||
|
||||
#### where-clause {#where-clause}
|
||||
> *where-clause* → **where** [*where 表达式*](#where-expression)
|
||||
> *where-clause* → **where** [where 表达式](#where-expression)
|
||||
>
|
||||
|
||||
#### where-expression {#where-expression}
|
||||
> *where-expression* → [*表达式*](./04_Expressions.md#expression)
|
||||
> *where-expression* → [表达式](./04_Expressions.md#expression)
|
||||
>
|
||||
>
|
||||
|
||||
#### 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)
|
||||
> *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}
|
||||
> *switch-if-directive 语句* → [*if-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) <sub>可选</sub>
|
||||
> *switch-if-directive 语句* → [if-directive](#switch-case-attributes-label) [compilation-condition](#switch-case-attributes-label) [switch-cases](#switch-case-attributes-label) <sub>可选</sub>
|
||||
>
|
||||
|
||||
#### 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>
|
||||
> *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}
|
||||
> *switch-elseif-directive 语句* → [*elseif-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label)<sub>可选</sub>
|
||||
> *switch-elseif-directive 语句* → [elseif-directive](#switch-case-attributes-label) [compilation-condition](#switch-case-attributes-label) [switch-cases](#switch-case-attributes-label)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### 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>
|
||||
> *switch-else-directive 语句* → [else-directive](#switch-case-attributes-label) [switch-cases](#switch-case-attributes-label) <sub>可选</sub>
|
||||
>
|
||||
|
||||
## 带标签的语句 {#labeled-statements}
|
||||
@ -376,21 +374,21 @@ case .suppressed:
|
||||
>
|
||||
|
||||
#### labeled-statement {#labeled-statement}
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#grammar-loop-statement)
|
||||
> *带标签的语句* → [语句标签](#statement-label) [循环语句](#grammar-loop-statement)
|
||||
>
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*if 语句*](#if-statement)
|
||||
> *带标签的语句* → [语句标签](#statement-label) [if 语句](#if-statement)
|
||||
>
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*switch 语句*](#switch-statement)
|
||||
> *带标签的语句* → [语句标签](#statement-label) [switch 语句](#switch-statement)
|
||||
>
|
||||
> > *带标签的语句* → [*语句标签*](#statement-label) [*do 语句*](#sdo-statement)
|
||||
> > *带标签的语句* → [语句标签](#statement-label) [do 语句](#sdo-statement)
|
||||
>
|
||||
|
||||
#### statement-label {#statement-label}
|
||||
> *语句标签* → [*标签名称*](#label-name) **:**
|
||||
> *语句标签* → [标签名称](#label-name) **:**
|
||||
>
|
||||
|
||||
#### label-name {#label-name}
|
||||
> *标签名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *标签名称* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 控制转移语句 {#control-transfer-statements}
|
||||
@ -401,15 +399,15 @@ case .suppressed:
|
||||
>
|
||||
|
||||
#### control-transfer-statement {#control-transfer-statement}
|
||||
> *控制转移语句* → [*break 语句*](#break-statement)
|
||||
> *控制转移语句* → [break 语句](#break-statement)
|
||||
>
|
||||
> *控制转移语句* → [*continue 语句*](#continue-statement)
|
||||
> *控制转移语句* → [continue 语句](#continue-statement)
|
||||
>
|
||||
> *控制转移语句* → [*fallthrough 语句*](#fallthrough-statement)
|
||||
> *控制转移语句* → [fallthrough 语句](#fallthrough-statement)
|
||||
>
|
||||
> *控制转移语句* → [*return 语句*](#return-statement)
|
||||
> *控制转移语句* → [return 语句](#return-statement)
|
||||
>
|
||||
> *控制转移语句* → [*throw 语句*](#throw-statement)
|
||||
> *控制转移语句* → [throw 语句](#throw-statement)
|
||||
>
|
||||
|
||||
### Break 语句 {#break-statement}
|
||||
@ -433,7 +431,7 @@ case .suppressed:
|
||||
>
|
||||
|
||||
#### break-statement {#break-statement}
|
||||
> *break 语句* → **break** [*标签名称*](#label-name)<sub>可选</sub>
|
||||
> *break 语句* → **break** [标签名称](#label-name)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### Continue 语句 {#continue-statement}
|
||||
@ -459,7 +457,7 @@ case .suppressed:
|
||||
>
|
||||
|
||||
#### continue-statement {#continue-statement}
|
||||
> *continue 语句* → **continue** [*标签名称*](#label-name)<sub>可选</sub>
|
||||
> *continue 语句* → **continue** [标签名称](#label-name)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### Fallthrough 语句 {#fallthrough-statements}
|
||||
@ -502,7 +500,7 @@ case .suppressed:
|
||||
>
|
||||
|
||||
#### return-statement {#return-statement}
|
||||
> *return 语句* → **return** [*表达式*](./04_Expressions.html#expression)<sub>可选</sub>
|
||||
> *return 语句* → **return** [表达式](./04_Expressions.html#expression)<sub>可选</sub>
|
||||
|
||||
### Throw 语句 {#throw-statements}
|
||||
|
||||
@ -524,7 +522,7 @@ case .suppressed:
|
||||
>
|
||||
|
||||
#### throw-statement {#throw-statement}
|
||||
> *throw 语句* → **throw** [*表达式*](./04_Expressions.md#expression)
|
||||
> *throw 语句* → **throw** [表达式](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
## Defer 语句 {#defer-statements}
|
||||
@ -561,7 +559,7 @@ f()
|
||||
>
|
||||
|
||||
#### defer-statement {#defer-statement}
|
||||
> *延迟语句* → **defer** [*代码块*](./06_Declarations.md#code-block)
|
||||
> *延迟语句* → **defer** [代码块](./06_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
## Do 语句 {#do-statements}
|
||||
@ -593,15 +591,15 @@ do {
|
||||
>
|
||||
|
||||
#### do-statement {#do-statement}
|
||||
> *do 语句* → **do** [*代码块*](./06_Declarations.md#code-block) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
|
||||
> *do 语句* → **do** [代码块](./06_Declarations.md#code-block) [多条 catch 子句](#catch-clauses)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### catch-clauses {#catch-clauses}
|
||||
> *多条 catch 子句* → [*catch 子句*](#catch-clause) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
|
||||
> *多条 catch 子句* → [catch 子句](#catch-clause) [多条 catch 子句](#catch-clauses)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### 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 +610,11 @@ do {
|
||||
>
|
||||
|
||||
#### compiler-control-statement {#compiler-control-statement}
|
||||
> *编译器控制语句* → [*条件编译语句*](#grammar-conditional-compilation-block)
|
||||
> *编译器控制语句* → [条件编译语句](#grammar-conditional-compilation-block)
|
||||
>
|
||||
> *编译器控制语句* → [*线路控制语句*](#line-control-statement)
|
||||
> *编译器控制语句* → [线路控制语句](#line-control-statement)
|
||||
>
|
||||
> *编译器控制语句* → [*诊断语句*](#grammar-diagnostic-statement)
|
||||
> *编译器控制语句* → [诊断语句](#grammar-diagnostic-statement)
|
||||
>
|
||||
|
||||
### 条件编译代码块 {#Conditional-Compilation-Block}
|
||||
@ -642,7 +640,7 @@ statements
|
||||
| `swift()` | `>=` 或 `<` 后跟版本号 |
|
||||
| `compiler()` | `>=` 或 `<` 后跟版本号 |
|
||||
| `canImport()` | 模块名 |
|
||||
| `targetEnvironment()` | 模拟器 |
|
||||
| `targetEnvironment()` | `simulator`,`macCatalyst` |
|
||||
|
||||
在 `swift()` 和 `compiler()` 之后的版本号包含有主版本号,可选副版本号,可选补丁版本号类似,并且用(`.`)来分隔。在比较符和版本号之间不能有空格,版本号与前面的函数相对应,比如 `compiler()` 对应的就是这个编译器的版本号,`swift()` 对应的就是你要编译的 `Swift` 语言的版本号。举个简单的例子,如果你在使用 `Swift 5` 的编译器,想编译 `Swift 4.2` ,可以看下面的例子:
|
||||
|
||||
@ -698,23 +696,23 @@ 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)
|
||||
> *条件编译代码块* → [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>
|
||||
> *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)
|
||||
> *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>
|
||||
> *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>
|
||||
> *else-directive 语句* → [else-directive](#grammar-else-directive) [语句(复数)](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
|
||||
@ -729,47 +727,41 @@ statements to compile if both compilation conditions are false
|
||||
|
||||
|
||||
#### compilation-condition {#compilation-condition}
|
||||
> *编译条件* → [*平台条件*](#grammar-platform-condition)
|
||||
> *编译条件* → [平台条件](#grammar-platform-condition)
|
||||
>
|
||||
> *编译条件* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *编译条件* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
> *编译条件* → [*布尔值字面量*](./02_Lexical_Structure.md#boolean-literal)
|
||||
> *编译条件* → [布尔值字面量](./02_Lexical_Structure.md#boolean-literal)
|
||||
>
|
||||
> *编译条件* → **(** [*编译条件*](#compilation-condition) **)**
|
||||
> *编译条件* → **(** [编译条件](#compilation-condition) **)**
|
||||
>
|
||||
> *编译条件* → **!** [*编译条件*](#compilation-condition)
|
||||
> *编译条件* → **!** [编译条件](#compilation-condition)
|
||||
>
|
||||
> *编译条件* → [*编译条件*](#compilation-condition) **&&** [*编译条件*](#compilation-condition)
|
||||
> *编译条件* → [编译条件](#compilation-condition) **&&** [编译条件](#compilation-condition)
|
||||
>
|
||||
> *编译条件* → [*编译条件*](#compilation-condition) **||** [*编译条件*](#compilation-condition)
|
||||
> *编译条件* → [编译条件](#compilation-condition) **||** [编译条件](#compilation-condition)
|
||||
>
|
||||
|
||||
|
||||
#### grammar-platform-condition {#grammar-platform-condition}
|
||||
|
||||
#### grammar-platform-condition-os {#grammar-platform-condition-os}
|
||||
> *平台条件* → **os ( [*操作系统*](#operating-system) )**
|
||||
>
|
||||
> *平台条件* → **os ( [操作系统](#operating-system) )**
|
||||
|
||||
#### grammar-platform-condition-arch {#grammar-platform-condition-arch}
|
||||
> *平台条件* → **arch ( [*架构*](#architecture) )**
|
||||
>
|
||||
> *平台条件* → **arch ( [架构](#architecture) )**
|
||||
|
||||
#### grammar-platform-condition-swift {#grammar-platform-condition-swift}
|
||||
> *平台条件* → **swift ( >= [*swift 版本*](#swift-version) )** | **swift ( < [*swift 版本*](#swift-version) )**
|
||||
>
|
||||
> *平台条件* → **swift ( >= [swift 版本](#swift-version) )** | **swift ( < [swift 版本](#swift-version) )**
|
||||
|
||||
#### grammar-platform-condition-compiler {#grammar-platform-condition-compiler}
|
||||
> *平台条件* → **compiler ( >= [*swift 版本*](#swift-version) )** | **compiler ( < [*swift 版本*](#swift-version) )**
|
||||
>
|
||||
> *平台条件* → **compiler ( >= [swift 版本](#swift-version) )** | **compiler ( < [swift 版本](#swift-version) )**
|
||||
|
||||
#### grammar-platform-condition-canImport {#grammar-platform-condition-canImport}
|
||||
> *平台条件* → **canImport ( [*模块名*](#grammar-module-name) )**
|
||||
>
|
||||
> *平台条件* → **canImport ( [模块名](#grammar-module-name) )**
|
||||
|
||||
#### grammar-platform-condition-targetEnvironment {#grammar-platform-condition-targetEnvironment}
|
||||
> *平台条件* → **targetEnvironment ( [*环境*](#grammar-environment) )**
|
||||
>
|
||||
> *平台条件* → **targetEnvironment ( [环境](#grammar-environment) )**
|
||||
|
||||
#### operating-system {#operating-system}
|
||||
> *操作系统* → **macOS** | **iOS** | **watchOS** | **tvOS**
|
||||
@ -780,18 +772,16 @@ 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>
|
||||
>
|
||||
> *swift 版本延续* → **.** [十进制数字](./02_Lexical_Structure.md#decimal-digit) [swift 版本延续](#grammar-swift-version-continuation) <sub>可选</sub>
|
||||
|
||||
#### grammar-module-name {#grammar-module-name}
|
||||
> *模块名* → [*identifier*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
> *模块名* → [identifier](./02_Lexical_Structure.md#identifier)
|
||||
|
||||
#### grammar-environment {#grammar-environment}
|
||||
|
||||
> *环境* → **模拟器**
|
||||
>
|
||||
|
||||
@ -814,19 +804,17 @@ statements to compile if both compilation conditions are false
|
||||
#### line-control-statement {#line-control-statement}
|
||||
> 行控制语句语法
|
||||
>
|
||||
>
|
||||
> *行控制语句* → **#sourceLocation(file:[*文件名*](#file-name),line:[*行号*](#line-number))**
|
||||
>
|
||||
> *行控制语句* → **#sourceLocation()**
|
||||
>*行控制语句* → **#sourceLocation(file:[文件名](#file-name),line:[行号](#line-number))**
|
||||
>
|
||||
> *行控制语句* → **#sourceLocation( )**
|
||||
|
||||
#### line-number {#line-number}
|
||||
|
||||
> *行号* → 大于 0 的十进制整数
|
||||
>
|
||||
|
||||
#### file-name {#file-name}
|
||||
> *文件名* → [*静态字符串字面量*](./02_Lexical_Structure.md#static-string-literal)
|
||||
>
|
||||
> *文件名* → [静态字符串字面量](./02_Lexical_Structure.md#static-string-literal)
|
||||
|
||||
### 编译时诊断语句 {#compile-time-diagnostic-statement}
|
||||
|
||||
@ -844,12 +832,11 @@ 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) **)**
|
||||
> *诊断语句* → **#error** **(** [诊断消息](#grammar-diagnostic-message) **)**
|
||||
>
|
||||
> *诊断语句* → **#warning** **(** [*diagnostic-message*](#grammar-diagnostic-message) **)**
|
||||
>
|
||||
> *诊断语句* → [*静态字符串字面量*](./02_Lexical_Structure.md#static-string-literal)
|
||||
> *诊断语句* → **#warning** **(** [诊断消息](#grammar-diagnostic-message) **)**
|
||||
>
|
||||
> *诊断语句* → [静态字符串字面量](./02_Lexical_Structure.md#static-string-literal)
|
||||
|
||||
## 可用性条件 {#availability-condition}
|
||||
|
||||
@ -876,12 +863,10 @@ if #available(platform name version, ..., *) {
|
||||
>
|
||||
|
||||
#### availability-condition {#availability-condition}
|
||||
> *可用性条件* → **#available** **(** [*可用性参数列表*](#availability-arguments) **)**
|
||||
>
|
||||
> *可用性条件* → **#available** **(** [可用性参数列表](#availability-arguments) **)**
|
||||
|
||||
#### availability-arguments {#availability-arguments}
|
||||
> *可用性参数列表* → [*可用性参数*](#availability-argument) | [*可用性参数*](#availability-argument) **,** [*可用性参数列表*](#availability-arguments)
|
||||
>
|
||||
> *可用性参数列表* → [可用性参数](#availability-argument) | [可用性参数](#availability-argument) **,** [可用性参数列表](#availability-arguments)
|
||||
|
||||
#### availability-argument {#availability-argument}
|
||||
> *可用性参数* → [平台名称](#platform-name) [平台版本](#platform-version)
|
||||
@ -906,4 +891,3 @@ if #available(platform name 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)
|
||||
>
|
||||
|
||||
@ -9,38 +9,38 @@
|
||||
>
|
||||
#### declaration {#declaration}
|
||||
>
|
||||
> *声明* → [*导入声明*](#import-declaration)
|
||||
> *声明* → [导入声明](#import-declaration)
|
||||
>
|
||||
> *声明* → [*常量声明*](#constant-declaration)
|
||||
> *声明* → [常量声明](#constant-declaration)
|
||||
>
|
||||
> *声明* → [*变量声明*](#variable-declaration)
|
||||
> *声明* → [变量声明](#variable-declaration)
|
||||
>
|
||||
> *声明* → [*类型别名声明*](#typealias-declaration)
|
||||
> *声明* → [类型别名声明](#typealias-declaration)
|
||||
>
|
||||
> *声明* → [*函数声明*](#function-declaration)
|
||||
> *声明* → [函数声明](#function-declaration)
|
||||
>
|
||||
> *声明* → [*枚举声明*](#enum-declaration)
|
||||
> *声明* → [枚举声明](#enum-declaration)
|
||||
>
|
||||
> *声明* → [*结构体声明*](#struct-declaration)
|
||||
> *声明* → [结构体声明](#struct-declaration)
|
||||
>
|
||||
> *声明* → [*类声明*](#class-declaration)
|
||||
> *声明* → [类声明](#class-declaration)
|
||||
>
|
||||
> *声明* → [*协议声明*](#protocol-declaration)
|
||||
> *声明* → [协议声明](#protocol-declaration)
|
||||
>
|
||||
> *声明* → [*构造器声明*](#initializer-declaration)
|
||||
> *声明* → [构造器声明](#initializer-declaration)
|
||||
>
|
||||
> *声明* → [*析构器声明*](#deinitializer-declaration)
|
||||
> *声明* → [析构器声明](#deinitializer-declaration)
|
||||
>
|
||||
> *声明* → [*扩展声明*](#extension-declaration)
|
||||
> *声明* → [扩展声明](#extension-declaration)
|
||||
>
|
||||
> *声明* → [*下标声明*](#subscript-declaration)
|
||||
> *声明* → [下标声明](#subscript-declaration)
|
||||
>
|
||||
> *声明* → [*运算符声明*](#operator-declaration)
|
||||
> *声明* → [运算符声明](#operator-declaration)
|
||||
>
|
||||
>
|
||||
#### declarations {#declarations}
|
||||
>
|
||||
> *多条声明* → [*声明*](#declaration) [*多条声明*](#declarations)<sub>可选</sub>
|
||||
> *多条声明* → [声明](#declaration) [多条声明](#declarations)<sub>可选</sub>
|
||||
>
|
||||
|
||||
## 顶级代码 {#top-level-code}
|
||||
@ -48,7 +48,7 @@ Swift 的源文件中的顶级代码(top-level code)由零个或多个语句
|
||||
|
||||
> 顶级声明语法
|
||||
>
|
||||
> *顶级声明* → [*多条语句*](./05_Statements.md#statements)<sub>可选</sub>
|
||||
> *顶级声明* → [多条语句](./05_Statements.md#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
## 代码块 {#code-blocks}
|
||||
@ -67,7 +67,7 @@ Swift 的源文件中的顶级代码(top-level code)由零个或多个语句
|
||||
>
|
||||
#### code-block {#code-block}
|
||||
>
|
||||
> *代码块* → **{** [*多条语句*](./05_Statements.md#statements)<sub>可选</sub> **}**
|
||||
> *代码块* → **{** [多条语句](./05_Statements.md#statements)<sub>可选</sub> **}**
|
||||
>
|
||||
|
||||
## 导入声明 {#import-declaration}
|
||||
@ -91,7 +91,7 @@ import 模块.子模块
|
||||
>
|
||||
#### import-declaration {#import-declaration}
|
||||
>
|
||||
> *导入声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **import** [*导入类型*](#import-kind)<sub>可选</sub> [*导入路径*](#import-path)
|
||||
> *导入声明* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **import** [导入类型](#import-kind)<sub>可选</sub> [导入路径](#import-path)
|
||||
>
|
||||
>
|
||||
#### import-kind {#import-kind}
|
||||
@ -101,12 +101,12 @@ import 模块.子模块
|
||||
>
|
||||
#### import-path {#import-path}
|
||||
>
|
||||
> *导入路径* → [*导入路径标识符*](#import-path-identifier) | [*导入路径标识符*](#import-path-identifier) **.** [*导入路径*](#import-path)
|
||||
> *导入路径* → [导入路径标识符](#import-path-identifier) | [导入路径标识符](#import-path-identifier) **.** [导入路径](#import-path)
|
||||
>
|
||||
>
|
||||
#### 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}
|
||||
@ -148,22 +148,22 @@ print("The second number is \(secondNumber).")
|
||||
>
|
||||
#### constant-declaration {#constant-declaration}
|
||||
>
|
||||
> *常量声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **let** [*模式构造器列表*](pattern-initializer-list)
|
||||
> *常量声明* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> [声明修饰符列表](#declaration-modifiers)<sub>可选</sub> **let** [模式构造器列表](pattern-initializer-list)
|
||||
>
|
||||
>
|
||||
#### pattern-initializer-list {#pattern-initializer-list}
|
||||
>
|
||||
> *模式构造器列表* → [*模式构造器*](#pattern-initializer) | [*模式构造器*](#pattern-initializer) **,** [*模式构造器列表*](#pattern-initializer-list)
|
||||
> *模式构造器列表* → [模式构造器](#pattern-initializer) | [模式构造器](#pattern-initializer) **,** [模式构造器列表](#pattern-initializer-list)
|
||||
>
|
||||
>
|
||||
#### pattern-initializer {#pattern-initializer}
|
||||
>
|
||||
> *模式构造器* → [*模式*](./08_Patterns.md#pattern) [*构造器*](#initializer)<sub>可选</sub>
|
||||
> *模式构造器* → [模式](./08_Patterns.md#pattern) [构造器](#initializer)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### initializer {#initializer}
|
||||
>
|
||||
> *构造器* → **=** [*表达式*](./04_Expressions.md#expression)
|
||||
> *构造器* → **=** [表达式](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
## 变量声明 {#variable-declaration}
|
||||
@ -257,85 +257,85 @@ var 变量名称: 类型 = 表达式 {
|
||||
>
|
||||
|
||||
#### variable-declaration {#variable-declaration}
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*模式构造器列表*](#pattern-initializer-list)
|
||||
> *变量声明* → [变量声明头](#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) [构造器](#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)
|
||||
>
|
||||
|
||||
|
||||
#### variable-declaration-head {#variable-declaration-head}
|
||||
> *变量声明头* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **var**
|
||||
> *变量声明头* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> [声明修饰符列表](#declaration-modifiers)<sub>可选</sub> **var**
|
||||
>
|
||||
>
|
||||
#### variable-name {#variable-name}
|
||||
>
|
||||
> *变量名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *变量名称* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
|
||||
#### getter-setter-block {#getter-setter-block}
|
||||
> *getter-setter 代码块* → [*代码块*](#code-block)
|
||||
> *getter-setter 代码块* → [代码块](#code-block)
|
||||
>
|
||||
> *getter-setter 代码块* → **{** [*getter 子句*](#getter-clause) [*setter 子句*](#setter-clause)<sub>可选</sub> **}**
|
||||
> *getter-setter 代码块* → **{** [getter 子句](#getter-clause) [setter 子句](#setter-clause)<sub>可选</sub> **}**
|
||||
>
|
||||
> *getter-setter 代码块* → **{** [*setter 子句*](#setter-clause) [*getter 子句*](#getter-clause) **}**
|
||||
> *getter-setter 代码块* → **{** [setter 子句](#setter-clause) [getter 子句](#getter-clause) **}**
|
||||
>
|
||||
>
|
||||
#### getter-clause {#getter-clause}
|
||||
>
|
||||
> *getter 子句* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **get** [*代码块*](#code-block)
|
||||
> *getter 子句* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **get** [代码块](#code-block)
|
||||
>
|
||||
>
|
||||
#### setter-clause {#setter-clause}
|
||||
>
|
||||
> *setter 子句* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **set** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
> *setter 子句* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **set** [setter 名称](#setter-name)<sub>可选</sub> [代码块](#code-block)
|
||||
>
|
||||
>
|
||||
#### setter-name {#setter-name}
|
||||
>
|
||||
> *setter 名称* → **(** [*标识符*](./02_Lexical_Structure.md#identifier) **)**
|
||||
> *setter 名称* → **(** [标识符](./02_Lexical_Structure.md#identifier) **)**
|
||||
>
|
||||
|
||||
|
||||
#### getter-setter-keyword-block {#getter-setter-keyword-block}
|
||||
> *getter-setter 关键字代码块* → **{** [*getter 关键字子句*](#getter-keyword-clause) [*setter 关键字子句*](#setter-keyword-clause)<sub>可选</sub> **}**
|
||||
> *getter-setter 关键字代码块* → **{** [getter 关键字子句](#getter-keyword-clause) [setter 关键字子句](#setter-keyword-clause)<sub>可选</sub> **}**
|
||||
>
|
||||
> *getter-setter 关键字代码块* → **{** [*setter 关键字子句*](#setter-keyword-clause) [*getter 关键字子句*](#getter-keyword-clause) **}**
|
||||
> *getter-setter 关键字代码块* → **{** [setter 关键字子句](#setter-keyword-clause) [getter 关键字子句](#getter-keyword-clause) **}**
|
||||
>
|
||||
>
|
||||
#### getter-keyword-clause {#getter-keyword-clause}
|
||||
>
|
||||
> *getter 关键字子句* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **get**
|
||||
> *getter 关键字子句* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **get**
|
||||
>
|
||||
>
|
||||
#### setter-keyword-clause {#setter-keyword-clause}
|
||||
>
|
||||
> *setter 关键字子句* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **set**
|
||||
> *setter 关键字子句* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **set**
|
||||
>
|
||||
|
||||
|
||||
#### willSet-didSet-block {#willSet-didSet-block}
|
||||
> *willSet-didSet 代码块* → **{** [*willSet 子句*](#willSet-clause) [*didSet 子句*](#didSet-clause)<sub>可选</sub> **}**
|
||||
> *willSet-didSet 代码块* → **{** [willSet 子句](#willSet-clause) [didSet 子句](#didSet-clause)<sub>可选</sub> **}**
|
||||
>
|
||||
> *willSet-didSet 代码块* → **{** [*didSet 子句*](#didSet-clause) [*willSet 子句*](#willSet-clause)<sub>可选</sub> **}**
|
||||
> *willSet-didSet 代码块* → **{** [didSet 子句](#didSet-clause) [willSet 子句](#willSet-clause)<sub>可选</sub> **}**
|
||||
>
|
||||
>
|
||||
#### willSet-clause {#willSet-clause}
|
||||
>
|
||||
> *willSet 子句* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **willSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
> *willSet 子句* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **willSet** [setter 名称](#setter-name)<sub>可选</sub> [代码块](#code-block)
|
||||
>
|
||||
>
|
||||
#### didSet-clause {#didSet-clause}
|
||||
>
|
||||
> *didSet 子句* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **didSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
> *didSet 子句* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **didSet** [setter 名称](#setter-name)<sub>可选</sub> [代码块](#code-block)
|
||||
>
|
||||
|
||||
## 类型别名声明 {#type-alias-declaration}
|
||||
@ -396,17 +396,17 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
|
||||
>
|
||||
#### typealias-declaration {#typealias-declaration}
|
||||
>
|
||||
> *类型别名声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **typealias** [*类型别名名称*](#typealias-name) [*类型别子句*](#typealias-clause) [*类型别名赋值*](#typealias-assignment)
|
||||
> *类型别名声明* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **typealias** [类型别名名称](#typealias-name) [类型别子句](#typealias-clause) [类型别名赋值](#typealias-assignment)
|
||||
>
|
||||
>
|
||||
#### typealias-name {#typealias-name}
|
||||
>
|
||||
> *类型别名名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *类型别名名称* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
>
|
||||
#### typealias-assignment {#typealias-assignment}
|
||||
>
|
||||
> *类型别名赋值* → **=** [*类型*](./03_Types.md#type)
|
||||
> *类型别名赋值* → **=** [类型](./03_Types.md#type)
|
||||
>
|
||||
|
||||
## 函数声明 {#function-declaration}
|
||||
@ -594,71 +594,71 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
>
|
||||
|
||||
#### 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>
|
||||
>
|
||||
|
||||
|
||||
#### function-head {#function-head}
|
||||
> *函数头* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **func**
|
||||
> *函数头* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> [声明修饰符列表](#declaration-modifiers)<sub>可选</sub> **func**
|
||||
>
|
||||
>
|
||||
#### function-name {#function-name}
|
||||
>
|
||||
> *函数名* → [*标识符*](./02_Lexical_Structure.md#identifier) | [*运算符*](./02-Lexical-Structure.md#operator)
|
||||
> *函数名* → [标识符](./02_Lexical_Structure.md#identifier) | [运算符](./02-Lexical-Structure.md#operator)
|
||||
>
|
||||
>
|
||||
>
|
||||
#### function-signature {#function-signature}
|
||||
>
|
||||
>
|
||||
> *函数签名* → [*参数子句列表*](#parameter-clauses) **throws**<sub>可选</sub> [*函数结果*](#function-result)<sub>可选</sub>
|
||||
> *函数签名* → [参数子句列表](#parameter-clauses) **throws**<sub>可选</sub> [函数结果](#function-result)<sub>可选</sub>
|
||||
>
|
||||
> *函数签名* → [*参数子句列表*](#parameter-clauses) **rethrows** [*函数结果*](#function-result)<sub>可选</sub>
|
||||
> *函数签名* → [参数子句列表](#parameter-clauses) **rethrows** [函数结果](#function-result)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### 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}
|
||||
>
|
||||
> *函数体* → [*代码块*](#code-block)
|
||||
> *函数体* → [代码块](#code-block)
|
||||
>
|
||||
|
||||
>
|
||||
#### parameter-clause {#parameter-clause}
|
||||
>
|
||||
> *参数子句* → **(** **)** | **(** [*参数列表*](#parameter-list) **)**
|
||||
> *参数子句* → **(** **)** | **(** [参数列表](#parameter-list) **)**
|
||||
>
|
||||
>
|
||||
#### parameter-list {#parameter-list}
|
||||
>
|
||||
> *参数列表* → [*参数*](#parameter) | [*参数*](#parameter) **,** [*参数列表*](#parameter-list)
|
||||
> *参数列表* → [参数](#parameter) | [参数](#parameter) **,** [参数列表](#parameter-list)
|
||||
>
|
||||
>
|
||||
#### 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}
|
||||
>
|
||||
> *默认参数子句* → **=** [*表达式*](./04_Expressions.md#expression)
|
||||
> *默认参数子句* → **=** [表达式](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
## 枚举声明 {#enumeration-declaration}
|
||||
@ -768,83 +768,83 @@ enum GamePlayMode: String {
|
||||
>
|
||||
#### enum-declaration {#enum-declaration}
|
||||
>
|
||||
> *枚举声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> [*联合风格枚举*](#union-style-enum)
|
||||
> *枚举声明* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> [联合风格枚举](#union-style-enum)
|
||||
>
|
||||
> *枚举声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> [*原始值风格枚举*](#raw-value-style-enum)
|
||||
> *枚举声明* → [特性列表](./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}
|
||||
>
|
||||
> *多个联合风格枚举成员* → [*联合风格枚举成员*](#union-style-enum-member) [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub>
|
||||
> *多个联合风格枚举成员* → [联合风格枚举成员](#union-style-enum-member) [多个联合风格枚举成员](#union-style-enum-members)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### 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}
|
||||
>
|
||||
> *联合风格枚举用例子句* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **indirect**<sub>可选</sub> **case** [*联合风格枚举用例列表*](#union-style-enum-case-list)
|
||||
> *联合风格枚举用例子句* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **indirect**<sub>可选</sub> **case** [联合风格枚举用例列表](#union-style-enum-case-list)
|
||||
>
|
||||
>
|
||||
#### union-style-enum-case-list {#union-style-enum-case-list}
|
||||
>
|
||||
> *联合风格枚举用例列表* → [*联合风格枚举用例*](#union-style-enum-case) | [*联合风格枚举用例*](#union-style-enum-case) **,** [*联合风格枚举用例列表*](#union-style-enum-case-list)
|
||||
> *联合风格枚举用例列表* → [联合风格枚举用例](#union-style-enum-case) | [联合风格枚举用例](#union-style-enum-case) **,** [联合风格枚举用例列表](#union-style-enum-case-list)
|
||||
>
|
||||
>
|
||||
#### 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}
|
||||
>
|
||||
> *枚举名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *枚举名称* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
>
|
||||
#### enum-case-name {#enum-case-name}
|
||||
>
|
||||
> *枚举用例名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *枚举用例名称* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
>
|
||||
> #### 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}
|
||||
>
|
||||
> *多个原始值风格枚举成员* → [*原始值风格枚举成员*](#raw-value-style-enum-member) [*多个原始值风格枚举成员*](#raw-value-style-enum-members)<sub>可选</sub>
|
||||
> *多个原始值风格枚举成员* → [原始值风格枚举成员](#raw-value-style-enum-member) [多个原始值风格枚举成员](#raw-value-style-enum-members)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### 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}
|
||||
>
|
||||
> *原始值风格枚举用例子句* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **case** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list)
|
||||
> *原始值风格枚举用例子句* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **case** [原始值风格枚举用例列表](#raw-value-style-enum-case-list)
|
||||
>
|
||||
>
|
||||
#### raw-value-style-enum-case-list {#raw-value-style-enum-case-list}
|
||||
>
|
||||
> *原始值风格枚举用例列表* → [*原始值风格枚举用例*](#raw-value-style-enum-case) | [*原始值风格枚举用例*](#raw-value-style-enum-case) **,** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list)
|
||||
> *原始值风格枚举用例列表* → [原始值风格枚举用例](#raw-value-style-enum-case) | [原始值风格枚举用例](#raw-value-style-enum-case) **,** [原始值风格枚举用例列表](#raw-value-style-enum-case-list)
|
||||
>
|
||||
>
|
||||
#### raw-value-style-enum-case {#raw-value-style-enum-case}
|
||||
>
|
||||
> *原始值风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*原始值赋值*](#raw-value-assignment)<sub>可选</sub>
|
||||
> *原始值风格枚举用例* → [枚举用例名称](#enum-case-name) [原始值赋值](#raw-value-assignment)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### raw-value-assignment {#raw-value-assignment}
|
||||
>
|
||||
> *原始值赋值* → **=** [*原始值字面量*](#raw-value-literal)
|
||||
> *原始值赋值* → **=** [原始值字面量](#raw-value-literal)
|
||||
>
|
||||
>
|
||||
#### raw-value-literal {#raw-value-literal}
|
||||
@ -890,28 +890,28 @@ struct 结构体名称: 采纳的协议 {
|
||||
>
|
||||
#### 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}
|
||||
>
|
||||
> *结构体名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *结构体名称* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
>
|
||||
#### struct-body {#struct-body}
|
||||
>
|
||||
> *结构体主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
|
||||
> *结构体主体* → **{** [多条声明](#declarations)<sub>可选</sub> **}**
|
||||
>
|
||||
>
|
||||
#### struct-name {#struct-name}
|
||||
>
|
||||
>
|
||||
> *结构体多个成员* → [*结构体成员*](#struct-member) [*结构体多个成员*](#struct-members)<sub>可选</sub>
|
||||
> *结构体多个成员* → [结构体成员](#struct-member) [结构体多个成员](#struct-members)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### struct-member {#struct-member}
|
||||
>
|
||||
> *结构体成员* → [*声明*](#declaration) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
> *结构体成员* → [声明](#declaration) | [编译控制流语句](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
|
||||
## 类声明 {#class-declaration}
|
||||
@ -955,27 +955,27 @@ class 类名: 超类, 采纳的协议 {
|
||||
>
|
||||
#### 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}
|
||||
>
|
||||
> *类名* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *类名* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
>
|
||||
#### class-body {#class-body}
|
||||
>
|
||||
> *类主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
|
||||
> *类主体* → **{** [多条声明](#declarations)<sub>可选</sub> **}**
|
||||
>
|
||||
>
|
||||
> *类多个成员* → [*类成员*](#class-member) [*类多个成员*](#class-members)<sub>可选</sub>
|
||||
> *类多个成员* → [类成员](#class-member) [类多个成员](#class-members)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### class-member {#class-member}
|
||||
>
|
||||
> *类成员* → [*声明*](#declaration) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
> *类成员* → [声明](#declaration) | [编译控制流语句](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
|
||||
## 协议声明 {#protocol-declaration}
|
||||
@ -1027,44 +1027,44 @@ protocol SomeProtocol: AnyObject {
|
||||
>
|
||||
#### 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}
|
||||
>
|
||||
> *协议名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *协议名称* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
>
|
||||
#### protocol-body {#protocol-body}
|
||||
>
|
||||
> *协议主体* → **{** [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub> **}**
|
||||
> *协议主体* → **{** [协议成员声明列表](#protocol-member-declarations)<sub>可选</sub> **}**
|
||||
>
|
||||
>
|
||||
> *协议多个成员* → [*协议成员*](#protocol-member) [*协议多个成员*](#protocol-members)<sub>可选</sub>
|
||||
> *协议多个成员* → [协议成员](#protocol-member) [协议多个成员](#protocol-members)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### protocol-member {#protocol-member}
|
||||
>
|
||||
> *协议成员* → [*协议成员声明*](#protocol-member-declaration) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
> *协议成员* → [协议成员声明](#protocol-member-declaration) | [编译控制流语句](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
>
|
||||
>
|
||||
#### protocol-member-declaration {#protocol-member-declaration}
|
||||
>
|
||||
> *协议成员声明* → [*协议属性声明*](#protocol-property-declaration)
|
||||
> *协议成员声明* → [协议属性声明](#protocol-property-declaration)
|
||||
>
|
||||
> *协议成员声明* → [*协议方法声明*](#protocol-method-declaration)
|
||||
> *协议成员声明* → [协议方法声明](#protocol-method-declaration)
|
||||
>
|
||||
> *协议成员声明* → [*协议构造器声明*](#protocol-initializer-declaration)
|
||||
> *协议成员声明* → [协议构造器声明](#protocol-initializer-declaration)
|
||||
>
|
||||
> *协议成员声明* → [*协议下标声明*](#protocol-subscript-declaration)
|
||||
> *协议成员声明* → [协议下标声明](#protocol-subscript-declaration)
|
||||
>
|
||||
> *协议成员声明* → [*协议关联类型声明*](#protocol-associated-type-declaration)
|
||||
> *协议成员声明* → [协议关联类型声明](#protocol-associated-type-declaration)
|
||||
>
|
||||
>
|
||||
#### protocol-member-declarations {#protocol-member-declarations}
|
||||
>
|
||||
> *协议成员声明列表* → [*协议成员声明*](#protocol-member-declaration) [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub>
|
||||
> *协议成员声明列表* → [协议成员声明](#protocol-member-declaration) [协议成员声明列表](#protocol-member-declarations)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### 协议属性声明 {#protocol-property-declaration}
|
||||
@ -1089,7 +1089,7 @@ var 属性名: 类型 { get set }
|
||||
>
|
||||
#### 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}
|
||||
@ -1106,7 +1106,7 @@ var 属性名: 类型 { get set }
|
||||
>
|
||||
#### 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}
|
||||
@ -1126,9 +1126,9 @@ var 属性名: 类型 { get set }
|
||||
>
|
||||
#### 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}
|
||||
@ -1151,7 +1151,7 @@ subscript (参数列表) -> 返回类型 { get set }
|
||||
>
|
||||
#### 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}
|
||||
@ -1184,10 +1184,10 @@ protocol SubProtocolB: SomeProtocol where SomeType: Equatable { }
|
||||
>
|
||||
#### 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}
|
||||
|
||||
构造器声明会为程序中的类、结构体或枚举引入构造器。构造器使用关键字 `init` 来声明,有两种基本形式。
|
||||
|
||||
结构体、枚举、类可以有任意数量的构造器,但是类的构造器具有不同的规则和行为。不同于结构体和枚举,类有两种构造器,即指定构造器和便利构造器,请参阅 [构造过程](../02_language_guide/14_Initialization.md)。
|
||||
@ -1277,23 +1277,23 @@ if let actualInstance = SomeStruct(input: "Hello") {
|
||||
>
|
||||
#### 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}
|
||||
>
|
||||
> *构造器头* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init**
|
||||
> *构造器头* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> [声明修饰符列表](#declaration-modifiers)<sub>可选</sub> **init**
|
||||
>
|
||||
> *构造器头* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **?**
|
||||
> *构造器头* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> [声明修饰符列表](#declaration-modifiers)<sub>可选</sub> **init** **?**
|
||||
>
|
||||
> *构造器头* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **!**
|
||||
> *构造器头* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> [声明修饰符列表](#declaration-modifiers)<sub>可选</sub> **init** **!**
|
||||
>
|
||||
>
|
||||
#### initializer-body {#initializer-body}
|
||||
>
|
||||
> *构造器主体* → [*代码块*](#code-block)
|
||||
> *构造器主体* → [代码块](#code-block)
|
||||
>
|
||||
|
||||
## 析构器声明 {#deinitializer-declaration}
|
||||
@ -1320,7 +1320,7 @@ deinit {
|
||||
>
|
||||
#### deinitializer-declaration {#deinitializer-declaration}
|
||||
>
|
||||
> *析构器声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block)
|
||||
> *析构器声明* → [特性列表](./07_Attributes.md#attributes)<sub>可选</sub> **deinit** [代码块](#code-block)
|
||||
>
|
||||
|
||||
## 扩展声明 {#extension-declaration}
|
||||
@ -1353,9 +1353,11 @@ extension 类型名称: 采纳的协议 where 约束条件 {
|
||||
协议声明不能为现有的类添加类的继承关系,因此你只能在 “类型名称” 的冒号后面添加一系列协议。
|
||||
|
||||
### 条件遵循 {#conditional-conformance}
|
||||
|
||||
你可以扩展一个泛型类型并使其有条件地遵循某协议,此后此类型的实例只有在特定的限制条件满足时才遵循此协议。在扩展声明中加入限制条件来为协议添加条件遵循。
|
||||
|
||||
## 已重写的限制条件会在某些泛型上下文中失效 {#overridden-requirements-aren't-Used-in-some-generic-contexts}
|
||||
|
||||
对于一些通过条件遵循获得了特定行为的类型,在某些泛型上下文中,并不能够确保能够使用协议限制中的特定实现。为了说明这个行为,下面的例子中定义了两个协议以及一个有条件地遵循两个协议的泛型类型。
|
||||
|
||||
```swift
|
||||
@ -1502,16 +1504,16 @@ extension Array: Loggable where Element: MarkedLoggable { }
|
||||
>
|
||||
#### 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> **}**
|
||||
> *扩展主体* → **{** [多条声明](#declarations)<sub>可选</sub> **}**
|
||||
>
|
||||
> *多条声明* → [单条声明](#subscript-declaration) [多条声明](#declarations) <sub>可选</sub>
|
||||
>
|
||||
> *单条声明* → [声明语句](#declarations) | [*编译控制流语句*](05-Statements.md#compiler-control-statement)
|
||||
> *单条声明* → [声明语句](#declarations) | [编译控制流语句](05-Statements.md#compiler-control-statement)
|
||||
>
|
||||
|
||||
## 下标声明 {#subscript-declaration}
|
||||
@ -1557,21 +1559,21 @@ subscript (参数列表) -> 返回类型 {
|
||||
>
|
||||
#### 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}
|
||||
@ -1619,25 +1621,25 @@ postfix operator 运算符名称 {}
|
||||
>
|
||||
|
||||
#### operator-declaration {#operator-declaration}
|
||||
> *运算符声明* → [*前缀运算符声明*](#prefix-operator-declaration) | [*后缀运算符声明*](#postfix-operator-declaration) | [*中缀运算符声明*](#infix-operator-declaration)
|
||||
> *运算符声明* → [前缀运算符声明](#prefix-operator-declaration) | [后缀运算符声明](#postfix-operator-declaration) | [中缀运算符声明](#infix-operator-declaration)
|
||||
>
|
||||
|
||||
#### prefix-operator-declaration {#prefix-operator-declaration}
|
||||
> *前缀运算符声明* → **prefix** **运算符** [*运算符*](./02_Lexical_Structure.md#operator) **{** **}**
|
||||
> *前缀运算符声明* → **prefix** **运算符** [运算符](./02_Lexical_Structure.md#operator) **{** **}**
|
||||
>
|
||||
>
|
||||
#### postfix-operator-declaration {#postfix-operator-declaration}
|
||||
>
|
||||
> *后缀运算符声明* → **postfix** **运算符** [*运算符*](./02_Lexical_Structure.html#operator) **{** **}**
|
||||
> *后缀运算符声明* → **postfix** **运算符** [运算符](./02_Lexical_Structure.html#operator) **{** **}**
|
||||
>
|
||||
>
|
||||
#### infix-operator-declaration {#infix-operator-declaration}
|
||||
>
|
||||
> *中缀运算符声明* → **infix** **运算符** [*运算符*](./02_Lexical_Structure.md#operator) **{** [*中缀运算符属性*](#infix-operator-attributes)<sub>可选</sub> **}**
|
||||
> *中缀运算符声明* → **infix** **运算符** [运算符](./02_Lexical_Structure.md#operator) **{** [中缀运算符属性](#infix-operator-attributes)<sub>可选</sub> **}**
|
||||
>
|
||||
|
||||
#### infix-operator-group {#infix-operator-group}
|
||||
> *中缀运算符组* → [*优先级组名称*](#precedence-group-name)
|
||||
> *中缀运算符组* → [优先级组名称](#precedence-group-name)
|
||||
>
|
||||
|
||||
## 优先级组声明 {#precedence-group-declaration-modifiers}
|
||||
@ -1672,31 +1674,31 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
>
|
||||
|
||||
#### precedence-group-declaration {#precedence-group-declaration}
|
||||
> *优先级组声明* → **precedence**[*优先级组名称*](#precedence-group-name){[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> }
|
||||
> *优先级组声明* → **precedence**[优先级组名称](#precedence-group-name){[多优先级组属性](#precedence-group-attributes)<sub>可选</sub> }
|
||||
>
|
||||
|
||||
#### precedence-group-attributes {#precedence-group-attributes}
|
||||
> *优先级组属性* → [*优先级组属性*](#precedence-group-attribute)[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> **{** **}**
|
||||
> *优先级组属性* → [优先级组属性](#precedence-group-attribute)[多优先级组属性](#precedence-group-attributes)<sub>可选</sub> **{** **}**
|
||||
>
|
||||
|
||||
#### precedence-group-attribute {#precedence-group-attribute}
|
||||
> *优先级组属性* → [*优先级组关系*](#precedence-group-relation)
|
||||
> *优先级组属性* → [优先级组关系](#precedence-group-relation)
|
||||
>
|
||||
> *优先级组属性* → [*优先级组赋值性*](#precedence-group-assignment)
|
||||
> *优先级组属性* → [优先级组赋值性](#precedence-group-assignment)
|
||||
>
|
||||
> *优先级组属性* → [*优先级组相关性*](#precedence-group-associativity)
|
||||
> *优先级组属性* → [优先级组相关性](#precedence-group-associativity)
|
||||
>
|
||||
>
|
||||
#### precedence-group-relation {#precedence-group-relation}
|
||||
>
|
||||
> *优先级组关系* → **higherThan:**[*多优先级组名称*](#precedence-group-names)
|
||||
> *优先级组关系* → **higherThan:**[多优先级组名称](#precedence-group-names)
|
||||
>
|
||||
> *优先级组关系* → **lowerThan:**[*多优先级组名称*](#precedence-group-names)
|
||||
> *优先级组关系* → **lowerThan:**[多优先级组名称](#precedence-group-names)
|
||||
>
|
||||
>
|
||||
#### precedence-group-assignment {#precedence-group-assignment}
|
||||
>
|
||||
> *优先级组赋值* → **assignment:**[*布尔字面值*](./02_Lexical_Structure.md#boolean-literal)
|
||||
> *优先级组赋值* → **assignment:**[布尔字面值](./02_Lexical_Structure.md#boolean-literal)
|
||||
>
|
||||
|
||||
#### precedence-group-associativity {#precedence-group-associativity}
|
||||
@ -1708,11 +1710,11 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
>
|
||||
|
||||
#### precedence-group-names {#precedence-group-names}
|
||||
> *多优先级组名称* → [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name)
|
||||
> *多优先级组名称* → [优先级组名称](#precedence-group-name) | [优先级组名称](#precedence-group-name) | [优先级组名称](#precedence-group-name)
|
||||
>
|
||||
|
||||
#### precedence-group-name {#precedence-group-name}
|
||||
> *优先级组名称* →[*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> *优先级组名称* →[标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 声明修饰符 {#Declaration-Modifiers}
|
||||
@ -1791,13 +1793,12 @@ Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`
|
||||
#### declaration-modifier {#declaration-modifier}
|
||||
> *声明修饰符* → **class** | **convenience**| **dynamic** | **final** | **infix** | **lazy** | **mutating** | **nonmutating** | **optional** | **override** | **postfix** | **prefix** | **required** | **static** | **unowned** | **unowned ( safe )** | **unowned ( unsafe )** | **weak**
|
||||
>
|
||||
> 声明修饰符 → [*访问级别修饰符*](#access-level-modifier)
|
||||
> 声明修饰符 → [访问级别修饰符](#access-level-modifier)
|
||||
>
|
||||
>
|
||||
#### declaration-modifiers {#declaration-modifiers}
|
||||
>
|
||||
> *声明修饰符列表* → [*声明修饰符*](#declaration-modifier) [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub>
|
||||
>
|
||||
> *声明修饰符列表* → [声明修饰符](#declaration-modifier) [声明修饰符列表](#declaration-modifiers)<sub>可选</sub>
|
||||
|
||||
#### access-level-modifier {#access-level-modifier}
|
||||
> 访问级别修饰符 → **internal** | **internal ( set )**
|
||||
|
||||
@ -12,21 +12,21 @@ 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)
|
||||
> *模式* → [值绑定模式](#value-binding-pattern)
|
||||
>
|
||||
> *模式* → [*元组模式*](#tuple-pattern) [*类型注解*](03-Types.md#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [元组模式](#tuple-pattern) [类型注解](03-Types.md#type-annotation)<sub>可选</sub>
|
||||
>
|
||||
> *模式* → [*枚举用例模式*](#enum-case-pattern)
|
||||
> *模式* → [枚举用例模式](#enum-case-pattern)
|
||||
>
|
||||
> *模式* → [*可选模式*](#optional-pattern)
|
||||
> *模式* → [可选模式](#optional-pattern)
|
||||
>
|
||||
> *模式* → [*类型转换模式*](#type-casting-pattern)
|
||||
> *模式* → [类型转换模式](#type-casting-pattern)
|
||||
>
|
||||
> *模式* → [*表达式模式*](#expression-pattern)
|
||||
> *模式* → [表达式模式](#expression-pattern)
|
||||
>
|
||||
|
||||
## 通配符模式(Wildcard Pattern) {#wildcard-pattern}
|
||||
@ -41,8 +41,7 @@ for _ in 1...3 {
|
||||
|
||||
> 通配符模式语法
|
||||
>
|
||||
|
||||
#### wildcard-pattern {#wildcard-pattern}
|
||||
> #### wildcard-pattern {#wildcard-pattern}
|
||||
> *通配符模式* → **_**
|
||||
>
|
||||
|
||||
@ -59,9 +58,8 @@ let someValue = 42
|
||||
|
||||
> 标识符模式语法
|
||||
>
|
||||
|
||||
#### identifier-pattern {#identifier-pattern}
|
||||
> *标识符模式* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
> #### identifier-pattern {#identifier-pattern}
|
||||
> *标识符模式* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 值绑定模式(Value-Binding Pattern) {#value-binding-pattern}
|
||||
@ -83,9 +81,8 @@ case let (x, y):
|
||||
|
||||
> 值绑定模式语法
|
||||
>
|
||||
|
||||
#### value-binding-pattern {#value-binding-pattern}
|
||||
> *值绑定模式* → **var** [*模式*](#pattern) | **let** [*模式*](#pattern)
|
||||
> #### value-binding-pattern {#value-binding-pattern}
|
||||
> *值绑定模式* → **var** [模式](#pattern) | **let** [模式](#pattern)
|
||||
>
|
||||
|
||||
## 元组模式 {#tuple-pattern}
|
||||
@ -113,17 +110,15 @@ let (a): Int = 2 // a: Int = 2
|
||||
|
||||
> 元组模式语法
|
||||
>
|
||||
|
||||
#### tuple-pattern {#tuple-pattern}
|
||||
> *元组模式* → **(** [*元组模式元素列表*](#tuple-pattern-element-list)<sub>可选</sub> **)**
|
||||
> #### tuple-pattern {#tuple-pattern}
|
||||
> *元组模式* → **(** [元组模式元素列表](#tuple-pattern-element-list)<sub>可选</sub> **)**
|
||||
>
|
||||
|
||||
#### tuple-pattern-element-list {#tuple-pattern-element-list}
|
||||
> *元组模式元素列表* → [*元组模式元素*](#tuple-pattern-element) | [*元组模式元素*](#tuple-pattern-element) **,** [*元组模式元素列表*](#tuple-pattern-element-list)
|
||||
> *元组模式元素列表* → [元组模式元素](#tuple-pattern-element) | [元组模式元素](#tuple-pattern-element) **,** [元组模式元素列表](#tuple-pattern-element-list)
|
||||
>
|
||||
|
||||
#### tuple-pattern-element {#tuple-pattern-element}
|
||||
> *元组模式元素* → [*模式*](#pattern)
|
||||
> #### tuple-pattern-element {#tuple-pattern-element}
|
||||
> *元组模式元素* → [模式](#pattern)
|
||||
>
|
||||
|
||||
## 枚举用例模式(Enumeration Case Pattern) {#enumeration-case-pattern}
|
||||
@ -131,11 +126,26 @@ let (a): Int = 2 // a: Int = 2
|
||||
|
||||
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../02_language_guide/08_Enumerations.md#associated-values)。
|
||||
|
||||
枚举用例模式同样会匹配那些被包装成可选值的用例。简化的语法能将可选模式过滤掉。注意,由于 `Optional` 是枚举实现的,`.none` 和 `.some` 都会作为枚举类型的用例出现在 switch 中。
|
||||
|
||||
```swift
|
||||
enum SomeEnum { case left, right }
|
||||
let x: SomeEnum? = .left
|
||||
switch x {
|
||||
case .left:
|
||||
print("Turn left")
|
||||
case .right:
|
||||
print("Turn right")
|
||||
case nil:
|
||||
print("Keep going straight")
|
||||
}
|
||||
// 打印 "Turn left"
|
||||
```
|
||||
|
||||
> 枚举用例模式语法
|
||||
>
|
||||
|
||||
#### enum-case-pattern {#enum-case-pattern}
|
||||
> *枚举用例模式* → [*类型标识*](./03_Types.md#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](./06-Declarations.md#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
|
||||
> #### enum-case-pattern {#enum-case-pattern}
|
||||
> *枚举用例模式* → [类型标识](./03_Types.md#type-identifier)<sub>可选</sub> **.** [枚举用例名](./06-Declarations.md#enum-case-name) [元组模式](#tuple-pattern)<sub>可选</sub>
|
||||
>
|
||||
|
||||
## 可选模式(Optional Pattern) {#optional-pattern}
|
||||
@ -171,9 +181,8 @@ for case let number? in arrayOfOptinalInts {
|
||||
|
||||
> 可选模式语法
|
||||
>
|
||||
|
||||
#### optional-pattern {#optional-pattern}
|
||||
> *可选模式* → [*标识符模式*](./03_Types.md#type-identifier) **?**
|
||||
> #### optional-pattern {#optional-pattern}
|
||||
> *可选模式* → [标识符模式](./03_Types.md#type-identifier) **?**
|
||||
>
|
||||
|
||||
## 类型转换模式(Type-Casting Patterns) {#type-casting-patterns}
|
||||
@ -192,17 +201,14 @@ for case let number? in arrayOfOptinalInts {
|
||||
|
||||
> 类型转换模式语法
|
||||
>
|
||||
|
||||
#### type-casting-pattern {#type-casting-pattern}
|
||||
> *类型转换模式* → [*is 模式*](#is-pattern) | [*as 模式*](#as-pattern)
|
||||
> #### type-casting-pattern {#type-casting-pattern}
|
||||
> *类型转换模式* → [is 模式](#is-pattern) | [as 模式](#as-pattern)
|
||||
>
|
||||
|
||||
#### is-pattern {#is-pattern}
|
||||
> *is 模式* → **is** [*类型*](./03_Types.md#type)
|
||||
> #### is-pattern {#is-pattern}
|
||||
> *is 模式* → **is** [类型](./03_Types.md#type)
|
||||
>
|
||||
|
||||
#### as-pattern {#as-pattern}
|
||||
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03-Types.md#type)
|
||||
> #### as-pattern {#as-pattern}
|
||||
> *as 模式* → [模式](#pattern) **as** [类型](03-Types.md#type)
|
||||
>
|
||||
|
||||
## 表达式模式(Expression Pattern) {#expression-pattern}
|
||||
@ -242,7 +248,6 @@ default:
|
||||
|
||||
> 表达式模式语法
|
||||
>
|
||||
|
||||
#### expression-pattern {#expression-pattern}
|
||||
> *表达式模式* → [*表达式*](./04_Expressions.md#expression)
|
||||
> #### expression-pattern {#expression-pattern}
|
||||
> *表达式模式* → [表达式](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
@ -57,43 +57,43 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
>
|
||||
|
||||
#### generic-parameter-clause {#generic-parameter-clause}
|
||||
> *泛型形参子句* → **<** [*泛型形参列表*](#generic-parameter-list) [*约束子句*](#requirement-clause)<sub>可选</sub> **>**
|
||||
> *泛型形参子句* → **<** [泛型形参列表](#generic-parameter-list) [约束子句](#requirement-clause)<sub>可选</sub> **>**
|
||||
>
|
||||
|
||||
#### generic-parameter-list {#generic-parameter-list}
|
||||
> *泛型形参列表* → [*泛形形参*](#generic-parameter) | [*泛形形参*](#generic-parameter) **,** [*泛型形参列表*](#generic-parameter-list)
|
||||
> *泛型形参列表* → [泛形形参](#generic-parameter) | [泛形形参](#generic-parameter) **,** [泛型形参列表](#generic-parameter-list)
|
||||
>
|
||||
|
||||
#### generic-parameter {#generic-parameter}
|
||||
> *泛形形参* → [*类型名称*](./03_Types.md#type-name)
|
||||
> *泛形形参* → [类型名称](./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}
|
||||
>
|
||||
> *约束子句* → **where** [*约束列表*](#requirement-list)
|
||||
> *约束子句* → **where** [约束列表](#requirement-list)
|
||||
>
|
||||
|
||||
#### requirement-list {#requirement-list}
|
||||
> *约束列表* → [*约束*](#requirement) | [*约束*](#requirement) **,** [*约束列表*](#requirement-list)
|
||||
> *约束列表* → [约束](#requirement) | [约束](#requirement) **,** [约束列表](#requirement-list)
|
||||
>
|
||||
|
||||
#### requirement {#requirement}
|
||||
> *约束* → [*一致性约束*](#conformance-requirement) | [*同类型约束*](#same-type-requirement)
|
||||
> *约束* → [一致性约束](#conformance-requirement) | [同类型约束](#same-type-requirement)
|
||||
>
|
||||
>
|
||||
#### 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}
|
||||
@ -124,13 +124,13 @@ let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
>
|
||||
|
||||
#### generic-argument-clause {#generic-argument-clause}
|
||||
> *泛型实参子句* → **<** [*泛型实参列表*](#generic-argument-list) **>**
|
||||
> *泛型实参子句* → **<** [泛型实参列表](#generic-argument-list) **>**
|
||||
>
|
||||
|
||||
#### generic-argument-list {#generic-argument-list}
|
||||
> *泛型实参列表* → [*泛型实参*](#generic-argument) | [*泛型实参*](#generic-argument) **,** [*泛型实参列表*](#generic-argument-list)
|
||||
> *泛型实参列表* → [泛型实参](#generic-argument) | [泛型实参](#generic-argument) **,** [泛型实参列表](#generic-argument-list)
|
||||
>
|
||||
|
||||
#### generic-argument {#generic-argument}
|
||||
> *泛型实参* → [*类型*](./03_Types.md#type)
|
||||
> *泛型实参* → [类型](./03_Types.md#type)
|
||||
>
|
||||
|
||||
@ -22,6 +22,8 @@
|
||||
>
|
||||
> *换行符* → U+000D 后面是 U+000A
|
||||
>
|
||||
>
|
||||
>
|
||||
> *注释* → **//** [单行内容注释](./02_Lexical_Structure.md#comment-text) [换行符](./02-Lexical-Structure.md#line-break)
|
||||
>
|
||||
> *注释* → **/\*** [多行内容注释](./02_Lexical_Structure.md#multiline-comment-text) **\*/**
|
||||
@ -53,6 +55,8 @@
|
||||
>
|
||||
> *标识符* → [隐式参数名](./02_Lexical_Structure.md#implicit-parameter-name)
|
||||
>
|
||||
> *标识符* → [属性包装器呈现值](./02_Lexical_Structure.md#property-wrapper-projection)
|
||||
>
|
||||
> *标识符集* → [标识符](./02_Lexical_Structure.md#identifier) | [标识符](./02-Lexical-Structure.md#identifier) **,** [标识符集](./02-Lexical-Structure.md#identifier-list)
|
||||
>
|
||||
> *标识符头(Head)* → 大写或者小写字母 A 到 Z
|
||||
@ -97,6 +101,8 @@
|
||||
>
|
||||
> *隐式参数名* → **$** [十进制数字集](./02_Lexical_Structure.md#decimal-digits)
|
||||
>
|
||||
> *属性包装器呈现值* → **$** [标识符字符集](./02-Lexical-Structure.md#identifier-characters)
|
||||
>
|
||||
|
||||
<!-- -->
|
||||
|
||||
@ -1660,6 +1666,9 @@
|
||||
>
|
||||
> *同类型约束* → [类型标识](./03_Types.md#type-identifier) **==** [类型](./03-Types.md#type-identifier)
|
||||
>
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 泛型实参子句语法
|
||||
>
|
||||
> *泛型实参子句* → **<** [泛型实参集](./09_Generic_Parameters_and_Arguments.md#generic-argument-list) **>**
|
||||
|
||||
@ -1,13 +1,17 @@
|
||||
# Swift 文档修订历史
|
||||
|
||||
### 2019-06-03
|
||||
### 2019-09-10
|
||||
|
||||
* 更新至 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/12_Subscripts.md#type-subscripts) 章节中新增有关在类型中使用下标的内容。
|
||||
* 在 [不透明类型](../02_language_guide/27_Opaque_Types.md) 篇章中新增了有关函数返回值遵循指定协议,而不需要提供指定返回类型的内容。
|
||||
* 在 [属性包装器](../02_language_guide/10_Properties.md#property-wrappers) 章节中新增了有关属性包装器的内容。
|
||||
* 在 [冻结](../03_language_reference/07_Attributes.md#frozen) 章节中新增了有关因库演变而需要的枚举和结构体冻结。
|
||||
* 新增 [隐式返回的函数](../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) 章节中新增了有关在类型中使用下标的内容。
|
||||
* 更新 [枚举 Case 模式匹配](../03_language_reference/08_Patterns.md#enumeration-case-pattern) 章节,现在枚举 case 模式匹配支持匹配可选值。
|
||||
* 更新 [结构体的逐一成员构造器](../02_language_guide/14_Initialization.md#memberwise-initializers-for-structure-types) 章节,现在逐一成员构造器支持在属性有默认值时省略形参。
|
||||
* 在 [动态查找成员](../03_language_reference/07_Attributes.md#dynamicmemberlookup) 章节中新增了有关在运行时用 key path 查找动态成员的内容。
|
||||
* 在 [条件编译代码块](../03_language_reference/05_Statements.md#Conditional-Compilation-Block) 中的目标环境里添加了 `macCatalyst`。
|
||||
* 更新 [自身类型](../03_language_reference/03_Types.md#self-type-h) 章节,现在 `Self` 可以指向当前类,结构体或者枚举声明时的类型。
|
||||
|
||||
### 2019-03-25
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
* [关于 Swift](01_welcome_to_swift/01_about_swift.md)
|
||||
* [版本兼容性](01_welcome_to_swift/02_version_compatibility.md)
|
||||
* [Swift 初见](01_welcome_to_swift/03_a_swift_tour.md)
|
||||
* [Swift 版本历史记录](01_welcome_to_swift/04_revision_history.md)
|
||||
* [Swift 版本历史记录](04_revision_history/04_revision_history.md)
|
||||
* Swift 教程
|
||||
* [基础部分](02_language_guide/01_The_Basics.md)
|
||||
* [基本运算符](02_language_guide/02_Basic_Operators.md)
|
||||
|
||||
Reference in New Issue
Block a user