@ -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) }
|
||||
>
|
||||
|
||||
|
||||
@ -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}
|
||||
@ -619,15 +618,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 +851,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 +925,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 +980,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 +998,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 +1050,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 +1115,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 +1144,7 @@ let x = [10, 3, 20, 15, 4]
|
||||
>
|
||||
|
||||
#### postfix-self-expression {#postfix-self-expression}
|
||||
> *后缀 self 表达式* → [*后缀表达式*](#postfix-expression) **.** **self**
|
||||
> *后缀 self 表达式* → [后缀表达式](#postfix-expression) **.** **self**
|
||||
>
|
||||
|
||||
|
||||
@ -1165,7 +1162,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 +1189,7 @@ someDictionary["a"]![0] = 100
|
||||
>
|
||||
|
||||
#### forced-value-expression {#forced-value-expression}
|
||||
> *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!**
|
||||
> *强制取值表达式* → [后缀表达式](#postfix-expression) **!**
|
||||
>
|
||||
|
||||
### 可选链表达式 {#optional-chaining-expression}
|
||||
@ -1244,5 +1241,4 @@ someDictionary["a"]?[0] = someFunctionWithSideEffects()
|
||||
>
|
||||
|
||||
#### optional-chaining-expression {#optional-chaining-expression}
|
||||
> *可选链表达式* → [*后缀表达式*](#postfix-expression) **?**
|
||||
>
|
||||
> *可选链表达式* → [后缀表达式](#postfix-expression) **?**
|
||||
|
||||
@ -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 )**
|
||||
|
||||
@ -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)
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user