update chapter 3 parts 04-10 for Swift 4.1 (#781)
This commit is contained in:
@ -115,11 +115,11 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
<a name="binary_expressions"></a>
|
||||
## 二元表达式
|
||||
|
||||
二元表达式由中缀运算符和左右参数表达式组成。形式如下:
|
||||
*二元表达式*由中缀运算符和左右参数表达式组成。形式如下:
|
||||
|
||||
> `左侧参数` `二元运算符` `右侧参数`
|
||||
|
||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/25_Advanced_Operators.md)。
|
||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.html) 和 [高级运算符](../chapter2/26_Advanced_Operators.html)。
|
||||
|
||||
关于 Swift 标准库提供的运算符的更多信息,请参阅 [*Swift Standard Library Operators Reference*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_StandardLibrary_Operators/index.html#//apple_ref/doc/uid/TP40016054)。
|
||||
|
||||
@ -130,7 +130,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
<a name="binary-expression"></a>
|
||||
> 二元表达式语法
|
||||
>
|
||||
> *二元表达式* → [*二元运算符*](02_Lexical_Structure.md#binary-operator) [*前缀表达式*](#prefix-expression)
|
||||
> *二元表达式* → [*二元运算符*](02_Lexical_Structure.md#binary-operator) [*前缀表达式*](#prefix-expression).
|
||||
> *二元表达式* → [*赋值运算符*](#assignment-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression)
|
||||
> *二元表达式* → [*条件运算符*](#conditional-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression)
|
||||
> *二元表达式* → [*类型转换运算符*](#type-casting-operator)
|
||||
@ -161,7 +161,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
<a name="ternary_conditional_operator"></a>
|
||||
### 三元条件运算符
|
||||
|
||||
三元条件运算符会根据条件来对两个给定表达式中的一个进行求值,形式如下:
|
||||
*三元条件运算符*会根据条件来对两个给定表达式中的一个进行求值,形式如下:
|
||||
|
||||
> `条件` ? `表达式(条件为真则使用)` : `表达式(条件为假则使用)`
|
||||
|
||||
@ -172,7 +172,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
> 三元条件运算符语法
|
||||
>
|
||||
<a name="conditional-operator"></a>
|
||||
> *三元条件运算符* → **?** [try 运算符](#try-operator)<sub>可选</sub> [*表达式*](#expression) **:**
|
||||
> *三元条件运算符* → **?** [*表达式*](#expression) **:**
|
||||
|
||||
<a name="type-casting_operators"></a>
|
||||
### 类型转换运算符
|
||||
@ -206,7 +206,7 @@ f(x as Any)
|
||||
// 打印 “Function for Any”
|
||||
```
|
||||
|
||||
桥接可将 Swift 标准库中的类型(例如 `String`)作为一个与之相关的 Foundation 类型(例如 `NSString`)来使用,而不需要新建一个实例。关于桥接的更多信息,请参阅 [*Using Swift with Cocoa and Objective-C (Swift2.2)*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216) 中的 [Working with Cocoa Data Types](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6)。
|
||||
桥接可将 Swift 标准库中的类型(例如 `String`)作为一个与之相关的 Foundation 类型(例如 `NSString`)来使用,而不需要新建一个实例。关于桥接的更多信息,请参阅 [*Using Swift with Cocoa and Objective-C (Swift4.1)*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216) 中的 [Working with Cocoa Data Types](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6)。
|
||||
|
||||
`as?` 运算符有条件地执行类型转换,返回目标类型的可选值。在运行时,如果转换成功,返回的可选值将包含转换后的值,否则返回 `nil`。如果在编译时就能确定转换一定会成功或是失败,则会导致编译报错。
|
||||
|
||||
@ -225,7 +225,7 @@ f(x as Any)
|
||||
<a name="primary_expressions"></a>
|
||||
## 基本表达式
|
||||
|
||||
基本表达式是最基本的表达式。它们可以单独使用,也可以跟前缀表达式、二元表达式、后缀表达式组合使用。
|
||||
*基本表达式*是最基本的表达式。它们可以单独使用,也可以跟前缀表达式、二元表达式、后缀表达式组合使用。
|
||||
|
||||
> 基本表达式语法
|
||||
>
|
||||
@ -239,11 +239,12 @@ f(x as Any)
|
||||
> *基本表达式* → [*隐式成员表达式*](#implicit-member-expression)
|
||||
> *基本表达式* → [*通配符表达式*](#wildcard-expression)
|
||||
> *基本表达式* → [*选择器表达式*](#selector-expression)
|
||||
> *基本表达式* → [*key-path字符串表达式*](#key-patch-string-expression)
|
||||
|
||||
<a name="literal_expression"></a>
|
||||
### 字面量表达式
|
||||
|
||||
字面量表达式可由普通字面量(例如字符串或者数字),字典或者数组字面量,或者下面列表中的特殊字面量组成:
|
||||
*字面量表达式*可由普通字面量(例如字符串或者数字),字典或者数组字面量,或者下面列表中的特殊字面量组成:
|
||||
|
||||
字面量 | 类型 | 值
|
||||
:------------- | :---------- | :----------
|
||||
@ -252,11 +253,10 @@ f(x as Any)
|
||||
`#column` | `Int` | 所在的列数
|
||||
`#function` | `String` | 所在的声明的名字
|
||||
|
||||
`#line` 除了上述含义外,还有另一种含义。当它出现在单独一行时,会被理解成行控制语句,请参阅[线路控制语句](../chapter3/10_Statements.md#线路控制语句)。
|
||||
|
||||
对于 `#function`,在函数中会返回当前函数的名字,在方法中会返回当前方法的名字,在属性的存取器中会返回属性的名字,在特殊的成员如 `init` 或 `subscript` 中会返回这个关键字的名字,在某个文件中会返回当前模块的名字。
|
||||
|
||||
`#function` 作为函数或者方法的默认参数值时,该字面量的值取决于函数或方法的调用环境。
|
||||
当其作为函数或者方法的默认参数值时,该字面量的值取决于函数或方法的调用环境。
|
||||
|
||||
```swift
|
||||
func logFunctionName(string: String = #function) {
|
||||
@ -292,22 +292,23 @@ var emptyDictionary: [String : Double] = [:]
|
||||
>
|
||||
<a name="literal-expression"></a>
|
||||
> *字面量表达式* → [*字面量*](02_Lexical_Structure.md#literal)
|
||||
> *字面量表达式* → [*数组字面量*](#array-literal) | [*字典字面量*](#dictionary-literal)
|
||||
> *字面量表达式* → [*数组字面量*](#array-literal) | [*字典字面量*](#dictionary-literal) | [*练习场字面量*](#playground-literal)
|
||||
> *字面量表达式* → **#file** | **#line** | **#column** | **#function**
|
||||
|
||||
>
|
||||
<a name="array-literal"></a>
|
||||
> *数组字面量* → **[** [*数组字面量项列表*](#array-literal-items)<sub>可选</sub> **]**
|
||||
> *数组字面量* → [[*数组字面量项列表*](#array-literal-items)<sub>可选</sub> **]**
|
||||
<a name="array-literal-items"></a>
|
||||
> *数组字面量项列表* → [*数组字面量项*](#array-literal-item) **,**<sub>可选</sub> | [*数组字面量项*](#array-literal-item) **,** [*数组字面量项列表*](#array-literal-items)
|
||||
<a name="array-literal-item"></a>
|
||||
> *数组字面量项* → [*表达式*](#expression)
|
||||
|
||||
>
|
||||
<a name="dictionary-literal"></a>
|
||||
> *字典字面量* → **[** [*字典字面量项列表*](#dictionary-literal-items) **]** | **[** **:** **]**
|
||||
> *字典字面量* → [[*字典字面量项列表*](#dictionary-literal-items) **]** | **[** **:** **]**
|
||||
<a name="dictionary-literal-items"></a>
|
||||
> *字典字面量项列表* → [*字典字面量项*](#dictionary-literal-item) **,**<sub>可选</sub> | [*字典字面量项*](#dictionary-literal-item) **,** [*字典字面量项列表*](#dictionary-literal-items)
|
||||
<a name="dictionary-literal-item"></a>
|
||||
> *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)
|
||||
> *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)。
|
||||
|
||||
|
||||
<a name="self_expression"></a>
|
||||
### Self 表达式
|
||||
@ -352,7 +353,7 @@ struct Point {
|
||||
<a name="self-method-expression"></a>
|
||||
> *self 方法表达式* → **self** **.** [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
<a name="self-subscript-expression"></a>
|
||||
> *self 下标表达式* → **self** **[** [*表达式*](#expression) **]**
|
||||
> *self 下标表达式* → **self** [ [*函数调用参数表*](#function-call-argument-list) **]**
|
||||
<a name="self-initializer-expression"></a>
|
||||
> *self 构造器表达式* → **self** **.** **init**
|
||||
|
||||
@ -377,14 +378,14 @@ struct Point {
|
||||
<a name="superclass-method-expression"></a>
|
||||
> *超类方法表达式* → **super** **.** [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
<a name="superclass-subscript-expression"></a>
|
||||
> *超类下标表达式* → **super** **[** [*表达式*](#expression) **]**
|
||||
> *超类下标表达式* → **super** [[*函数调用参数表*](#function-call-argument-list) **]**
|
||||
<a name="superclass-initializer-expression"></a>
|
||||
> *超类构造器表达式* → **super** **.** **init**
|
||||
|
||||
<a name="closure_expression"></a>
|
||||
### 闭包表达式
|
||||
|
||||
闭包表达式会创建一个闭包,在其他语言中也叫 *lambda* 或*匿名*函数。跟函数一样,闭包包含了待执行的代码,不同的是闭包还会捕获所在环境中的常量和变量。它的形式如下:
|
||||
*闭包表达式*会创建一个闭包,在其他语言中也叫 *lambda* 或*匿名*函数。跟函数一样,闭包包含了待执行的代码,不同的是闭包还会捕获所在环境中的常量和变量。它的形式如下:
|
||||
|
||||
```swift
|
||||
{ (parameters) -> return type in
|
||||
@ -392,7 +393,7 @@ struct Point {
|
||||
}
|
||||
```
|
||||
|
||||
闭包的参数声明形式跟函数一样,请参阅 [函数声明](05_Declarations.md#function_declaration)。
|
||||
闭包的参数声明形式跟函数一样,请参阅 [函数声明](05_Declarations.html#function_declaration)。
|
||||
|
||||
闭包还有几种特殊的形式,能让闭包使用起来更加简洁:
|
||||
|
||||
@ -420,11 +421,11 @@ myFunction { $0 + $1 }
|
||||
|
||||
关于如何将闭包作为参数来传递的内容,请参阅 [函数调用表达式](#function_call_expression)。
|
||||
|
||||
关于逃逸闭包的内容,请参阅[逃逸闭包](./chapter2/07_Closures.md#escaping_closures)
|
||||
关于逃逸闭包的内容,请参阅[逃逸闭包](./chapter2/07_Closures.html#escaping_closures)
|
||||
|
||||
#### 捕获列表
|
||||
|
||||
默认情况下,闭包会捕获附近作用域中的常量和变量,并使用强引用指向它们。你可以通过一个捕获列表来显式指定它的捕获行为。
|
||||
默认情况下,闭包会捕获附近作用域中的常量和变量,并使用强引用指向它们。你可以通过一个*捕获列表*来显式指定它的捕获行为。
|
||||
|
||||
捕获列表在参数列表之前,由中括号括起来,里面是由逗号分隔的一系列表达式。一旦使用了捕获列表,就必须使用 `in` 关键字,即使省略了参数名、参数类型和返回类型。
|
||||
|
||||
@ -478,22 +479,22 @@ myFunction { [unowned self] in print(self.title) } // 以无主引用捕获
|
||||
myFunction { [weak parent = self.parent] in print(parent!.title) }
|
||||
```
|
||||
|
||||
关于闭包表达式的更多信息和例子,请参阅 [闭包表达式](../chapter2/07_Closures.md#closure_expressions)。关于捕获列表的更多信息和例子,请参阅 [解决闭包引起的循环强引用](../chapter2/16_Automatic_Reference_Counting.md#resolving_strong_reference_cycles_for_closures)。
|
||||
关于闭包表达式的更多信息和例子,请参阅 [闭包表达式](../chapter2/07_Closures.html#closure_expressions)。关于捕获列表的更多信息和例子,请参阅 [解决闭包引起的循环强引用](../chapter2/23_Automatic_Reference_Counting.html#resolving_strong_reference_cycles_for_closures)。
|
||||
|
||||
> 闭包表达式语法
|
||||
>
|
||||
<a name="closure-expression"></a>
|
||||
> *闭包表达式* → **{** [*闭包签名*](#closure-signature)<sub>可选</sub> [*语句*](10_Statements.md#statements) **}**
|
||||
> *闭包表达式* → **{** [*闭包签名*](#closure-signature)<sub>可选</sub> [*语句*](#statements) **}**
|
||||
|
||||
<a name="closure-signature"></a>
|
||||
> *闭包签名* → [*参数子句*](05_Declarations.md#parameter-clause) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
> *闭包签名* → [*标识符列表*](02_Lexical_Structure.md#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
> *闭包签名* → [*参数子句*](#parameter-clause) [*函数结果*](05_Declarations.html#function-result)<sub>可选</sub> **in**
|
||||
> *闭包签名* → [*标识符列表*](#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
> *闭包签名* → [*捕获列表*](#capture-list) [*参数子句*](05_Declarations.md#parameter-clause) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
> *闭包签名* → [*捕获列表*](#capture-list) [*标识符列表*](02_Lexical_Structure.md#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
> *闭包签名* → [*捕获列表*](#capture-list) **in**
|
||||
|
||||
<a name="capture-list"></a>
|
||||
> *捕获列表* → **[** [*捕获列表项列表*](#capture-list-items) **]**
|
||||
> *捕获列表* → [ [*捕获列表项列表*](#capture-list-items) **]**
|
||||
<a name="capture-list-items"></a>
|
||||
> *捕获列表项列表* → [*捕获列表项*](#capture-list-item) | [*捕获列表项*](#capture-list-item) **,** [*捕获列表项列表*](#capture-list-items)
|
||||
<a name="capture-list-item"></a>
|
||||
@ -504,7 +505,7 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
|
||||
<a name="implicit_member_expression"></a>
|
||||
### 隐式成员表达式
|
||||
|
||||
若类型可被推断出来,可以使用隐式成员表达式来访问某个类型的成员(例如某个枚举成员或某个类型方法),形式如下:
|
||||
若类型可被推断出来,可以使用*隐式成员表达式*来访问某个类型的成员(例如某个枚举成员或某个类型方法),形式如下:
|
||||
|
||||
> .`成员名称`
|
||||
|
||||
@ -533,7 +534,7 @@ x = .AnotherValue
|
||||
<a name="Tuple_Expression"></a>
|
||||
### 元组表达式
|
||||
|
||||
元组表达式由圆括号和其中多个逗号分隔的子表达式组成。每个子表达式前面可以有一个标识符,用冒号隔开。元组表达式形式如下:
|
||||
*元组表达式*由圆括号和其中多个逗号分隔的子表达式组成。每个子表达式前面可以有一个标识符,用冒号隔开。元组表达式形式如下:
|
||||
|
||||
> (`标识符 1` : `表达式 1`, `标识符 2` : `表达式 2`, `...`)
|
||||
|
||||
@ -551,7 +552,7 @@ x = .AnotherValue
|
||||
<a name="wildcard_expression"></a>
|
||||
### 通配符表达式
|
||||
|
||||
通配符表达式可以在赋值过程中显式忽略某个值。例如下面的代码中,`10` 被赋值给 `x`,而 `20` 则被忽略:
|
||||
*通配符表达式*可以在赋值过程中显式忽略某个值。例如下面的代码中,`10` 被赋值给 `x`,而 `20` 则被忽略:
|
||||
|
||||
```swift
|
||||
(x, _) = (10, 20)
|
||||
@ -566,7 +567,7 @@ x = .AnotherValue
|
||||
<a name="selector_expression"></a>
|
||||
### 选择器表达式
|
||||
|
||||
选择器表达式可以让你通过选择器来引用在 Objective-C 中方法(method)和属性(property)的 setter 和 getter 方法。
|
||||
*选择器表达式*可以让你通过选择器来引用在 Objective-C 中方法(method)和属性(property)的 setter 和 getter 方法。
|
||||
|
||||
> \#selector(方法名)
|
||||
\#selector(getter: 属性名)
|
||||
@ -606,7 +607,7 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
|
||||
>
|
||||
> 虽然方法名或者属性名是个表达式,但是它不会被求值。
|
||||
|
||||
更多关于如何在 Swift 代码中使用选择器来与 Objective-C API 进行交互的信息,请参阅 [Using Swift with Cocoa and Objective-C (Swift 3)](https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216) 中[Objective-C Selectors](https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-ID59)部分。
|
||||
更多关于如何在 Swift 代码中使用选择器来与 Objective-C API 进行交互的信息,请参阅 [Using Swift with Cocoa and Objective-C (Swift 4.1)](https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216) 中[Objective-C Selectors](https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-ID59)部分。
|
||||
|
||||
> 选择器表达式语法
|
||||
<a name="selector-expression"></a>
|
||||
@ -617,9 +618,9 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
|
||||
<a name="postfix_expressions"></a>
|
||||
## 后缀表达式
|
||||
|
||||
后缀表达式就是在某个表达式的后面运用后缀运算符或其他后缀语法。从语法构成上来看,基本表达式也是后缀表达式。
|
||||
*后缀表达式*就是在某个表达式的后面运用后缀运算符或其他后缀语法。从语法构成上来看,基本表达式也是后缀表达式。
|
||||
|
||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/25_Advanced_Operators.md)。
|
||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.html) 和 [高级运算符](../chapter2/26_Advanced_Operators.html)。
|
||||
|
||||
关于 Swift 标准库提供的运算符的更多信息,请参阅 [*Swift Standard Library Operators Reference*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_StandardLibrary_Operators/index.html#//apple_ref/doc/uid/TP40016054)。
|
||||
|
||||
@ -639,7 +640,7 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
|
||||
<a name="function_call_expression"></a>
|
||||
### 函数调用表达式
|
||||
|
||||
函数调用表达式由函数名和参数列表组成,形式如下:
|
||||
*函数调用表达式*由函数名和参数列表组成,形式如下:
|
||||
|
||||
> `函数名`(`参数 1`, `参数 2`)
|
||||
|
||||
@ -675,7 +676,7 @@ myData.someMethod {$0 == 13}
|
||||
<a name="initializer_expression"></a>
|
||||
### 构造器表达式
|
||||
|
||||
构造器表达式用于访问某个类型的构造器,形式如下:
|
||||
*构造器表达式*用于访问某个类型的构造器,形式如下:
|
||||
|
||||
> `表达式`.init(`构造器参数`)
|
||||
|
||||
@ -718,7 +719,7 @@ let s3 = someValue.dynamicType.init(data: 7) // 有效
|
||||
<a name="explicit_member_expression"></a>
|
||||
### 显式成员表达式
|
||||
|
||||
显式成员表达式允许我们访问命名类型、元组或者模块的成员,其形式如下:
|
||||
*显式成员表达式*允许我们访问命名类型、元组或者模块的成员,其形式如下:
|
||||
|
||||
> `表达式`.`成员名`
|
||||
|
||||
@ -772,14 +773,14 @@ let x = [10, 3, 20, 15, 4]
|
||||
|
||||
> 显式成员表达式语法
|
||||
<a name="explicit-member-expression"></a>
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*十进制数字*](02_Lexical_Structure.md#decimal-digit)
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02_Lexical_Structure.md#identifier) [*泛型实参子句*](08_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.html#decimal-digit)
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02_Lexical_Structure.html#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.html#generic-argument-clause)<sub>可选</sub><br/>
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.html#identifier) **(** [*参数名称*](#argument-names) **)**
|
||||
>
|
||||
<a name="argument-names"></a>
|
||||
> *参数名称* → [*参数名*](#argument-name) [*参数名称*](#argument-names)<sub>可选</sub><br/>
|
||||
<a name="argument-name"></a>
|
||||
> *参数名* → [*标识符*](02_Lexical_Structure.md#identifier) **:**
|
||||
> *参数名* → [*标识符*](02_Lexical_Structure.html#identifier) **:**
|
||||
|
||||
<a name="postfix_self_expression"></a>
|
||||
### 后缀 self 表达式
|
||||
@ -794,59 +795,31 @@ let x = [10, 3, 20, 15, 4]
|
||||
第二种形式返回相应的类型。我们可以用它来获取某个实例的类型作为一个值来使用。例如,`SomeClass.self` 会返回 `SomeClass` 类型本身,你可以将其传递给相应函数或者方法作为参数。
|
||||
|
||||
> 后缀 self 表达式语法
|
||||
>
|
||||
<a name="postfix-self-expression"></a>
|
||||
> *后缀 self 表达式* → [*后缀表达式*](#postfix-expression) **.** **self**
|
||||
|
||||
<a name="dynamic_type_expression"></a>
|
||||
### dynamicType 表达式
|
||||
|
||||
dynamicType 表达式由类似[函数调用表达式(Function Call Expression)](#function-call-expression)的特殊语法表达式组成,形式如下:
|
||||
|
||||
> type(of:`表达式`)
|
||||
|
||||
上述形式中的表达式不能是类型名。type(of:) 表达式会返回某个实例在运行时的类型,具体请看下面的例子:
|
||||
|
||||
```swift
|
||||
class SomeBaseClass {
|
||||
class func printClassName() {
|
||||
print("SomeBaseClass")
|
||||
}
|
||||
}
|
||||
class SomeSubClass: SomeBaseClass {
|
||||
override class func printClassName() {
|
||||
print("SomeSubClass")
|
||||
}
|
||||
}
|
||||
let someInstance: SomeBaseClass = SomeSubClass()
|
||||
// someInstance 在编译时的静态类型为 SomeBaseClass,
|
||||
// 在运行时的动态类型为 SomeSubClass
|
||||
type(of: someInstance).printClassName()
|
||||
// 打印 “SomeSubClass”
|
||||
```
|
||||
|
||||
> 动态类型表达式语法
|
||||
<a name="dynamic-type-expression"></a>
|
||||
> *动态类型表达式* → type(of:表达式) **.** **dynamicType**
|
||||
|
||||
<a name="subscript_expression"></a>
|
||||
### 下标表达式
|
||||
|
||||
可通过下标表达式访问相应的下标,形式如下:
|
||||
可通过*下标表达式*访问相应的下标,形式如下:
|
||||
|
||||
> `表达式`[`索引表达式`]
|
||||
|
||||
要获取下标表达式的值,可将索引表达式作为下标表达式的参数来调用下标 getter。下标 setter 的调用方式与之一样。
|
||||
|
||||
关于下标的声明,请参阅 [协议下标声明](05_Declarations.md#protocol_subscript_declaration)。
|
||||
关于下标的声明,请参阅 [协议下标声明](05_Declarations.html#protocol_subscript_declaration)。
|
||||
|
||||
> 下标表达式语法
|
||||
>
|
||||
<a name="subscript-expression"></a>
|
||||
> *下标表达式* → [*后缀表达式*](#postfix-expression) **[** [*表达式列表*](#expression-list) **]**
|
||||
|
||||
<a name="forced-Value_expression"></a>
|
||||
### 强制取值表达式
|
||||
|
||||
当你确定可选值不是 `nil` 时,可以使用强制取值表达式来强制解包,形式如下:
|
||||
当你确定可选值不是 `nil` 时,可以使用*强制取值表达式*来强制解包,形式如下:
|
||||
|
||||
> `表达式`!
|
||||
|
||||
@ -865,13 +838,14 @@ someDictionary["a"]![0] = 100
|
||||
```
|
||||
|
||||
> 强制取值语法
|
||||
>
|
||||
<a name="forced-value-expression"></a>
|
||||
> *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!**
|
||||
|
||||
<a name="optional-chaining_expression"></a>
|
||||
### 可选链表达式
|
||||
|
||||
可选链表达式提供了一种使用可选值的便捷方法,形式如下:
|
||||
*可选链表达式*提供了一种使用可选值的便捷方法,形式如下:
|
||||
|
||||
> `表达式`?
|
||||
|
||||
@ -915,5 +889,6 @@ someDictionary["a"]?[0] = someFunctionWithSideEffects()
|
||||
```
|
||||
|
||||
> 可选链表达式语法
|
||||
>
|
||||
<a name="optional-chaining-expression"></a>
|
||||
> *可选链表达式* → [*后缀表达式*](#postfix-expression) **?**
|
||||
|
||||
@ -70,6 +70,7 @@
|
||||
通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。
|
||||
|
||||
> 循环语句语法
|
||||
>
|
||||
<a name="loop-statement"></a>
|
||||
> *循环语句* → [*for-in 语句*](#for-in-statement)
|
||||
> *循环语句* → [*while 语句*](#while-statement)
|
||||
@ -126,6 +127,7 @@ while condition {
|
||||
> *条件子句* → [*表达式*](04_Expressions.md#expression) | [*表达式*](04_Expressions.md#expression) **,** [*条件列表*](#condition-list)
|
||||
<a name="condition"></a>
|
||||
> *条件* → [*表达式*](04_Expressions.md#expression) |[*可用性条件*](#availability-condition) | [*case 条件*](#case-condition) | [*可选绑定条件*](#optional-binding-condition)
|
||||
>
|
||||
<a name="case-condition"></a>
|
||||
> *case 条件* → **case** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer)
|
||||
<a name="optional-binding-condition"></a>
|
||||
@ -247,7 +249,7 @@ guard condition else {
|
||||
> guard 语句语法
|
||||
>
|
||||
<a name="guard-statement"></a>
|
||||
> *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*](05_Declarations.html#code-block)
|
||||
> *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05_Declarations.html#code-block)
|
||||
|
||||
<a name="switch_statements"></a>
|
||||
### Switch 语句
|
||||
@ -272,7 +274,7 @@ default:
|
||||
|
||||
`switch` 语句会先计算*控制表达式*的值,然后与每一个 `case` 的模式进行匹配。如果匹配成功,程序将会执行对应的 `case` 中的语句。另外,每一个 `case` 都不能为空,也就是说在每一个 `case` 中必须至少有一条语句。如果你不想在匹配到的 `case` 中执行代码,只需在该 `case` 中写一条 `break` 语句即可。
|
||||
|
||||
可以用作控制表达式的值是十分灵活的。除了标量类型外,如 `Int`、`Character`,你可以使用任何类型的值,包括浮点数、字符串、元组、自定义类型的实例和可选类型。控制表达式的值还可以用来匹配枚举类型中的成员值或是检查该值是否包含在指定的 `Range` 中。关于如何在 `switch` 语句中使用这些类型,请参阅 [控制流](../chapter2/05_Control_Flow.md) 一章中的 [Switch](../chapter2/05_Control_Flow.html#switch)。
|
||||
可以用作控制表达式的值是十分灵活的。除了标量类型外,如 `Int`、`Character`,你可以使用任何类型的值,包括浮点数、字符串、元组、自定义类型的实例和可选类型。控制表达式的值还可以用来匹配枚举类型中的成员值或是检查该值是否包含在指定的 `Range` 中。关于如何在 `switch` 语句中使用这些类型,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章中的 [Switch](../chapter2/05_Control_Flow.html#switch)。
|
||||
|
||||
每个 `case` 的模式后面可以有一个 `where` 子句。`where` 子句由 `where` 关键字紧跟一个提供额外条件的表达式组成。因此,当且仅当控制表达式匹配一个 `case` 的模式且 `where` 子句的表达式为真时,`case` 中的语句才会被执行。在下面的例子中,控制表达式只会匹配包含两个相等元素的元组,例如 `(1, 1)`:
|
||||
|
||||
@ -297,7 +299,7 @@ case let (x, y) where x == y:
|
||||
> switch 语句语法
|
||||
>
|
||||
<a name="switch-statement"></a>
|
||||
> *switch 语句* → **switch** [*表达式*](04_Expressions.md#expression) **{** [*switch-case 列表*](#switch-cases)<sub>可选</sub> **}**
|
||||
> *switch 语句* → **switch** [*表达式*](04_Expressions.html#expression) **{** [*switch-case 列表*](#switch-cases)<sub>可选</sub> **}**
|
||||
<a name="switch-cases"></a>
|
||||
> *switch case 列表* → [*switch-case*](#switch-case) [*switch-case 列表*](#switch-cases)<sub>可选</sub>
|
||||
<a name="switch-case"></a>
|
||||
@ -309,7 +311,7 @@ case let (x, y) where x == y:
|
||||
> *case 项列表* → [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> | [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> **,** [*case 项列表*](#case-item-list)
|
||||
<a name="default-label"></a>
|
||||
> *default 标签* → **default** **:**
|
||||
|
||||
>
|
||||
<a name="where-clause"></a>
|
||||
> *where-clause* → **where** [*where 表达式*](#where-expression)
|
||||
<a name="where-expression"></a>
|
||||
@ -322,12 +324,15 @@ case let (x, y) where x == y:
|
||||
|
||||
标签的作用域在该标签所标记的语句内。可以嵌套使用带标签的语句,但标签名必须唯一。
|
||||
|
||||
关于使用带标签的语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.md) 一章中的 [带标签的语句](../chapter2/05_Control_Flow.md#labeled_statements)。
|
||||
关于使用带标签的语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章中的 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
|
||||
|
||||
> 带标签的语句语法
|
||||
>
|
||||
<a name="labeled-statement"></a>
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#loop-statement) | [*语句标签*](#statement-label) [*if 语句*](#if-statement) | [*语句标签*](#statement-label) [*switch 语句*](#switch-statement)
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#loop-statement)
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*if 语句*](#if-statement)
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*switch 语句*](#switch-statement)
|
||||
> > *带标签的语句* → [*语句标签*](#statement-label) [*do 语句*](#sdo-statement)
|
||||
<a name="statement-label"></a>
|
||||
> *语句标签* → [*标签名称*](#label-name) **:**
|
||||
<a name="label-name"></a>
|
||||
@ -361,7 +366,7 @@ case let (x, y) where x == y:
|
||||
|
||||
无论哪种情况,控制权都会被转移给被终止的控制流语句后面的第一行语句。
|
||||
|
||||
关于使用 `break` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.md) 一章的 [Break](../chapter2/05_Control_Flow.md#break) 和 [带标签的语句](../chapter2/05_Control_Flow.md#labeled_statements)。
|
||||
关于使用 `break` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Break](../chapter2/05_Control_Flow.html#break) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
|
||||
|
||||
> break 语句语法
|
||||
>
|
||||
@ -384,7 +389,7 @@ case let (x, y) where x == y:
|
||||
|
||||
在 `for` 语句中,`continue` 语句执行后,增量表达式还是会被计算,这是因为每次循环体执行完毕后,增量表达式都会被计算。
|
||||
|
||||
关于使用 `continue` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.md) 一章的 [Continue](../chapter2/05_Control_Flow.md#continue) 和 [带标签的语句](../chapter2/05_Control_Flow.md#labeled_statements)。
|
||||
关于使用 `continue` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Continue](../chapter2/05_Control_Flow.html#continue) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
|
||||
|
||||
> continue 语句语法
|
||||
>
|
||||
@ -398,7 +403,7 @@ case let (x, y) where x == y:
|
||||
|
||||
`fallthrough` 语句可出现在 `switch` 语句中的任意 `case` 中,但不能出现在最后一个 `case` 中。同时,`fallthrough` 语句也不能把控制权转移到使用了值绑定的 `case`。
|
||||
|
||||
关于在 `switch` 语句中使用 `fallthrough` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.md) 一章的 [控制转移语句](../chapter2/05_Control_Flow.md#control_transfer_statements)。
|
||||
关于在 `switch` 语句中使用 `fallthrough` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [控制转移语句](../chapter2/05_Control_Flow.html#control_transfer_statements)。
|
||||
|
||||
> fallthrough 语句语法
|
||||
>
|
||||
@ -419,7 +424,7 @@ case let (x, y) where x == y:
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 正如 [可失败构造器](05_Declarations.md#failable_initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。
|
||||
> 正如 [可失败构造器](05_Declarations.html#failable_initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。
|
||||
|
||||
而只写 `return` 时,仅仅是从该函数或方法中返回,而不返回任何值(也就是说,函数或方法的返回类型为 `Void` 或者说 `()`)。
|
||||
|
||||
@ -441,12 +446,12 @@ case let (x, y) where x == y:
|
||||
|
||||
表达式的结果必须符合 `ErrorType` 协议。
|
||||
|
||||
关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.md) 一章的 [用 throwing 函数传递错误](../chapter2/18_Error_Handling.md#propagating_errors_using_throwing_functions)。
|
||||
关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html) 一章的 [用 throwing 函数传递错误](../chapter2/18_Error_Handling.html#propagating_errors_using_throwing_functions)。
|
||||
|
||||
> throw 语句语法
|
||||
>
|
||||
<a name="throw-statement"></a>
|
||||
> *throw 语句* → **throw** [*表达式*](04_Expressions.md#expression)
|
||||
> *throw 语句* → **throw** [*表达式*](04_Expressions.html#expression)
|
||||
|
||||
<a name="defer_statements"></a>
|
||||
## Defer 语句
|
||||
@ -482,7 +487,7 @@ f()
|
||||
> defer 语句语法
|
||||
>
|
||||
<a name="defer-statement"></a>
|
||||
> *延迟语句* → **defer** [*代码块*](05_Declarations.md#code-block)
|
||||
> *延迟语句* → **defer** [*代码块*](05_Declarations.html#code-block)
|
||||
|
||||
<a name="do_statements"></a>
|
||||
## Do 语句
|
||||
@ -506,18 +511,18 @@ do {
|
||||
|
||||
如同 `switch` 语句,编译器会判断 `catch` 子句是否有遗漏。如果 `catch` 子句没有遗漏,则认为错误已被处理。否则,错误会自动传递到外围作用域,被某个 `catch` 子句处理掉或者被用 `throws` 关键字声明的抛出函数继续向外抛出。
|
||||
|
||||
为了确保错误已经被处理,可以让 `catch` 子句使用匹配所有错误的模式,如通配符模式(`_`)。如果一个 `catch` 子句不指定一种具体模式,`catch` 子句会匹配任何错误,并绑定到名为 `error` 的局部常量。有关在 `catch` 子句中使用模式的更多信息,请参阅 [模式](07_Patterns.md)。
|
||||
为了确保错误已经被处理,可以让 `catch` 子句使用匹配所有错误的模式,如通配符模式(`_`)。如果一个 `catch` 子句不指定一种具体模式,`catch` 子句会匹配任何错误,并绑定到名为 `error` 的局部常量。有关在 `catch` 子句中使用模式的更多信息,请参阅 [模式](07_Patterns.html)。
|
||||
|
||||
关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.md#handling_errors)。
|
||||
关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../chapter2/17_Error_Handling.html#handling_errors)。
|
||||
|
||||
> do 语句语法
|
||||
>
|
||||
<a name="do-statement"></a>
|
||||
> *do 语句* → **do** [*代码块*](05_Declarations.md#code-block) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
|
||||
> *do 语句* → **do** [*代码块*](05_Declarations.html#code-block) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
|
||||
<a name="catch-clauses"></a>
|
||||
> *多条 catch 子句* → [*catch 子句*](#catch-clause) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
|
||||
<a name="catch-clause"></a>
|
||||
> *catch 子句* → **catch** [*模式*](07_Patterns.md#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
|
||||
> *catch 子句* → **catch** [*模式*](07_Patterns.html#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.html#code-block)
|
||||
|
||||
<a name="compiler_control_statements"></a>
|
||||
## 编译器控制语句
|
||||
@ -530,12 +535,12 @@ do {
|
||||
> *编译器控制语句* → [*编译配置语句*](#build-config-statement)
|
||||
> *编译器控制语句* → [*线路控制语句*](#line-control-statement)
|
||||
|
||||
<a name="build_config_statements"></a>
|
||||
### 编译配置语句
|
||||
<a name="Conditional_Compilation_Block"></a>
|
||||
### 条件性编译块
|
||||
|
||||
编译配置语句可以根据一个或多个配置来有条件地编译代码。
|
||||
条件性编译块可以根据一个或多个配置来有条件地编译代码。
|
||||
|
||||
每一个编译配置语句都以 `#if` 开始,`#endif` 结束。如下是一个简单的编译配置语句:
|
||||
每一个条件性编译块都以 `#if` 开始,`#endif` 结束。如下是一个简单的条件性编译块:
|
||||
|
||||
```swift
|
||||
#if compilation condition
|
||||
@ -581,7 +586,7 @@ statements to compile if both compilation conditions are false
|
||||
> 编译配置语句语法
|
||||
>
|
||||
<a name="build-configuration-statement"></a>
|
||||
> *单个编译配置语句* → **#if** [*编译配置*](#build-configuration) [*语句*](#statements)<sub>可选</sub> [*多个编译配置 elseif 子句*](#build-configuration-elseif-clauses)<sub>可选</sub> **-** [*单个编译配置 else 子句*](#build-configuration-else-clause)<sub>可选</sub> **#endif**
|
||||
> *单个编译配置语句* → **if** [*编译配置*](#build-configuration) [*语句*](#statements)<sub>可选</sub> [*多个编译配置 elseif 子句*](#build-configuration-elseif-clauses)<sub>可选</sub> **-** [*单个编译配置 else 子句*](#build-configuration-else-clause)<sub>可选</sub> **#endif**
|
||||
<a name="build-configuration-elseif-clauses"></a>
|
||||
> *多个编译配置 elseif 子句* → [*单个编译配置 elseif 子句*](#build-configuration-elseif-clause) [*多个编译配置 elseif 子句*](build-configuration-elseif-clauses)<sub>可选</sub>
|
||||
<a name="build-configuration-elseif-clause"></a>
|
||||
@ -666,11 +671,12 @@ if #available(platform name version, ..., *) {
|
||||
<a name="availability-argument"></a>
|
||||
> *可用性参数* → [平台名称](#platform-name) [平台版本](#platform-version)
|
||||
> *可用性条件* → __*__
|
||||
|
||||
>
|
||||
<a name="platform-name"></a>
|
||||
> *平台名称* → **iOS** | **iOSApplicationExtension**
|
||||
> *平台名称* → **OSX** | **OSXApplicationExtension**
|
||||
> *平台名称* → **OSX** | **macOSApplicationExtension**
|
||||
> *平台名称* → **watchOS**
|
||||
> *平台名称* → **tvOS**
|
||||
<a name="platform-version"></a>
|
||||
> *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits)
|
||||
> *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits)
|
||||
|
||||
@ -20,6 +20,9 @@
|
||||
> 3.0
|
||||
> 翻译:[chenmingjia](https://github.com/chenmingjia)
|
||||
|
||||
> 4.1
|
||||
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
|
||||
|
||||
本页包含内容:
|
||||
|
||||
- [顶级代码](#top-level_code)
|
||||
@ -90,7 +93,8 @@
|
||||
Swift 的源文件中的顶级代码 (top-level code) 由零个或多个语句、声明和表达式组成。默认情况下,在一个源文件的顶层声明的变量,常量和其他具有命名的声明可以被同模块中的每一个源文件中的代码访问。可以使用一个访问级别修饰符来标记声明来覆盖这种默认行为,请参阅 [访问控制级别](#access_control_levels)。
|
||||
|
||||
> 顶级声明语法
|
||||
> *顶级声明* → [*多条语句*](10_Statements.md#statements)<sub>可选</sub>
|
||||
>
|
||||
> *顶级声明* → [*多条语句*](10_Statements.html#statements)<sub>可选</sub>
|
||||
|
||||
<a name="code_blocks"></a>
|
||||
## 代码块
|
||||
@ -106,8 +110,9 @@ Swift 的源文件中的顶级代码 (top-level code) 由零个或多个语句
|
||||
代码块中的“语句”包括声明、表达式和各种其他类型的语句,它们按照在源码中的出现顺序被依次执行。
|
||||
|
||||
> 代码块语法
|
||||
>
|
||||
> <a name="code-block"></a>
|
||||
> *代码块* → **{** [*多条语句*](10_Statements.md#statements)<sub>可选</sub> **}**
|
||||
> *代码块* → **{** [*多条语句*](10_Statements.html#statements)<sub>可选</sub> **}**
|
||||
|
||||
<a name="import_declaration"></a>
|
||||
## 导入声明
|
||||
@ -127,14 +132,16 @@ import 模块.子模块
|
||||
|
||||
<a name="grammer_of_an_import_declaration"></a>
|
||||
> 导入声明语法
|
||||
>
|
||||
> <a name="import-declaration"></a>
|
||||
> *导入声明* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **import** [*导入类型*](#import-kind)<sub>可选</sub> [*导入路径*](#import-path)
|
||||
> *导入声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **import** [*导入类型*](#import-kind)<sub>可选</sub> [*导入路径*](#import-path)
|
||||
>
|
||||
> <a name="import-kind"></a>
|
||||
> *导入类型* → **typealias** | **struct** | **class** | **enum** | **protocol** | **var** | **func**
|
||||
> *导入类型* → **typealias** | **struct** | **class** | **enum** | **protocol** | **let** | **var** | **func**
|
||||
> <a name="import-path"></a>
|
||||
> *导入路径* → [*导入路径标识符*](#import-path-identifier) | [*导入路径标识符*](#import-path-identifier) **.** [*导入路径*](#import-path)
|
||||
> <a name="import-path-identifier"></a>
|
||||
> *导入路径标识符* → [*标识符*](02_Lexical_Structure.md#identifier) | [*运算符*](02_Lexical_Structure.md#operator)
|
||||
> *导入路径标识符* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator)
|
||||
|
||||
<a name="constant_declaration"></a>
|
||||
## 常量声明
|
||||
@ -164,22 +171,23 @@ print("The second number is \(secondNumber).")
|
||||
// 打印 “The second number is 42.”
|
||||
```
|
||||
|
||||
当常量名称的类型 (`:` 类型) 可以被推断出时,类型标注在常量声明中是可选的,正如 [类型推断](03_Types.md#type_inference) 中所描述的。
|
||||
当常量名称的类型 (`:` 类型) 可以被推断出时,类型标注在常量声明中是可选的,正如 [类型推断](03_Types.html#type_inference) 中所描述的。
|
||||
|
||||
声明一个常量类型属性要使用 `static` 声明修饰符。类型属性在 [类型属性](../chapter2/10_Properties.md#type_properties)中有介绍。
|
||||
声明一个常量类型属性要使用 `static` 声明修饰符。类型属性在 [类型属性](../chapter2/10_Properties.html#type_properties)中有介绍。
|
||||
|
||||
如果还想获得更多关于常量的信息或者想在使用中获得帮助,请参阅 [常量和变量](../chapter2/01_The_Basics.md#constants_and_variables) 和 [存储属性](../chapter2/10_Properties.md#stored_properties)。
|
||||
如果还想获得更多关于常量的信息或者想在使用中获得帮助,请参阅 [常量和变量](../chapter2/01_The_Basics.html#constants_and_variables) 和 [存储属性](../chapter2/10_Properties.html#stored_properties)。
|
||||
|
||||
<a name="grammer_of_a_constant_declaration"></a>
|
||||
> 常量声明语法
|
||||
>
|
||||
> <a name="constant-declaration"></a>
|
||||
> *常量声明* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **let** [*模式构造器列表*](pattern-initializer-list)
|
||||
> *常量声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **let** [*模式构造器列表*](pattern-initializer-list)
|
||||
> <a name="pattern-initializer-list"></a>
|
||||
> *模式构造器列表* → [*模式构造器*](#pattern-initializer) | [*模式构造器*](#pattern-initializer) **,** [*模式构造器列表*](#pattern-initializer-list)
|
||||
> <a name="pattern-initializer"></a>
|
||||
> *模式构造器* → [*模式*](07_Patterns.md#pattern) [*构造器*](#initializer)<sub>可选</sub>
|
||||
> *模式构造器* → [*模式*](07_Patterns.html#pattern) [*构造器*](#initializer)<sub>可选</sub>
|
||||
> <a name="initializer"></a>
|
||||
> *构造器* → **=** [*表达式*](04_Expressions.md#expression)
|
||||
> *构造器* → **=** [*表达式*](04_Expressions.html#expression)
|
||||
|
||||
<a name="variable_declaration"></a>
|
||||
## 变量声明
|
||||
@ -192,7 +200,7 @@ print("The second number is \(secondNumber).")
|
||||
>
|
||||
> 也可以在协议声明中声明属性,详情请参阅 [协议属性声明](#protocol_property_declaration)。
|
||||
|
||||
可以在子类中重写继承来的变量属性,使用 `override` 声明修饰符标记属性的声明即可,详情请参阅 [重写](../chapter2/13_Inheritance.md#overriding)。
|
||||
可以在子类中重写继承来的变量属性,使用 `override` 声明修饰符标记属性的声明即可,详情请参阅 [重写](../chapter2/13_Inheritance.html#overriding)。
|
||||
|
||||
<a name="stored_variables_and_stored_variable_properties"></a>
|
||||
### 存储型变量和存储型变量属性
|
||||
@ -229,13 +237,13 @@ var 变量名称: 类型 {
|
||||
|
||||
可以在全局范围、函数内部,以及类、结构、枚举、扩展的声明中使用这种形式的声明。当变量以这种形式在全局范围或者函数内部被声明时,它表示一个计算型变量。当它在类、结构、枚举、扩展声明的上下文中被声明时,它表示一个*计算型属性 (computed property)*。
|
||||
|
||||
getter 用来读取变量值,setter 用来写入变量值。setter 子句是可选的,getter 子句是必须的。不过也可以将这些子句都省略,直接返回请求的值,正如在 [只读计算型属性](../chapter2/10_Properties.md#computed_properties) 中描述的那样。但是如果提供了一个 setter 子句,就必须也提供一个 getter 子句。
|
||||
getter 用来读取变量值,setter 用来写入变量值。setter 子句是可选的,getter 子句是必须的。不过也可以将这些子句都省略,直接返回请求的值,正如在 [只读计算型属性](../chapter2/10_Properties.html#computed_properties) 中描述的那样。但是如果提供了一个 setter 子句,就必须也提供一个 getter 子句。
|
||||
|
||||
setter 的圆括号以及 setter 名称是可选的。如果提供了 setter 名称,它就会作为 setter 的参数名称使用。如果不提供 setter 名称,setter 的参数的默认名称为 `newValue`,正如在 [便捷 setter 声明](../chapter2/10_Properties.md#shorthand_setter_declaration) 中描述的那样。
|
||||
setter 的圆括号以及 setter 名称是可选的。如果提供了 setter 名称,它就会作为 setter 的参数名称使用。如果不提供 setter 名称,setter 的参数的默认名称为 `newValue`,正如在 [便捷 setter 声明](../chapter2/10_Properties.html#shorthand_setter_declaration) 中描述的那样。
|
||||
|
||||
与存储型变量和存储型属性不同,计算型变量和计算型属性的值不存储在内存中。
|
||||
|
||||
要获得更多关于计算型属性的信息和例子,请参阅 [计算型属性](../chapter2/10_Properties.md#computed_properties)。
|
||||
要获得更多关于计算型属性的信息和例子,请参阅 [计算型属性](../chapter2/10_Properties.html#computed_properties)。
|
||||
|
||||
<a name="stored_variable_observers_and_property_observers"></a>
|
||||
### 存储型变量和属性的观察器
|
||||
@ -256,7 +264,7 @@ var 变量名称: 类型 = 表达式 {
|
||||
可以在全局范围、函数内部,或者类、结构的声明中使用这种形式的声明。当变量以这种形式在全局范围或者函数内部被声明时,观察器表示一个存储型变量观察器。当它在类和结构的声明中被声明时,观察器表示一个属性观察器。
|
||||
|
||||
可以为任何存储型属性添加观察器。也可以通过重写父类属性的方式为任何继承的属性(无论是存储型还是计算型的)添加观察器
|
||||
,正如 [重写属性观察器](../chapter2/13_Inheritance.md#overriding_property_observers) 中所描述的。
|
||||
,正如 [重写属性观察器](../chapter2/13_Inheritance.html#overriding_property_observers) 中所描述的。
|
||||
|
||||
用于初始化的表达式在类或者结构的声明中是可选的,但是在其他声明中则是必须的。如果可以从初始化表达式中推断出类型信息,那么可以不提供类型标注。
|
||||
|
||||
@ -268,12 +276,12 @@ var 变量名称: 类型 = 表达式 {
|
||||
|
||||
提供了 `willSet` 时,`didSet` 是可选的。同样的,提供了 `didSet` 时,`willSet` 则是可选的。
|
||||
|
||||
要获得更多信息以及查看如何使用属性观察器的例子,请参阅 [属性观察器](../chapter2/10_Properties.md#property_observers)。
|
||||
要获得更多信息以及查看如何使用属性观察器的例子,请参阅 [属性观察器](../chapter2/10_Properties.html#property_observers)。
|
||||
|
||||
<a name="type_variable_properties"></a>
|
||||
### 类型变量属性
|
||||
|
||||
要声明一个类型变量属性,用 `static` 声明修饰符标记该声明。类可以改用 `class` 声明修饰符标记类的类型计算型属性从而允许子类重写超类的实现。类型属性在 [类型属性](../chapter2/10_Properties.md#type_properties) 章节有详细讨论。
|
||||
要声明一个类型变量属性,用 `static` 声明修饰符标记该声明。类可以改用 `class` 声明修饰符标记类的类型计算型属性从而允许子类重写超类的实现。类型属性在 [类型属性](../chapter2/10_Properties.html#type_properties) 章节有详细讨论。
|
||||
|
||||
> 注意
|
||||
>
|
||||
@ -281,46 +289,46 @@ var 变量名称: 类型 = 表达式 {
|
||||
|
||||
<a name="grammer_of_a_variable_declaration"></a>
|
||||
> 变量声明语法
|
||||
|
||||
>
|
||||
<a name="variable-declaration"></a>
|
||||
> *变量声明* → [*变量声明头*](#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) [*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.html#type-annotation) [*代码块*](#code-block)
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 代码块*](#getter-setter-block)
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*构造器*](#initializer) [*willSet-didSet 代码块*](#willSet-didSet-block)
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.md#type-annotation) [*构造器*](#initializer)<sub>可选</sub> [*willSet-didSet 代码块*](#willSet-didSet-block)
|
||||
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*构造器*](#initializer)<sub>可选</sub> [*willSet-didSet 代码块*](#willSet-didSet-block)
|
||||
|
||||
<a name="variable-declaration-head"></a>
|
||||
> *变量声明头* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **var**
|
||||
> *变量声明头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **var**
|
||||
> <a name="variable-name"></a>
|
||||
> *变量名称* → [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
> *变量名称* → [*标识符*](02_Lexical_Structure.html#identifier)
|
||||
|
||||
<a name="getter-setter-block"></a>
|
||||
> *getter-setter 代码块* → [*代码块*](#code-block)
|
||||
> *getter-setter 代码块* → **{** [*getter 子句*](#getter-clause) [*setter 子句*](#setter-clause)<sub>可选</sub> **}**
|
||||
> *getter-setter 代码块* → **{** [*setter 子句*](#setter-clause) [*getter 子句*](#getter-clause) **}**
|
||||
> <a name="getter-clause"></a>
|
||||
> *getter 子句* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **get** [*代码块*](#code-block)
|
||||
> *getter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get** [*代码块*](#code-block)
|
||||
> <a name="setter-clause"></a>
|
||||
> *setter 子句* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **set** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
> *setter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
> <a name="setter-name"></a>
|
||||
> *setter 名称* → **(** [*标识符*](02_Lexical_Structure.md#identifier) **)**
|
||||
> *setter 名称* → **(** [*标识符*](02_Lexical_Structure.html#identifier) **)**
|
||||
|
||||
<a name="getter-setter-keyword-block"></a>
|
||||
> *getter-setter 关键字代码块* → **{** [*getter 关键字子句*](#getter-keyword-clause) [*setter 关键字子句*](#setter-keyword-clause)<sub>可选</sub> **}**
|
||||
> *getter-setter 关键字代码块* → **{** [*setter 关键字子句*](#setter-keyword-clause) [*getter 关键字子句*](#getter-keyword-clause) **}**
|
||||
> <a name="getter-keyword-clause"></a>
|
||||
> *getter 关键字子句* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **get**
|
||||
> *getter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get**
|
||||
> <a name="setter-keyword-clause"></a>
|
||||
> *setter 关键字子句* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **set**
|
||||
> *setter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set**
|
||||
|
||||
<a name="willSet-didSet-block"></a>
|
||||
> *willSet-didSet 代码块* → **{** [*willSet 子句*](#willSet-clause) [*didSet 子句*](#didSet-clause)<sub>可选</sub> **}**
|
||||
> *willSet-didSet 代码块* → **{** [*didSet 子句*](#didSet-clause) [*willSet 子句*](#willSet-clause)<sub>可选</sub> **}**
|
||||
> <a name="willSet-clause"></a>
|
||||
> *willSet 子句* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **willSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
> *willSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **willSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
> <a name="didSet-clause"></a>
|
||||
> *didSet 子句* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **didSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
> *didSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **didSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
|
||||
<a name="type_alias_declaration"></a>
|
||||
## 类型别名声明
|
||||
@ -368,14 +376,15 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
|
||||
|
||||
<a name="grammer_of_a_type_alias_declaration"></a>
|
||||
> 类型别名声明语法
|
||||
>
|
||||
> <a name="typealias-declaration"></a>
|
||||
> *类型别名声明* → [*类型别名头*](#typealias-head) [*类型别名赋值*](#typealias-assignment)
|
||||
> <a name="typealias-head"></a>
|
||||
> *类型别名头* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **typealias** [*类型别名名称*](#typealias-name)
|
||||
> *类型别名头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **typealias** [*类型别名名称*](#typealias-name)
|
||||
> <a name="typealias-name"></a>
|
||||
> *类型别名名称* → [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
> *类型别名名称* → [*标识符*](02_Lexical_Structure.html#identifier)
|
||||
> <a name="typealias-assignment"></a>
|
||||
> *类型别名赋值* → **=** [*类型*](03_Types.md#type)
|
||||
> *类型别名赋值* → **=** [*类型*](03_Types.html#type)
|
||||
|
||||
<a name="function_declaration"></a>
|
||||
## 函数声明
|
||||
@ -400,7 +409,7 @@ func 函数名称(参数列表) {
|
||||
|
||||
函数可以使用元组类型作为返回类型来返回多个值。
|
||||
|
||||
函数定义可以出现在另一个函数声明内。这种函数被称作*嵌套函数 (nested function)*。更多关于嵌套函数的讨论,请参阅 [嵌套函数](../chapter2/06_Functions.md#Nested_Functions)。
|
||||
函数定义可以出现在另一个函数声明内。这种函数被称作*嵌套函数 (nested function)*。更多关于嵌套函数的讨论,请参阅 [嵌套函数](../chapter2/06_Functions.html#Nested_Functions)。
|
||||
|
||||
<a name="parameter_names"></a>
|
||||
### 参数名
|
||||
@ -475,7 +484,7 @@ print(x)
|
||||
|
||||
调用嵌套函数 `inner()` 对 `a` 递增后,`x` 的值并未发生改变,因为 `inner()` 在外层函数 `outer()` 返回后才被调用。若要改变 `x` 的值,必须在 `outer()` 返回前调用 `inner()`。
|
||||
|
||||
关于输入输出参数的详细讨论,请参阅 [输入输出参数](../chapter2/06_Functions.md#in_out_parameters)。
|
||||
关于输入输出参数的详细讨论,请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。
|
||||
|
||||
<a name="special_kinds_of_parameters"></a>
|
||||
### 特殊参数
|
||||
@ -490,7 +499,7 @@ _ : 参数类型
|
||||
|
||||
以下划线(`_`)命名的参数会被显式忽略,无法在函数体内使用。
|
||||
|
||||
一个参数的基本类型名称如果紧跟着三个点(`...`),会被视为可变参数。一个函数至多可以拥有一个可变参数,且必须是最后一个参数。可变参数会作为包含该参数类型元素的数组处理。举例来讲,可变参数 `Int...` 会作为 `[Int]` 来处理。关于使用可变参数的例子,请参阅 [可变参数](../chapter2/06_Functions.md#variadic_parameters)。
|
||||
一个参数的基本类型名称如果紧跟着三个点(`...`),会被视为可变参数。一个函数至多可以拥有一个可变参数,且必须是最后一个参数。可变参数会作为包含该参数类型元素的数组处理。举例来讲,可变参数 `Int...` 会作为 `[Int]` 来处理。关于使用可变参数的例子,请参阅 [可变参数](../chapter2/06_Functions.html#variadic_parameters)。
|
||||
|
||||
如果在参数类型后面有一个以等号(`=`)连接的表达式,该参数会拥有默认值,即给定表达式的值。当函数被调用时,给定的表达式会被求值。如果参数在函数调用时被省略了,就会使用其默认值。
|
||||
|
||||
@ -549,25 +558,25 @@ func someFunction(callback: () throws -> Void) rethrows {
|
||||
|
||||
Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它的调用者。`Never` 返回类型的函数或方法可以称为不归,不归函数、方法要么引发不可恢复的错误,要么永远不停地运作,这会使调用后本应执行得代码就不再执行了。但即使是不归函数、方法,抛错函数和重抛出函数也可以将程序控制转移到合适的 `catch` 代码块。
|
||||
|
||||
不归函数、方法可以在 guard 语句的 else 字句中调用,具体讨论在[*Guard 语句*](10_Statements.md#guard_statements)。
|
||||
不归函数、方法可以在 guard 语句的 else 字句中调用,具体讨论在[*Guard 语句*](10_Statements.html#guard_statements)。
|
||||
你可以重载一个不归方法,但是新的方法必须保持原有的返回类型和没有返回的行为。
|
||||
|
||||
<a name="grammer_of_a_function_declaration"></a>
|
||||
> 函数声明语法
|
||||
|
||||
>
|
||||
<a name="function-declaration"></a>
|
||||
> *函数声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) [*函数体*](#function-body)<sub>可选</sub>
|
||||
> *函数声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) [*函数体*](#function-body)<sub>可选</sub>
|
||||
|
||||
<a name="function-head"></a>
|
||||
> *函数头* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **func**
|
||||
> *函数头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **func**
|
||||
> <a name="function-name"></a>
|
||||
> *函数名* → [*标识符*](02_Lexical_Structure.md#identifier) | [*运算符*](02_Lexical_Structure.md#operator)
|
||||
> *函数名* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator)
|
||||
|
||||
<a name="function-signature"></a>
|
||||
> *函数签名* → [*参数子句列表*](#parameter-clauses) **throws**<sub>可选</sub> [*函数结果*](#function-result)<sub>可选</sub>
|
||||
> *函数签名* → [*参数子句列表*](#parameter-clauses) **rethrows** [*函数结果*](#function-result)<sub>可选</sub>
|
||||
> <a name="function-result"></a>
|
||||
> *函数结果* → **->** [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*类型*](03_Types.md#type)
|
||||
> *函数结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type)
|
||||
> <a name="function-body"></a>
|
||||
> *函数体* → [*代码块*](#code-block)
|
||||
|
||||
@ -578,15 +587,15 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
> <a name="parameter-list"></a>
|
||||
> *参数列表* → [*参数*](#parameter) | [*参数*](#parameter) **,** [*参数列表*](#parameter-list)
|
||||
> <a name="parameter"></a>
|
||||
> *参数* → **let**<sub>可选</sub> [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.md#type-annotation) [*默认参数子句*](#default-argument-clause)<sub>可选</sub>
|
||||
> *参数* → **inout** [*外部参数名*](#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) **...**
|
||||
> *参数* → **let**<sub>可选</sub> [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) [*默认参数子句*](#default-argument-clause)<sub>可选</sub>
|
||||
> *参数* → **inout** [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation)
|
||||
> *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) **...**
|
||||
> <a name="external-parameter-name"></a>
|
||||
> *外部参数名* → [*标识符*](02_Lexical_Structure.md#identifier) | **_**
|
||||
> *外部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_**
|
||||
> <a name="local-parameter-name"></a>
|
||||
> *内部参数名* → [*标识符*](02_Lexical_Structure.md#identifier) | **_**
|
||||
> *内部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_**
|
||||
> <a name="default-argument-clause"></a>
|
||||
> *默认参数子句* → **=** [*表达式*](04_Expressions.md#expression)
|
||||
> *默认参数子句* → **=** [*表达式*](04_Expressions.html#expression)
|
||||
|
||||
<a name="enumeration_declaration"></a>
|
||||
## 枚举声明
|
||||
@ -599,7 +608,7 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
|
||||
不同于类或者结构体,枚举类型并不隐式提供默认构造器,所有构造器必须显式声明。一个构造器可以委托给枚举中的其他构造器,但是构造过程仅当构造器将一个枚举用例赋值给 `self` 后才算完成。
|
||||
|
||||
和结构体类似但是和类不同,枚举是值类型。枚举实例在被赋值到变量或常量时,或者传递给函数作为参数时会被复制。更多关于值类型的信息,请参阅 [结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.md#structures_and_enumerations_are_value_types)。
|
||||
和结构体类似但是和类不同,枚举是值类型。枚举实例在被赋值到变量或常量时,或者传递给函数作为参数时会被复制。更多关于值类型的信息,请参阅 [结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.html#structures_and_enumerations_are_value_types)。
|
||||
|
||||
可以扩展枚举类型,正如在 [扩展声明](#extension_declaration) 中讨论的一样。
|
||||
|
||||
@ -634,7 +643,7 @@ let f = Number.Integer
|
||||
let evenInts: [Number] = [0, 2, 4, 6].map(f)
|
||||
```
|
||||
|
||||
要获得更多关于具有关联值的枚举用例的信息和例子,请参阅 [关联值](../chapter2/08_Enumerations.md#associated_values)。
|
||||
要获得更多关于具有关联值的枚举用例的信息和例子,请参阅 [关联值](../chapter2/08_Enumerations.html#associated_values)。
|
||||
|
||||
<a name="enumerations_with_indirection"></a>
|
||||
#### 递归枚举
|
||||
@ -688,47 +697,47 @@ enum WeekendDay: String {
|
||||
|
||||
在上面这个例子中,`WeekendDay.Saturday` 的原始值是 `"Saturday"`,`WeekendDay.Sunday` 的原始值是 `"Sunday"`。
|
||||
|
||||
枚举用例具有原始值的枚举类型隐式地符合定义在 Swift 标准库中的 `RawRepresentable` 协议。所以,它们拥有一个 `rawValue` 属性和一个可失败构造器 `init?(rawValue: RawValue)`。可以使用 `rawValue` 属性去获取枚举用例的原始值,例如 `ExampleEnum.B.rawValue`。还可以根据原始值来创建一个相对应的枚举用例,只需调用枚举的可失败构造器,例如 `ExampleEnum(rawValue: 5)`,这个可失败构造器返回一个可选类型的用例。要获得更多关于具有原始值的枚举用例的信息和例子,请参阅 [原始值](../chapter2/08_Enumerations.md#raw_values)。
|
||||
枚举用例具有原始值的枚举类型隐式地符合定义在 Swift 标准库中的 `RawRepresentable` 协议。所以,它们拥有一个 `rawValue` 属性和一个可失败构造器 `init?(rawValue: RawValue)`。可以使用 `rawValue` 属性去获取枚举用例的原始值,例如 `ExampleEnum.B.rawValue`。还可以根据原始值来创建一个相对应的枚举用例,只需调用枚举的可失败构造器,例如 `ExampleEnum(rawValue: 5)`,这个可失败构造器返回一个可选类型的用例。要获得更多关于具有原始值的枚举用例的信息和例子,请参阅 [原始值](../chapter2/08_Enumerations.html#raw_values)。
|
||||
|
||||
<a name="accessing_enumeration_cases"></a>
|
||||
### 访问枚举用例
|
||||
|
||||
使用点语法(`.`)来引用枚举类型的枚举用例,例如 `EnumerationType.EnumerationCase`。当枚举类型可以由上下文推断而出时,可以省略它(但是 `.` 仍然需要),正如 [枚举语法](../chapter2/08_Enumerations.md#enumeration_syntax) 和 [显式成员表达式](04_Expressions.md#explicit_member_expression) 所述。
|
||||
使用点语法(`.`)来引用枚举类型的枚举用例,例如 `EnumerationType.EnumerationCase`。当枚举类型可以由上下文推断而出时,可以省略它(但是 `.` 仍然需要),正如 [枚举语法](../chapter2/08_Enumerations.html#enumeration_syntax) 和 [显式成员表达式](04_Expressions.html#explicit_member_expression) 所述。
|
||||
|
||||
可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../chapter2/08_Enumerations.md#matching_enumeration_values_with_a_switch_statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](07_Patterns.md#enumeration_case_pattern) 所述。
|
||||
可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../chapter2/08_Enumerations.html#matching_enumeration_values_with_a_switch_statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](07_Patterns.html#enumeration_case_pattern) 所述。
|
||||
|
||||
<a name="grammer_of_an_enumeration_declaration"></a>
|
||||
> 枚举声明语法
|
||||
|
||||
>
|
||||
<a name="enum-declaration"></a>
|
||||
> *枚举声明* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> [*联合风格枚举*](#union-style-enum)
|
||||
> *枚举声明* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> [*原始值风格枚举*](#raw-value-style-enum)
|
||||
> *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> [*联合风格枚举*](#union-style-enum)
|
||||
> *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> [*原始值风格枚举*](#raw-value-style-enum)
|
||||
|
||||
<a name="union-style-enum"></a>
|
||||
> *联合风格枚举* → **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.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause)<sub>可选</sub> **{** [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub> **}**
|
||||
> <a name="union-style-enum-members"></a>
|
||||
> *多个联合风格枚举成员* → [*联合风格枚举成员*](#union-style-enum-member) [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub>
|
||||
> <a name="union-style-enum-member"></a>
|
||||
> *联合风格枚举成员* → [*声明*](#declaration) | [*联合风格枚举用例子句*](#union-style-enum-case-clause)
|
||||
> <a name="union-style-enum-case-clause"></a>
|
||||
> *联合风格枚举用例子句* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **indirect**<sub>可选</sub> **case** [*联合风格枚举用例列表*](#union-style-enum-case-list)
|
||||
> *联合风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **indirect**<sub>可选</sub> **case** [*联合风格枚举用例列表*](#union-style-enum-case-list)
|
||||
> <a name="union-style-enum-case-list"></a>
|
||||
> *联合风格枚举用例列表* → [*联合风格枚举用例*](#union-style-enum-case) | [*联合风格枚举用例*](#union-style-enum-case) **,** [*联合风格枚举用例列表*](#union-style-enum-case-list)
|
||||
> <a name="union-style-enum-case"></a>
|
||||
> *联合风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*元组类型*](03_Types.md#tuple-type)<sub>可选</sub>
|
||||
> *联合风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*元组类型*](03_Types.html#tuple-type)<sub>可选</sub>
|
||||
> <a name="enum-name"></a>
|
||||
> *枚举名称* → [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
> *枚举名称* → [*标识符*](02_Lexical_Structure.html#identifier)
|
||||
> <a name="enum-case-name"></a>
|
||||
> *枚举用例名称* → [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
> *枚举用例名称* → [*标识符*](02_Lexical_Structure.html#identifier)
|
||||
|
||||
<a name="raw-value-style-enum"></a>
|
||||
> *原始值风格枚举* → **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.md#type-inheritance-clause) **{** [*多个原始值风格枚举成员*](#raw-value-style-enum-members) **}**
|
||||
> *原始值风格枚举* → **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause) **{** [*多个原始值风格枚举成员*](#raw-value-style-enum-members) **}**
|
||||
> <a name="raw-value-style-enum-members"></a>
|
||||
> *多个原始值风格枚举成员* → [*原始值风格枚举成员*](#raw-value-style-enum-member) [*多个原始值风格枚举成员*](#raw-value-style-enum-members)<sub>可选</sub>
|
||||
> <a name="raw-value-style-enum-member"></a>
|
||||
> *原始值风格枚举成员* → [*声明*](#declaration) | [*原始值风格枚举用例子句*](#raw-value-style-enum-case-clause)
|
||||
> <a name="raw-value-style-enum-case-clause"></a>
|
||||
> *原始值风格枚举用例子句* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **case** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list)
|
||||
> *原始值风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **case** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list)
|
||||
> <a name="raw-value-style-enum-case-list"></a>
|
||||
> *原始值风格枚举用例列表* → [*原始值风格枚举用例*](#raw-value-style-enum-case) | [*原始值风格枚举用例*](#raw-value-style-enum-case) **,** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list)
|
||||
> <a name="raw-value-style-enum-case"></a>
|
||||
@ -736,7 +745,7 @@ enum WeekendDay: String {
|
||||
> <a name="raw-value-assignment"></a>
|
||||
> *原始值赋值* → **=** [*原始值字面量*](#raw-value-literal)
|
||||
> <a name="raw-value-literal"></a>
|
||||
> *原始值字面量* → [数字型字面量](02_Lexical_Structure.md#numeric-literal) | [字符串型字面量](02_Lexical_Structure.md#static-string-literal) | [布尔型字面量](02_Lexical_Structure.md#boolean-literal)
|
||||
> *原始值字面量* → [数字型字面量](02_Lexical_Structure.html#numeric-literal) | [字符串型字面量](02_Lexical_Structure.html#static-string-literal) | [布尔型字面量](02_Lexical_Structure.html#boolean-literal)
|
||||
|
||||
<a name="structure_declaration"></a>
|
||||
## 结构体声明
|
||||
@ -749,33 +758,34 @@ struct 结构体名称: 采纳的协议 {
|
||||
}
|
||||
```
|
||||
|
||||
结构体内可包含零个或多个声明。这些声明可以包括存储型和计算型属性、类型属性、实例方法、类型方法、构造器、下标、类型别名,甚至其他结构体、类、和枚举声明。结构体声明不能包含析构器或者协议声明。关于结构体的详细讨论和示例,请参阅 [类和结构体](../chapter2/09_Classes_and_Structures.md)。
|
||||
结构体内可包含零个或多个声明。这些声明可以包括存储型和计算型属性、类型属性、实例方法、类型方法、构造器、下标、类型别名,甚至其他结构体、类、和枚举声明。结构体声明不能包含析构器或者协议声明。关于结构体的详细讨论和示例,请参阅 [类和结构体](../chapter2/09_Classes_and_Structures.html)。
|
||||
|
||||
结构体可以采纳任意数量的协议,但是不能继承自类、枚举或者其他结构体。
|
||||
|
||||
有三种方法可以创建一个已声明的结构体实例:
|
||||
|
||||
* 调用结构体内声明的构造器,正如 [构造器](../chapter2/14_Initialization.md#initializers) 所述。
|
||||
* 调用结构体内声明的构造器,正如 [构造器](../chapter2/14_Initialization.html#initializers) 所述。
|
||||
|
||||
* 如果没有声明构造器,调用结构体的成员逐一构造器,正如 [结构体类型的成员逐一构造器](../chapter2/14_Initialization.md#memberwise_initializers_for_structure_types) 所述。
|
||||
* 如果没有声明构造器,调用结构体的成员逐一构造器,正如 [结构体类型的成员逐一构造器](../chapter2/14_Initialization.html#memberwise_initializers_for_structure_types) 所述。
|
||||
|
||||
* 如果没有声明构造器,而且结构体的所有属性都有初始值,调用结构体的默认构造器,正如 [默认构造器](../chapter2/14_Initialization.md#default_initializers) 所述。
|
||||
* 如果没有声明构造器,而且结构体的所有属性都有初始值,调用结构体的默认构造器,正如 [默认构造器](../chapter2/14_Initialization.html#default_initializers) 所述。
|
||||
|
||||
结构体的构造过程请参阅 [构造过程](../chapter2/14_Initialization.md)。
|
||||
结构体的构造过程请参阅 [构造过程](../chapter2/14_Initialization.html)。
|
||||
|
||||
结构体实例的属性可以用点语法(`.`)来访问,正如 [访问属性](../chapter2/09_Classes_and_Structures.md#accessing_properties) 所述。
|
||||
结构体实例的属性可以用点语法(`.`)来访问,正如 [访问属性](../chapter2/09_Classes_and_Structures.html#accessing_properties) 所述。
|
||||
|
||||
结构体是值类型。结构体的实例在被赋予变量或常量,或传递给函数作为参数时会被复制。关于值类型的更多信息,请参阅
|
||||
[结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.md#structures_and_enumerations_are_value_types)。
|
||||
[结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.html#structures_and_enumerations_are_value_types)。
|
||||
|
||||
可以使用扩展声明来扩展结构体类型的行为,请参阅 [扩展声明](#extension_declaration)。
|
||||
|
||||
<a name="grammer_of_a_structure_declaration"></a>
|
||||
> 结构体声明语法
|
||||
>
|
||||
> <a name="struct-declaration"></a>
|
||||
> *结构体声明* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> **struct** [*结构体名称*](#struct-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.md#type-inheritance-clause)<sub>可选</sub> [*结构体主体*](#struct-body)
|
||||
> *结构体声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> **struct** [*结构体名称*](#struct-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*结构体主体*](#struct-body)
|
||||
> <a name="struct-name"></a>
|
||||
> *结构体名称* → [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
> *结构体名称* → [*标识符*](02_Lexical_Structure.html#identifier)
|
||||
> <a name="struct-body"></a>
|
||||
> *结构体主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
|
||||
|
||||
@ -790,7 +800,7 @@ class 类名: 超类, 采纳的协议 {
|
||||
}
|
||||
```
|
||||
|
||||
类内可以包含零个或多个声明。这些声明可以包括存储型和计算型属性、实例方法、类型方法、构造器、唯一的析构器、下标、类型别名,甚至其他结构体、类和枚举声明。类声明不能包含协议声明。关于类的详细讨论和示例,请参阅 [类和结构体](../chapter2/09_Classes_and_Structures.md)。
|
||||
类内可以包含零个或多个声明。这些声明可以包括存储型和计算型属性、实例方法、类型方法、构造器、唯一的析构器、下标、类型别名,甚至其他结构体、类和枚举声明。类声明不能包含协议声明。关于类的详细讨论和示例,请参阅 [类和结构体](../chapter2/09_Classes_and_Structures.html)。
|
||||
|
||||
一个类只能继承自一个超类,但是可以采纳任意数量的协议。超类紧跟在类名和冒号后面,其后跟着采纳的协议。泛型类可以继承自其它泛型类和非泛型类,但是非泛型类只能继承自其它非泛型类。当在冒号后面写泛型超类的名称时,必须写上泛型类的全名,包括它的泛型形参子句。
|
||||
|
||||
@ -804,22 +814,23 @@ class 类名: 超类, 采纳的协议 {
|
||||
|
||||
有两种方法来创建已声明的类的实例:
|
||||
|
||||
* 调用类中声明的构造器,请参阅 [构造器](../chapter2/14_Initialization.md#initializers)。
|
||||
* 调用类中声明的构造器,请参阅 [构造器](../chapter2/14_Initialization.html#initializers)。
|
||||
|
||||
* 如果没有声明构造器,而且类的所有属性都被赋予了初始值,调用类的默认构造器,请参阅 [默认构造器](../chapter2/14_Initialization.md#default_initializers)。
|
||||
* 如果没有声明构造器,而且类的所有属性都被赋予了初始值,调用类的默认构造器,请参阅 [默认构造器](../chapter2/14_Initialization.html#default_initializers)。
|
||||
|
||||
类实例属性可以用点语法(`.`)来访问,请参阅 [访问属性](../chapter2/09_Classes_and_Structures.md#accessing_properties)。
|
||||
类实例属性可以用点语法(`.`)来访问,请参阅 [访问属性](../chapter2/09_Classes_and_Structures.html#accessing_properties)。
|
||||
|
||||
类是引用类型。当被赋予常量或变量,或者传递给函数作为参数时,类的实例会被引用,而不是被复制。关于引用类型的更多信息,请参阅 [结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.md#structures_and_enumerations_are_value_types)。
|
||||
类是引用类型。当被赋予常量或变量,或者传递给函数作为参数时,类的实例会被引用,而不是被复制。关于引用类型的更多信息,请参阅 [结构体和枚举是值类型](../chapter2/09_Classes_and_Structures.html#structures_and_enumerations_are_value_types)。
|
||||
|
||||
可以使用扩展声明来扩展类的行为,请参阅 [扩展声明](#extension_declaration)。
|
||||
|
||||
<a name="grammer_of_a_class_declaration"></a>
|
||||
> 类声明语法
|
||||
>
|
||||
> <a name="class-declaration"></a>
|
||||
> *类声明* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [访问级别修饰符](#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> [*类主体*](#class-body)
|
||||
> *类声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*类主体*](#class-body)
|
||||
> <a name="class-name"></a>
|
||||
> *类名* → [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
> *类名* → [*标识符*](02_Lexical_Structure.html#identifier)
|
||||
> <a name="class-body"></a>
|
||||
> *类主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
|
||||
|
||||
@ -836,15 +847,15 @@ protocol 协议名称: 继承的协议 {
|
||||
|
||||
协议的主体包含零个或多个协议成员声明,这些成员描述了任何采纳该协议的类型必须满足的一致性要求。一个协议可以声明采纳者必须实现的某些属性、方法、构造器以及下标。协议也可以声明各种各样的类型别名,叫做关联类型,它可以指定协议的不同声明之间的关系。协议声明不能包括类、结构体、枚举或者其它协议的声明。协议成员声明会在后面进行讨论。
|
||||
|
||||
协议类型可以继承自任意数量的其它协议。当一个协议类型继承自其它协议的时候,来自其它协议的所有要求会聚合在一起,而且采纳当前协议的类型必须符合所有的这些要求。关于如何使用协议继承的例子,请参阅 [协议继承](../chapter2/22_Protocols.md#protocol_inheritance)。
|
||||
协议类型可以继承自任意数量的其它协议。当一个协议类型继承自其它协议的时候,来自其它协议的所有要求会聚合在一起,而且采纳当前协议的类型必须符合所有的这些要求。关于如何使用协议继承的例子,请参阅 [协议继承](../chapter2/21_Protocols.html#protocol_inheritance)。
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 也可以使用协议合成类型来聚合多个协议的一致性要求,请参阅 [协议合成类型](03_Types.md#protocol_composition_type) 和 [协议合成](../chapter2/22_Protocols.md#protocol_composition)。
|
||||
> 也可以使用协议合成类型来聚合多个协议的一致性要求,请参阅 [协议合成类型](03_Types.html#protocol_composition_type) 和 [协议合成](../chapter2/21_Protocols.html#protocol_composition)。
|
||||
|
||||
可以通过类型的扩展声明来采纳协议,从而为之前声明的类型添加协议一致性。在扩展中,必须实现所有采纳协议的要求。如果该类型已经实现了所有的要求,可以让这个扩展声明的主体留空。
|
||||
|
||||
默认地,符合某个协议的类型必须实现所有在协议中声明的属性、方法和下标。即便如此,可以用 `optional` 声明修饰符标注协议成员声明,以指定它们的实现是可选的。`optional` 修饰符仅仅可以用于使用 `objc` 特性标记过的协议。因此,仅仅类类型可以采用并符合包含可选成员要求的协议。更多关于如何使用 `optional` 声明修饰符的信息,以及如何访问可选协议成员的指导——例如不能确定采纳协议的类型是否实现了它们时——请参阅 [可选协议要求](../chapter2/22_Protocols.md#optional_protocol_requirements)
|
||||
默认地,符合某个协议的类型必须实现所有在协议中声明的属性、方法和下标。即便如此,可以用 `optional` 声明修饰符标注协议成员声明,以指定它们的实现是可选的。`optional` 修饰符仅仅可以用于使用 `objc` 特性标记过的协议。因此,仅仅类类型可以采用并符合包含可选成员要求的协议。更多关于如何使用 `optional` 声明修饰符的信息,以及如何访问可选协议成员的指导——例如不能确定采纳协议的类型是否实现了它们时——请参阅 [可选协议要求](../chapter2/21_Protocols.html#optional_protocol_requirements)
|
||||
|
||||
为了限制协议只能被类类型采纳,需要使用 `class` 关键字来标记协议,将 `class` 关键在写在冒号后面的继承的协议列表的首位。例如,下面的协议只能被类类型采纳:
|
||||
|
||||
@ -860,20 +871,20 @@ protocol SomeProtocol: class {
|
||||
>
|
||||
> 如果协议已经用 `objc` 特性标记了,`class` 要求就隐式地应用于该协议,无需显式使用 `class` 关键字。
|
||||
|
||||
协议类型是命名的类型,因此它们可以像其他命名类型一样使用,正如 [协议作为类型](../chapter2/22_Protocols.md#protocols_as_types) 所讨论的。然而,不能构造一个协议的实例,因为协议实际上不提供它们指定的要求的实现。
|
||||
协议类型是命名的类型,因此它们可以像其他命名类型一样使用,正如 [协议作为类型](../chapter2/21_Protocols.html#protocols_as_types) 所讨论的。然而,不能构造一个协议的实例,因为协议实际上不提供它们指定的要求的实现。
|
||||
|
||||
可以使用协议来声明作为代理的类或者结构体应该实现的方法,正如 [委托(代理)模式](../chapter2/22_Protocols.md#delegation) 中所述。
|
||||
可以使用协议来声明作为代理的类或者结构体应该实现的方法,正如 [委托(代理)模式](../chapter2/21_Protocols.html#delegation) 中所述。
|
||||
|
||||
<a name="grammer_of_a_protocol_declaration"></a>
|
||||
> 协议声明语法
|
||||
|
||||
>
|
||||
<a name="protocol-declaration"></a>
|
||||
> *协议声明* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*协议主体*](#protocol-body)
|
||||
> *协议声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*协议主体*](#protocol-body)
|
||||
> <a name="protocol-name"></a>
|
||||
> *协议名称* → [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
> *协议名称* → [*标识符*](02_Lexical_Structure.html#identifier)
|
||||
> <a name="protocol-body"></a>
|
||||
> *协议主体* → **{** [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub> **}**
|
||||
|
||||
>
|
||||
<a name="protocol-member-declaration"></a>
|
||||
> *协议成员声明* → [*协议属性声明*](#protocol-property-declaration)
|
||||
> *协议成员声明* → [*协议方法声明*](#protocol-method-declaration)
|
||||
@ -894,19 +905,20 @@ var 属性名: 类型 { get set }
|
||||
|
||||
同其它协议成员声明一样,这些属性声明仅仅针对符合该协议的类型声明了 getter 和 setter 要求,你不能在协议中直接实现 getter 和 setter。
|
||||
|
||||
符合类型可以通过多种方式满足 getter 和 setter 要求。如果属性声明包含 `get` 和 `set` 关键字,符合类型就可以用存储型变量属性或可读可写的计算型属性来满足此要求,但是属性不能以常量属性或只读计算型属性实现。如果属性声明仅仅包含 `get` 关键字的话,它可以作为任意类型的属性被实现。关于如何实现协议中的属性要求的例子,请参阅 [属性要求](../chapter2/22_Protocols.md#property_requirements)
|
||||
符合类型可以通过多种方式满足 getter 和 setter 要求。如果属性声明包含 `get` 和 `set` 关键字,符合类型就可以用存储型变量属性或可读可写的计算型属性来满足此要求,但是属性不能以常量属性或只读计算型属性实现。如果属性声明仅仅包含 `get` 关键字的话,它可以作为任意类型的属性被实现。关于如何实现协议中的属性要求的例子,请参阅 [属性要求](../chapter2/21_Protocols.html#property_requirements)
|
||||
|
||||
另请参阅 [变量声明](#variable_declaration)。
|
||||
|
||||
<a name="grammer_of_an_import_declaration"></a>
|
||||
> 协议属性声明语法
|
||||
>
|
||||
> <a name="protocol-property-declaration"></a>
|
||||
> *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.md#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||
> *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||
|
||||
<a name="protocol_method_declaration"></a>
|
||||
### 协议方法声明
|
||||
|
||||
协议可以通过在协议声明主体中引入一个协议方法声明,来声明符合的类型必须实现的方法。协议方法声明和函数方法声明有着相同的形式,但有两项例外:它们不包括函数体,也不能包含默认参数。关于如何实现协议中的方法要求的例子,请参阅 [方法要求](../chapter2/22_Protocols.md#method_requirements)。
|
||||
协议可以通过在协议声明主体中引入一个协议方法声明,来声明符合的类型必须实现的方法。协议方法声明和函数方法声明有着相同的形式,但有两项例外:它们不包括函数体,也不能包含默认参数。关于如何实现协议中的方法要求的例子,请参阅 [方法要求](../chapter2/21_Protocols.html#method_requirements)。
|
||||
|
||||
使用 `static` 声明修饰符可以在协议声明中声明一个类型方法。结构体实现这些方法时使用 `static` 声明修饰符,类在实现这些方法时,除了使用 `static` 声明修饰符,还可以选择使用 `class` 声明修饰符。通过扩展实现时亦是如此。
|
||||
|
||||
@ -914,8 +926,9 @@ var 属性名: 类型 { get set }
|
||||
|
||||
<a name="grammer_of_a_protocol_declaration"></a>
|
||||
> 协议方法声明语法
|
||||
>
|
||||
> <a name="protocol-method-declaration"></a>
|
||||
> *协议方法声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature)
|
||||
> *协议方法声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature)
|
||||
|
||||
<a name="protocol_initializer_declaration"></a>
|
||||
### 协议构造器声明
|
||||
@ -931,9 +944,10 @@ var 属性名: 类型 { get set }
|
||||
|
||||
<a name="grammer_of_a_protocol_initializer_declaration"></a>
|
||||
> 协议构造器声明语法
|
||||
>
|
||||
> <a name="protocol-initializer-declaration"></a>
|
||||
> *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub>
|
||||
> *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows**
|
||||
> *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub>
|
||||
> *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows**
|
||||
|
||||
<a name="protocol_subscript_declaration"></a>
|
||||
### 协议下标声明
|
||||
@ -956,21 +970,22 @@ subscript (参数列表) -> 返回类型 { get set }
|
||||
<a name="protocol_associated_type_declaration"></a>
|
||||
### 协议关联类型声明
|
||||
|
||||
使用关键字 `associatedtype` 来声明协议关联类型。关联类型为作为协议声明的一部分,为某种类型提供了一个别名。关联类型和泛型参数子句中的类型参数很相似,但是它们和 `Self` 一样,用于协议中。`Self` 指代采纳协议的类型。要获得更多信息和例子,请参阅 [关联类型](../chapter2/23_Generics.md#associated_types)。
|
||||
使用关键字 `associatedtype` 来声明协议关联类型。关联类型为作为协议声明的一部分,为某种类型提供了一个别名。关联类型和泛型参数子句中的类型参数很相似,但是它们和 `Self` 一样,用于协议中。`Self` 指代采纳协议的类型。要获得更多信息和例子,请参阅 [关联类型](../chapter2/22_Generics.html#associated_types)。
|
||||
|
||||
另请参阅 [类型别名声明](#type_alias_declaration)。
|
||||
|
||||
<a name="grammer_of_a_protocol_associated_type_declaration"></a>
|
||||
> 协议关联类型声明语法
|
||||
>
|
||||
> <a name="protocol-associated-type-declaration"></a>
|
||||
> *协议关联类型声明* → [*类型别名头*](#typealias-head) [*类型继承子句*](03_Types.md#type-inheritance-clause)<sub>可选</sub> [*类型别名赋值*](#typealias-assignment)<sub>可选</sub>
|
||||
> *协议关联类型声明* → [*类型别名头*](#typealias-head) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*类型别名赋值*](#typealias-assignment)<sub>可选</sub>
|
||||
|
||||
<a name="initializer_declaration"></a>
|
||||
## 构造器声明
|
||||
|
||||
构造器声明会为程序中的类、结构体或枚举引入构造器。构造器使用关键字 `init` 来声明,有两种基本形式。
|
||||
|
||||
结构体、枚举、类可以有任意数量的构造器,但是类的构造器具有不同的规则和行为。不同于结构体和枚举,类有两种构造器,即指定构造器和便利构造器,请参阅 [构造过程](../chapter2/14_Initialization.md)。
|
||||
结构体、枚举、类可以有任意数量的构造器,但是类的构造器具有不同的规则和行为。不同于结构体和枚举,类有两种构造器,即指定构造器和便利构造器,请参阅 [构造过程](../chapter2/14_Initialization.html)。
|
||||
|
||||
采用如下形式声明结构体和枚举的构造器,以及类的指定构造器:
|
||||
|
||||
@ -1008,7 +1023,7 @@ convenience init(参数列表) {
|
||||
|
||||
就像函数和方法,构造器也可以抛出或者重抛错误,你可以在构造器参数列表的圆括号之后使用 `throws` 或 `rethrows` 关键字来表明相应的抛出行为。
|
||||
|
||||
关于在不同类型中声明构造器的例子,请参阅 [构造过程](../chapter2/14_Initialization.md)。
|
||||
关于在不同类型中声明构造器的例子,请参阅 [构造过程](../chapter2/14_Initialization.html)。
|
||||
|
||||
<a name="failable_initializers"></a>
|
||||
### 可失败构造器
|
||||
@ -1049,17 +1064,18 @@ if let actualInstance = SomeStruct(input: "Hello") {
|
||||
|
||||
子类可以用任意种类的指定构造器重写超类的可失败指定构造器,但是只能用非可失败指定构造器重写超类的非可失败指定构造器。
|
||||
|
||||
更多关于可失败构造器的信息和例子,请参阅 [可失败构造器](../chapter2/14_Initialization.md#failable_initializers)。
|
||||
更多关于可失败构造器的信息和例子,请参阅 [可失败构造器](../chapter2/14_Initialization.html#failable_initializers)。
|
||||
|
||||
<a name="grammer_of_an_initializer_declaration"></a>
|
||||
> 构造器声明语法
|
||||
>
|
||||
> <a name="initializer-declaration"></a>
|
||||
> *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub> [*构造器主体*](#initializer-body)
|
||||
> *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows**<sub>可选</sub> [*构造器主体*](#initializer-body)
|
||||
> *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub> [*构造器主体*](#initializer-body)
|
||||
> *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows**<sub>可选</sub> [*构造器主体*](#initializer-body)
|
||||
> <a name="initializer-head"></a>
|
||||
> *构造器头* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init**
|
||||
> *构造器头* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **?**
|
||||
> *构造器头* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **!**
|
||||
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init**
|
||||
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **?**
|
||||
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **!**
|
||||
> <a name="initializer-body"></a>
|
||||
> *构造器主体* → [*代码块*](#code-block)
|
||||
|
||||
@ -1080,12 +1096,13 @@ deinit {
|
||||
|
||||
析构器不能直接调用。
|
||||
|
||||
关于如何在类声明中使用析构器的例子,请参阅 [析构过程](../chapter2/15_Deinitialization.md)。
|
||||
关于如何在类声明中使用析构器的例子,请参阅 [析构过程](../chapter2/15_Deinitialization.html)。
|
||||
|
||||
<a name="grammer_of_a_deinitializer_declaration"></a>
|
||||
> 析构器声明语法
|
||||
>
|
||||
> <a name="deinitializer-declaration"></a>
|
||||
> *析构器声明* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block)
|
||||
> *析构器声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block)
|
||||
|
||||
<a name="extension_declaration"></a>
|
||||
## 扩展声明
|
||||
@ -1104,7 +1121,7 @@ extension 类型名称 where 要求 {
|
||||
}
|
||||
```
|
||||
|
||||
扩展声明体可包含零个或多个声明语句。这些声明语句可以包括计算型属性、计算型类型属性、实例方法、类型方法、构造器、下标声明,甚至是类、结构体和枚举声明。扩展声明不能包含析构器、协议声明、存储型属性、属性观察器或其他扩展声明。关于扩展声明的详细讨论,以及各种扩展声明的例子,请参阅 [扩展](../chapter2/21_Extensions.md)。
|
||||
扩展声明体可包含零个或多个声明语句。这些声明语句可以包括计算型属性、计算型类型属性、实例方法、类型方法、构造器、下标声明,甚至是类、结构体和枚举声明。扩展声明不能包含析构器、协议声明、存储型属性、属性观察器或其他扩展声明。关于扩展声明的详细讨论,以及各种扩展声明的例子,请参阅 [扩展](../chapter2/20_Extensions.html)。
|
||||
|
||||
如果类型为类,结构体,或枚举类型,则扩展声明会扩展相应的类型。如果类型为协议类型,则扩展声明会扩展所有遵守这个协议的类型。在扩展的协议体中声明语句不能使用 `final` 标识符。
|
||||
|
||||
@ -1116,13 +1133,14 @@ extension 类型名称 where 要求 {
|
||||
|
||||
<a name="grammer_of_an_extension_declaration"></a>
|
||||
> 扩展声明语法
|
||||
>
|
||||
> <a name="extension-declaration"></a>
|
||||
> *扩展声明* → [特性](06_Attributes.md#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.md#type-identifier) [*类型-继承-子句*](03_Types.md#type-inheritance-clause)<sub>可选</sub> [*扩展主体*](#extension-body)
|
||||
> *扩展声明* → [特性](06_Attributes.html#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.html#type-identifier) [*类型-继承-子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*扩展主体*](#extension-body)
|
||||
> <a name="extension-body"></a>
|
||||
> *扩展声明* → [特性](06_Attributes.md#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.md#type-identifier) [*泛型-where-子句*](03_Types.md#type-inheritance-clause) [*扩展主体*](#extension-body)
|
||||
> *扩展声明* → [特性](06_Attributes.html#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.html#type-identifier) [*泛型-where-子句*](03_Types.html#type-inheritance-clause) [*扩展主体*](#extension-body)
|
||||
> *扩展主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
|
||||
> *多条声明* → [单条声明](#subscript_declaration) [多条声明](#declarations) <sub>可选</sub>
|
||||
> *单条声明* → [声明语句](#declarations) | [编译器-控制-语句](10_Statements.md#compiler-control-statement)
|
||||
> *单条声明* → [声明语句](#declarations) | [编译器-控制-语句](10_Statements.html#compiler-control-statement)
|
||||
|
||||
<a name="subscript_declaration"></a>
|
||||
## 下标声明
|
||||
@ -1152,18 +1170,19 @@ subscript (参数列表) -> 返回类型 {
|
||||
|
||||
同样可以在协议声明中声明下标,正如 [协议下标声明](#protocol_subscript_declaration) 中所述。
|
||||
|
||||
更多关于下标的信息和例子,请参阅 [下标](../chapter2/12_Subscripts.md)。
|
||||
更多关于下标的信息和例子,请参阅 [下标](../chapter2/12_Subscripts.html)。
|
||||
|
||||
<a name="grammer_of_a_subscript_declaration"></a>
|
||||
> 下标声明语法
|
||||
>
|
||||
> <a name="subscript-declaration"></a>
|
||||
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*代码块*](#code-block)
|
||||
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*getter-setter 代码块*](#getter-setter-block)
|
||||
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||
> <a name="subscript-head"></a>
|
||||
> *下标头* → [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **subscript** [*参数子句*](#parameter-clause)
|
||||
> *下标头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **subscript** [*参数子句*](#parameter-clause)
|
||||
> <a name="subscript-result"></a>
|
||||
> *下标结果* → **->** [*特性列表*](06_Attributes.md#attributes)<sub>可选</sub> [*类型*](03_Types.md#type)
|
||||
> *下标结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type)
|
||||
|
||||
<a name="operator_declaration"></a>
|
||||
## 运算符声明
|
||||
@ -1172,7 +1191,7 @@ subscript (参数列表) -> 返回类型 {
|
||||
|
||||
可以声明三种不同的缀性:中缀、前缀和后缀。运算符的缀性指定了运算符与其运算对象的相对位置。
|
||||
|
||||
运算符声明有三种基本形式,每种缀性各一种。运算符的缀性通过在 `operator` 关键字之前添加声明修饰符 `infix`,`prefix` 或 `postfix` 来指定。每种形式中,运算符的名字只能包含 [运算符](02_Lexical_Structure.md#operators) 中定义的运算符字符。
|
||||
运算符声明有三种基本形式,每种缀性各一种。运算符的缀性通过在 `operator` 关键字之前添加声明修饰符 `infix`,`prefix` 或 `postfix` 来指定。每种形式中,运算符的名字只能包含 [运算符](02_Lexical_Structure.html#operators) 中定义的运算符字符。
|
||||
|
||||
下面的形式声明了一个新的中缀运算符:
|
||||
|
||||
@ -1204,26 +1223,25 @@ postfix operator 运算符名称 {}
|
||||
|
||||
和前缀运算符一样,后缀运算符的声明中不指定优先级,而且后缀运算符是非结合的。
|
||||
|
||||
声明了一个新的运算符以后,需要实现一个跟这个运算符同名的函数来实现这个运算符。如果是实现一个前缀或者后缀运算符,也必须使用相符的 `prefix` 或者 `postfix` 声明修饰符标记函数声明。如果是实现中缀运算符,则不需要使用 `infix` 声明修饰符标记函数声明。关于如何实现一个新的运算符的例子,请参阅 [自定义运算符](../chapter2/25_Advanced_Operators.md#custom_operators)。
|
||||
声明了一个新的运算符以后,需要实现一个跟这个运算符同名的函数来实现这个运算符。如果是实现一个前缀或者后缀运算符,也必须使用相符的 `prefix` 或者 `postfix` 声明修饰符标记函数声明。如果是实现中缀运算符,则不需要使用 `infix` 声明修饰符标记函数声明。关于如何实现一个新的运算符的例子,请参阅 [自定义运算符](../chapter2/25_Advanced_Operators.html#custom_operators)。
|
||||
|
||||
<a name="grammer_of_an_operator_declaration"></a>
|
||||
> 运算符声明语法
|
||||
|
||||
>
|
||||
<a name="operator-declaration"></a>
|
||||
> *运算符声明* → [*前缀运算符声明*](#prefix-operator-declaration) | [*后缀运算符声明*](#postfix-operator-declaration) | [*中缀运算符声明*](#infix-operator-declaration)
|
||||
|
||||
>
|
||||
<a name="prefix-operator-declaration"></a>
|
||||
> *前缀运算符声明* → **prefix** **运算符** [*运算符*](02_Lexical_Structure.md#operator) **{** **}**
|
||||
> *前缀运算符声明* → **prefix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** **}**
|
||||
> <a name="postfix-operator-declaration"></a>
|
||||
> *后缀运算符声明* → **postfix** **运算符** [*运算符*](02_Lexical_Structure.md#operator) **{** **}**
|
||||
> *后缀运算符声明* → **postfix** **运算符** [*运算符*] (02_Lexical_Structure.html#operator) **{** **}**
|
||||
> <a name="infix-operator-declaration"></a>
|
||||
> *中缀运算符声明* → **infix** **运算符** [*运算符*](02_Lexical_Structure.md#operator) **{** [*中缀运算符属性*](#infix-operator-attributes)<sub>可选</sub> **}**
|
||||
|
||||
> *中缀运算符声明* → **infix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** [*中缀运算符属性*](#infix-operator-attributes)<sub>可选</sub> **}**
|
||||
>
|
||||
<a name="infix-operator-group"></a>
|
||||
> *中缀运算符组* → [*优先级组名称*](#precedence-group-name)
|
||||
|
||||
<a name="precedence_group_declaration_modifiers"></a>
|
||||
|
||||
## 优先级组声明
|
||||
|
||||
*优先级组声明 (A precedence group declaration)* 会向程序的中缀运算符引入一个全新的优先级组。当没有用圆括号分组时,运算符优先级反应了运算符与它的操作数的关系的紧密程度。
|
||||
@ -1252,7 +1270,7 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
|
||||
<a name="grammer_of_a_precedence_group_declaration"></a>
|
||||
> 优先级组声明语法
|
||||
|
||||
>
|
||||
<a name="precedence-group-declaration"></a>
|
||||
> *优先级组声明* → **precedence**[*优先级组名称*](#precedence-group-name){[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> }
|
||||
<a name="precedence-group-attributes"></a>
|
||||
@ -1265,16 +1283,18 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
> *优先级组关系* → **higherThan:**[*多优先级组名称*](#precedence-group-names)
|
||||
> *优先级组关系* → **lowerThan:**[*多优先级组名称*](#precedence-group-names)
|
||||
> <a name="precedence-group-assignment"></a>
|
||||
> *优先级组赋值* → **assignment:**[*布尔字面值*](02_Lexical_Structure.md#boolean-literal)
|
||||
> *优先级组赋值* → **assignment:**[*布尔字面值*](02_Lexical_Structure.html#boolean-literal)
|
||||
<a name="precedence-group-associativity"></a>
|
||||
> *优先级组结合性* → **associativity:left**
|
||||
> *优先级组结合性* → **associativity:right**
|
||||
> *优先级组结合性* → **associativity:none**
|
||||
>
|
||||
<a name="precedence-group-names"></a>
|
||||
> *多优先级组名称* → [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name)
|
||||
<a name="precedence-group-name"></a>
|
||||
> *优先级组名称* →[*标识符*](02_Lexical_Structure.md#identifier)
|
||||
> *优先级组名称* →[*标识符*](02_Lexical_Structure.html#identifier)
|
||||
|
||||
<a name="Declaration_Modifiers"></a>
|
||||
## 声明修饰符
|
||||
|
||||
声明修饰符都是关键字或上下文相关的关键字,可以修改一个声明的行为或者含义。可以在声明的特性(如果存在)和引入该声明的关键字之间,利用声明修饰符的关键字或上下文相关的关键字指定一个声明修饰符。
|
||||
@ -1291,13 +1311,13 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
|
||||
`lazy`
|
||||
|
||||
该修饰符用于修饰类或结构体中的存储型变量属性,表示该属性的初始值最多只被计算和存储一次,且发生在它被第一次访问时。关于如何使用 `lazy` 修饰符的例子,请参阅 [惰性存储型属性](../chapter2/10_Properties.md#lazy_stored_properties)。
|
||||
该修饰符用于修饰类或结构体中的存储型变量属性,表示该属性的初始值最多只被计算和存储一次,且发生在它被第一次访问时。关于如何使用 `lazy` 修饰符的例子,请参阅 [惰性存储型属性](../chapter2/10_Properties.html#lazy_stored_properties)。
|
||||
|
||||
`optional`
|
||||
|
||||
该修饰符用于修饰协议中的属性、方法以及下标成员,表示符合类型可以不实现这些成员要求。
|
||||
|
||||
只能将 `optional` 修饰符用于被 `objc` 特性标记的协议。这样一来,就只有类类型可以采纳并符合拥有可选成员要求的协议。关于如何使用 `optional` 修饰符,以及如何访问可选协议成员(比如,不确定符合类型是否已经实现了这些可选成员)的信息,请参阅 [可选协议要求](../chapter2/22_Protocols.md#optional_protocol_requirements)。
|
||||
只能将 `optional` 修饰符用于被 `objc` 特性标记的协议。这样一来,就只有类类型可以采纳并符合拥有可选成员要求的协议。关于如何使用 `optional` 修饰符,以及如何访问可选协议成员(比如,不确定符合类型是否已经实现了这些可选成员)的信息,请参阅 [可选协议要求](../chapter2/22_Protocols.html#optional_protocol_requirements)。
|
||||
|
||||
`required`
|
||||
|
||||
@ -1305,12 +1325,12 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
|
||||
`weak`
|
||||
|
||||
该修饰符用于修饰变量或存储型变量属性,表示该变量或属性持有其存储的对象的弱引用。这种变量或属性的类型必须是可选的类类型。使用 `weak` 修饰符可避免强引用循环。关于 `weak` 修饰符的更多信息和例子,请参阅 [弱引用](../chapter2/16_Automatic_Reference_Counting.md#resolving_strong_reference_cycles_between_class_instances)。
|
||||
该修饰符用于修饰变量或存储型变量属性,表示该变量或属性持有其存储的对象的弱引用。这种变量或属性的类型必须是可选的类类型。使用 `weak` 修饰符可避免强引用循环。关于 `weak` 修饰符的更多信息和例子,请参阅 [弱引用](../chapter2/16_Automatic_Reference_Counting.html#resolving_strong_reference_cycles_between_class_instances)。
|
||||
|
||||
<a name="access_control_levels"></a>
|
||||
### 访问控制级别
|
||||
|
||||
Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`。可以使用以下任意一种访问级别修饰符来指定声明的访问级别。访问控制在 [访问控制](../chapter2/24_Access_Control.md) 中有详细讨论。
|
||||
Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`。可以使用以下任意一种访问级别修饰符来指定声明的访问级别。访问控制在 [访问控制](../chapter2/24_Access_Control.html) 中有详细讨论。
|
||||
|
||||
`public`
|
||||
|
||||
@ -1324,17 +1344,17 @@ Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`
|
||||
|
||||
该修饰符表示声明只能被所在源文件的代码访问。
|
||||
|
||||
以上访问级别修饰符都可以选择带上一个参数,该参数由一对圆括号和其中的 `set` 关键字组成(例如,`private(set)`)。使用这种形式的访问级别修饰符来限制某个属性或下标的 setter 的访问级别低于其本身的访问级别,正如 [Getter 和 Setter](../chapter2/24_Access_Control.md#getters_and_setters) 中所讨论的。
|
||||
以上访问级别修饰符都可以选择带上一个参数,该参数由一对圆括号和其中的 `set` 关键字组成(例如,`private(set)`)。使用这种形式的访问级别修饰符来限制某个属性或下标的 setter 的访问级别低于其本身的访问级别,正如 [Getter 和 Setter](../chapter2/24_Access_Control.html#getters_and_setters) 中所讨论的。
|
||||
|
||||
<a name="grammer_of_a_declaration_modifier"></a>
|
||||
> 声明修饰符的语法
|
||||
|
||||
>
|
||||
<a name="declaration-modifier"></a>
|
||||
> *声明修饰符* → **class** | **convenience**| **dynamic** | **final** | **infix** | **lazy** | **mutating** | **nonmutating** | **optional** | **override** | **postfix** | **prefix** | **required** | **static** | **unowned** | **unowned ( safe )** | **unowned ( unsafe )** | **weak**
|
||||
> 声明修饰符 → [*访问级别修饰符*](#access-level-modifier)
|
||||
> <a name="declaration-modifiers"></a>
|
||||
> *声明修饰符列表* → [*声明修饰符*](#declaration-modifier) [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub>
|
||||
|
||||
>
|
||||
<a name="access-level-modifier"></a>
|
||||
> 访问级别修饰符 → **internal** | **internal ( set )**
|
||||
> 访问级别修饰符 → **private** | **private ( set )**
|
||||
|
||||
@ -11,6 +11,9 @@
|
||||
> 2.1
|
||||
> 翻译:[小铁匠 Linus](https://github.com/kevin833752)
|
||||
|
||||
> 4.1
|
||||
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
|
||||
|
||||
本页内容包括:
|
||||
|
||||
- [声明特性](#declaration_attributes)
|
||||
@ -216,23 +219,15 @@ convention 特性总是与下面的参数之一一起出现。
|
||||
在函数或者方法声明上使用该特性,它表示参数将不会被存储以供延迟执行,这将确保参数不会超出函数调用的生命周期。在使用 `escaping` 声明特性的函数类型中访问属性和方法时不需要显式地使用 `self.`。关于如何使用 `escaping` 特性的例子,请参阅 [逃逸闭包](http://wiki.jikexueyuan.com/project/swift/chapter2/07_Closures.html)。
|
||||
|
||||
> 特性语法
|
||||
|
||||
>
|
||||
> *特性 *→ @ <font color = 0x3386c8>特性名 特性参数子句</font><sub>可选</sub>
|
||||
|
||||
> *特性名* → <font color = 0x3386c8>标识符
|
||||
|
||||
> *特性参数子句* → ( <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub> )
|
||||
|
||||
> *特性列表* → <font color = 0x3386c8>特性 特性列表</font><sub>可选</sub>
|
||||
|
||||
>
|
||||
> *均衡令牌列表* → <font color = 0x3386c8>均衡令牌 均衡令牌列表</font><sub>可选</sub>
|
||||
|
||||
> *均衡令牌* → ( <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub> )
|
||||
|
||||
> *均衡令牌* → [ <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub> ]
|
||||
|
||||
> *均衡令牌* → { <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub>}
|
||||
|
||||
> *均衡令牌* → 任意标识符,关键字,字面量或运算符
|
||||
|
||||
> *均衡令牌* → 任意标点除了 (,),[,],{,或 }
|
||||
|
||||
@ -11,6 +11,9 @@
|
||||
> 2.1
|
||||
> 翻译:[BridgeQ](https://github.com/WXGBridgeQ)
|
||||
|
||||
> 4.1
|
||||
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
|
||||
|
||||
本页内容包括:
|
||||
|
||||
- [通配符模式(Wildcard Pattern)](#wildcard_pattern)
|
||||
@ -22,7 +25,7 @@
|
||||
- [类型转换模式(Type-Casting Pattern)](#type-casting_patterns)
|
||||
- [表达式模式(Expression Pattern)](#expression_pattern)
|
||||
|
||||
模式代表单个值或者复合值的结构。例如,元组 `(1, 2)` 的结构是由逗号分隔的,包含两个元素的列表。因为模式代表一种值的结构,而不是特定的某个值,你可以利用模式来匹配各种各样的值。比如,`(x, y)` 可以匹配元组 `(1, 2)`,以及任何含两个元素的元组。除了利用模式匹配一个值以外,你可以从复合值中提取出部分或全部值,然后分别把各个部分的值和一个常量或变量绑定起来。
|
||||
*模式*代表单个值或者复合值的结构。例如,元组 `(1, 2)` 的结构是由逗号分隔的,包含两个元素的列表。因为模式代表一种值的结构,而不是特定的某个值,你可以利用模式来匹配各种各样的值。比如,`(x, y)` 可以匹配元组 `(1, 2)`,以及任何含两个元素的元组。除了利用模式匹配一个值以外,你可以从复合值中提取出部分或全部值,然后分别把各个部分的值和一个常量或变量绑定起来。
|
||||
|
||||
Swift 中的模式分为两类:一种能成功匹配任何类型的值,另一种在运行时匹配某个特定值时可能会失败。
|
||||
|
||||
@ -31,11 +34,12 @@ Swift 中的模式分为两类:一种能成功匹配任何类型的值,另
|
||||
第二类模式用于全模式匹配,这种情况下你试图匹配的值在运行时可能不存在。此类模式包括枚举用例模式、可选模式、表达式模式和类型转换模式。你在 `switch` 语句的 `case` 标签中,`do` 语句的 `catch` 子句中,或者在 `if`、`while`、`guard` 和 `for-in` 语句的 `case` 条件句中使用这类模式。
|
||||
|
||||
> 模式语法
|
||||
>
|
||||
<a name="pattern"></a>
|
||||
> *模式* → [*通配符模式*](#wildcard_pattern) [*类型标注*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [*标识符模式*](#identifier_pattern) [*类型标注*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [*通配符模式*](#wildcard_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [*标识符模式*](#identifier_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [*值绑定模式*](#value-binding-pattern)
|
||||
> *模式* → [*元组模式*](#tuple-pattern) [*类型标注*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [*元组模式*](#tuple-pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
|
||||
> *模式* → [*枚举用例模式*](#enum-case-pattern)
|
||||
> *模式* → [*可选模式*](#optional-pattern)
|
||||
> *模式* → [*类型转换模式*](#type-casting-pattern)
|
||||
@ -44,7 +48,7 @@ Swift 中的模式分为两类:一种能成功匹配任何类型的值,另
|
||||
<a name="wildcard_pattern"></a>
|
||||
## 通配符模式(Wildcard Pattern)
|
||||
|
||||
通配符模式由一个下划线(`_`)构成,用于匹配并忽略任何值。当你想忽略被匹配的值时可以使用该模式。例如,下面这段代码在闭区间 `1...3` 中迭代,每次迭代都忽略该区间的当前值:
|
||||
*通配符模式*由一个下划线(`_`)构成,用于匹配并忽略任何值。当你想忽略被匹配的值时可以使用该模式。例如,下面这段代码在闭区间 `1...3` 中迭代,每次迭代都忽略该区间的当前值:
|
||||
|
||||
```swift
|
||||
for _ in 1...3 {
|
||||
@ -53,13 +57,14 @@ for _ in 1...3 {
|
||||
```
|
||||
|
||||
> 通配符模式语法
|
||||
>
|
||||
<a name="wildcard-pattern"></a>
|
||||
> *通配符模式* → **_**
|
||||
|
||||
<a name="identifier_pattern"></a>
|
||||
## 标识符模式(Identifier Pattern)
|
||||
|
||||
标识符模式匹配任何值,并将匹配的值和一个变量或常量绑定起来。例如,在下面的常量声明中,`someValue` 是一个标识符模式,匹配了 `Int` 类型的 `42`:
|
||||
*标识符模式*匹配任何值,并将匹配的值和一个变量或常量绑定起来。例如,在下面的常量声明中,`someValue` 是一个标识符模式,匹配了 `Int` 类型的 `42`:
|
||||
|
||||
```swift
|
||||
let someValue = 42
|
||||
@ -70,13 +75,14 @@ let someValue = 42
|
||||
如果一个变量或常量声明的左边是一个标识符模式,那么这个标识符模式是值绑定模式的子模式。
|
||||
|
||||
> 标识符模式语法
|
||||
>
|
||||
<a name="identifier-pattern"></a>
|
||||
> *标识符模式* → [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
> *标识符模式* → [*标识符*](02_Lexical_Structure.html#identifier)
|
||||
|
||||
<a name="value-binding_pattern"></a>
|
||||
## 值绑定模式(Value-Binding Pattern)
|
||||
|
||||
值绑定模式把匹配到的值绑定给一个变量或常量。把匹配到的值绑定给常量时,用关键字 `let`,绑定给变量时,用关键字 `var`。
|
||||
*值绑定模式*把匹配到的值绑定给一个变量或常量。把匹配到的值绑定给常量时,用关键字 `let`,绑定给变量时,用关键字 `var`。
|
||||
|
||||
在值绑定模式中的标识符模式会把新命名的变量或常量与匹配到的值做绑定。例如,你可以拆开一个元组,然后把每个元素绑定到相应的标识符模式中。
|
||||
|
||||
@ -93,13 +99,14 @@ case let (x, y):
|
||||
在上面这个例子中,`let` 会分配到元组模式 `(x, y)` 中的各个标识符模式。因此,`switch` 语句中 `case let (x, y):` 和 `case (let x, let y):` 的匹配效果是一样的。
|
||||
|
||||
> 值绑定模式语法
|
||||
>
|
||||
<a name="value-binding-pattern"></a>
|
||||
> *值绑定模式* → **var** [*模式*](#pattern) | **let** [*模式*](#pattern)
|
||||
|
||||
<a name="tuple_pattern"></a>
|
||||
## 元组模式
|
||||
|
||||
元组模式是由逗号分隔的,具有零个或多个模式的列表,并由一对圆括号括起来。元组模式匹配相应元组类型的值。
|
||||
*元组模式*是由逗号分隔的,具有零个或多个模式的列表,并由一对圆括号括起来。元组模式匹配相应元组类型的值。
|
||||
|
||||
你可以使用类型标注去限制一个元组模式能匹配哪种元组类型。例如,在常量声明 `let (x, y): (Int, Int) = (1, 2)` 中的元组模式 `(x, y): (Int, Int)` 只匹配两个元素都是 `Int` 类型的元组。
|
||||
|
||||
@ -122,6 +129,7 @@ let (a): Int = 2 // a: Int = 2
|
||||
```
|
||||
|
||||
> 元组模式语法
|
||||
>
|
||||
<a name="tuple-pattern"></a>
|
||||
> *元组模式* → **(** [*元组模式元素列表*](#tuple-pattern-element-list)<sub>可选</sub> **)**
|
||||
<a name="tuple-pattern-element-list"></a>
|
||||
@ -132,18 +140,19 @@ let (a): Int = 2 // a: Int = 2
|
||||
<a name="enumeration_case_pattern"></a>
|
||||
## 枚举用例模式(Enumeration Case Pattern)
|
||||
|
||||
枚举用例模式匹配现有的某个枚举类型的某个用例。枚举用例模式出现在 `switch` 语句中的 `case` 标签中,以及 `if`、`while`、`guard` 和 `for-in` 语句的 `case` 条件中。
|
||||
*枚举用例模式*匹配现有的某个枚举类型的某个用例。枚举用例模式出现在 `switch` 语句中的 `case` 标签中,以及 `if`、`while`、`guard` 和 `for-in` 语句的 `case` 条件中。
|
||||
|
||||
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../chapter2/08_Enumerations.md#associated_values)。
|
||||
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../chapter2/08_Enumerations.html#associated_values)。
|
||||
|
||||
> 枚举用例模式语法
|
||||
>
|
||||
<a name="enum-case-pattern"></a>
|
||||
> *枚举用例模式* → [*类型标识*](03_Types.md#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](05_Declarations.md#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
|
||||
> *枚举用例模式* → [*类型标识*](03_Types.html#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](05_Declarations.html#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
|
||||
|
||||
<a name="optional_pattern"></a>
|
||||
## 可选模式(Optional Pattern)
|
||||
|
||||
可选模式匹配包装在一个 `Optional(Wrapped)` 或者 `ExplicitlyUnwrappedOptional(Wrapped)` 枚举中的 `Some(Wrapped)` 用例中的值。可选模式由一个标识符模式和紧随其后的一个问号组成,可以像枚举用例模式一样使用。
|
||||
*可选模式*匹配包装在一个 `Optional(Wrapped)` 或者 `ExplicitlyUnwrappedOptional(Wrapped)` 枚举中的 `Some(Wrapped)` 用例中的值。可选模式由一个标识符模式和紧随其后的一个问号组成,可以像枚举用例模式一样使用。
|
||||
|
||||
由于可选模式是 `Optional` 和 `ImplicitlyUnwrappedOptional` 枚举用例模式的语法糖,下面两种写法是等效的:
|
||||
|
||||
@ -174,8 +183,9 @@ for case let number? in arrayOfOptinalInts {
|
||||
```
|
||||
|
||||
> 可选模式语法
|
||||
>
|
||||
<a name="optional-pattern"></a>
|
||||
> *可选模式* → [*标识符模式*](03_Types.md#type-identifier) **?**
|
||||
> *可选模式* → [*标识符模式*](03_Types.html#type-identifier) **?**
|
||||
|
||||
<a name="type-casting_patterns"></a>
|
||||
## 类型转换模式(Type-Casting Patterns)
|
||||
@ -189,20 +199,21 @@ for case let number? in arrayOfOptinalInts {
|
||||
|
||||
`as` 模式仅当一个值的类型在运行时和 `as` 模式右边的指定类型一致,或者是其子类的情况下,才会匹配这个值。如果匹配成功,被匹配的值的类型被转换成 `as` 模式右边指定的类型。
|
||||
|
||||
关于使用 `switch` 语句配合 `is` 模式和 `as` 模式来匹配值的例子,请参阅 [Any 和 AnyObject 的类型转换](../chapter2/19_Type_Casting.md#type_casting_for_any_and_anyobject)。
|
||||
关于使用 `switch` 语句配合 `is` 模式和 `as` 模式来匹配值的例子,请参阅 [Any 和 AnyObject 的类型转换](../chapter2/18_Type_Casting.html#type_casting_for_any_and_anyobject)。
|
||||
|
||||
> 类型转换模式语法
|
||||
>
|
||||
<a name="type-casting-pattern"></a>
|
||||
> *类型转换模式* → [*is 模式*](#is-pattern) | [*as 模式*](#as-pattern)
|
||||
<a name="is-pattern"></a>
|
||||
> *is 模式* → **is** [*类型*](03_Types.md#type)
|
||||
> *is 模式* → **is** [*类型*](03_Types.html#type)
|
||||
<a name="as-pattern"></a>
|
||||
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.md#type)
|
||||
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.html#type)
|
||||
|
||||
<a name="expression_pattern"></a>
|
||||
## 表达式模式(Expression Pattern)
|
||||
|
||||
表达式模式代表表达式的值。表达式模式只出现在 `switch` 语句中的 `case` 标签中。
|
||||
*表达式模式*代表表达式的值。表达式模式只出现在 `switch` 语句中的 `case` 标签中。
|
||||
|
||||
表达式模式代表的表达式会使用 Swift 标准库中的 `~=` 运算符与输入表达式的值进行比较。如果 `~=` 运算符返回 `true`,则匹配成功。默认情况下,`~=` 运算符使用 `==` 运算符来比较两个相同类型的值。它也可以将一个整型数值与一个 `Range` 实例中的一段整数区间做匹配,正如下面这个例子所示:
|
||||
|
||||
@ -237,5 +248,6 @@ default:
|
||||
```
|
||||
|
||||
> 表达式模式语法
|
||||
>
|
||||
<a name="expression-pattern"></a>
|
||||
> *表达式模式* → [*表达式*](04_Expressions.md#expression)
|
||||
> *表达式模式* → [*表达式*](04_Expressions.html#expression)
|
||||
|
||||
@ -11,6 +11,9 @@
|
||||
> 3.0
|
||||
> 翻译+校对:[chenmingjia](https:github.com/chenmingjia)
|
||||
|
||||
> 4.1
|
||||
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
|
||||
|
||||
本页包含内容:
|
||||
|
||||
- [泛型形参子句](#generic_parameter)
|
||||
@ -19,12 +22,12 @@
|
||||
|
||||
本节涉及泛型类型、泛型函数以及泛型构造器的参数,包括形参和实参。声明泛型类型、函数或构造器时,须指定相应的类型参数。类型参数相当于一个占位符,当实例化泛型类型、调用泛型函数或泛型构造器时,就用具体的类型实参替代之。
|
||||
|
||||
关于 Swift 语言的泛型概述,请参阅 [泛型](../chapter2/23_Generics.md)。
|
||||
关于 Swift 语言的泛型概述,请参阅 [泛型](../chapter2/22_Generics.html)。
|
||||
|
||||
<a name="generic_parameter"></a>
|
||||
## 泛型形参子句
|
||||
|
||||
泛型形参子句指定泛型类型或函数的类型形参,以及这些参数相关的约束和要求。泛型形参子句用尖括号(`<>`)包住,形式如下:
|
||||
*泛型形参子句*指定泛型类型或函数的类型形参,以及这些参数相关的约束和要求。泛型形参子句用尖括号(`<>`)包住,形式如下:
|
||||
|
||||
> <`泛型形参列表`>
|
||||
|
||||
@ -67,10 +70,10 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
|
||||
泛型函数或构造器可以重载,但在泛型形参子句中的类型形参必须有不同的约束或要求,抑或二者皆不同。当调用重载的泛型函数或构造器时,编译器会根据这些约束来决定调用哪个重载函数或构造器。
|
||||
|
||||
更多关于泛型 where 从句的信息和关于泛型函数声明的例子,可以看一看 [泛型 where 子句](https://github.com/numbbbbb/the-swift-programming-language-in-chinese/blob/gh-pages/source/chapter2/23_Generics.md#where_clauses)
|
||||
更多关于泛型 where 从句的信息和关于泛型函数声明的例子,可以看一看 [泛型 where 子句](https://github.com/numbbbbb/the-swift-programming-language-in-chinese/blob/gh-pages/source/chapter2/22_Generics.html#where_clauses)
|
||||
|
||||
> 泛型形参子句语法
|
||||
|
||||
>
|
||||
<a name="generic-parameter-clause"></a>
|
||||
> *泛型形参子句* → **<** [*泛型形参列表*](#generic-parameter-list) [*约束子句*](#requirement-clause)<sub>可选</sub> **>**
|
||||
<a name="generic-parameter-list"></a>
|
||||
@ -96,7 +99,7 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
<a name="generic_argument"></a>
|
||||
## 泛型实参子句
|
||||
|
||||
泛型实参子句指定泛型类型的类型实参。泛型实参子句用尖括号(`<>`)包住,形式如下:
|
||||
*泛型实参子句*指定泛型类型的类型实参。泛型实参子句用尖括号(`<>`)包住,形式如下:
|
||||
|
||||
> <`泛型实参列表`>
|
||||
|
||||
@ -119,6 +122,7 @@ let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
如 [泛型形参子句](#generic_parameter) 所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。
|
||||
|
||||
> 泛型实参子句语法
|
||||
>
|
||||
<a name="generic-argument-clause"></a>
|
||||
> *泛型实参子句* → **<** [*泛型实参列表*](#generic-argument-list) **>**
|
||||
<a name="generic-argument-list"></a>
|
||||
|
||||
@ -8,6 +8,9 @@
|
||||
> 2.0
|
||||
> 翻译+校对:[miaosiqi](https://github.com/miaosiqi)
|
||||
|
||||
> 4.1
|
||||
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
|
||||
|
||||
本页包含内容:
|
||||
|
||||
* [语句(Statements)](#statements)
|
||||
@ -23,6 +26,7 @@
|
||||
## 语句
|
||||
|
||||
> 语句语法
|
||||
>
|
||||
> *语句* → [*表达式*](../chapter3/04_Expressions.html#expression) **;** _可选_
|
||||
> *语句* → [*声明*](../chapter3/05_Declarations.html#declaration) **;** _可选_
|
||||
> *语句* → [*循环语句*](../chapter3/10_Statements.html#loop_statement) **;** _可选_
|
||||
@ -38,6 +42,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 循环语句语法
|
||||
>
|
||||
> *循环语句* → [*for 语句*](../chapter3/10_Statements.html#for_statement)
|
||||
> *循环语句* → [*for-in 语句*](../chapter3/10_Statements.html#for_in_statement)
|
||||
> *循环语句* → [*while 语句*](../chapter3/10_Statements.html#wheetatype 类型 ile_statement)
|
||||
@ -46,6 +51,7 @@
|
||||
<!-- -->
|
||||
|
||||
> For 循环语法
|
||||
>
|
||||
> *for 语句* → **for** [*for 初始条件*](../chapter3/10_Statements.html#for_init) _可选_ **;** [*表达式*](../chapter3/04_Expressions.html#expression) _可选_ **;** [*表达式*](../chapter3/04_Expressions.html#expression) _可选_ [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
> *for 语句* → **for** **(** [*for 初始条件*](../chapter3/10_Statements.html#for_init) _可选_ **;** [*表达式*](../chapter3/04_Expressions.html#expression) _可选_ **;** [*表达式*](../chapter3/04_Expressions.html#expression) _可选_ **)** [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
> *for 初始条件* → [*变量声明*](../chapter3/05_Declarations.html#variable_declaration) | [*表达式集*](../chapter3/04_Expressions.html#expression_list)
|
||||
@ -53,62 +59,56 @@
|
||||
<!-- -->
|
||||
|
||||
> For-In 循环语法
|
||||
>
|
||||
> *for-in 语句* → **for case** _可选_ [*模式*](../chapter3/07_Patterns.html#pattern) **in** [*表达式*](../chapter3/04_Expressions.html#expression) [*代码块*](../chapter3/05_Declarations.html#code_block) [*where 从句*](TODO) _可选_
|
||||
|
||||
<!-- -->
|
||||
|
||||
> While 循环语法
|
||||
>
|
||||
> *while 语句* → **while** [*条件从句*](../chapter3/10_Statements.html#while_condition) [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
> *条件从句* → [*表达式*](TODO)
|
||||
|
||||
> *条件从句* → [*表达式*](TODO) *,* [*表达式集*]()
|
||||
|
||||
> *条件从句* → [*表达式集*](TODO)
|
||||
|
||||
> *条件从句* → [*可用条件 (availability-condition*)](TODO) *|* [*表达式集*]()
|
||||
|
||||
> *条件集* → [*条件*](TODO) *|* [*条件*](TODO) *,* [*条件集*]()
|
||||
|
||||
> *条件* → [*可用条件(availability-condition)*](TODO) *|* [*个例条件(case-condition)*](TODO) *|* [*可选绑定条件(optional-binding-condition)*](TODO)
|
||||
|
||||
> *个例条件(case-condition)* → **case** [*模式*](TODO) [*构造器*](TODO) [*where 从句*](TODO)_可选_
|
||||
|
||||
> *可选绑定条件(optional-binding-condition)* → [*可选绑定头(optional-binding-head)*](TODO) [*可选绑定连续集(optional-binding-continuation-list)*](TODO) _可选_ [*where 从句*](TODO) _可选_
|
||||
|
||||
> *可选绑定头(optional-binding-head)* → **let** [*模式 构造器*](TODO) *|* **var** [*模式 构造器*](TODO)
|
||||
|
||||
> *可选绑定连续集(optional-binding-contiuation-list)* → [*可选绑定连续(optional-binding-contiuation)*](TODO) *|* [*可选绑定连续(optional-binding-contiuation)*](TODO) *,* [*可选绑定连续集(optional-binding-contiuation-list)*](TODO)
|
||||
|
||||
> *可选绑定连续(optional-binding-continuation)* → [*模式 构造器*](TODO) *|* [*可选绑定头(optional-binding-head)*](TODO)
|
||||
|
||||
<!-- -->
|
||||
> Repeat-While 语句语法
|
||||
>
|
||||
*repeat-while-statement* → **repeat** [*代码块*](TODO) **while** [*表达式*](TODO)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 分支语句语法
|
||||
>
|
||||
> *分支语句* → [*if 语句*](../chapter3/10_Statements.html#if_statement)
|
||||
|
||||
> *分支语句* → [*guard 语句*](TODO)
|
||||
|
||||
> *分支语句* → [*switch 语句*](../chapter3/10_Statements.html#switch_statement)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> If 语句语法
|
||||
>
|
||||
> *if 语句* → **if** [*条件从句*](TODO) [*代码块*](TODO) [*else 从句(Clause)*](TODO) _可选_
|
||||
|
||||
> *else 从句(Clause)* → **else** [*代码块*](../chapter3/05_Declarations.html#code_block) | **else** [*if 语句*](../chapter3/10_Statements.html#if_statement)
|
||||
|
||||
<!-- -->
|
||||
> Guard 语句语法
|
||||
>
|
||||
> *guard 语句* → **guard** [*条件从句*](TODO) **else** [*代码块*](TODO)
|
||||
|
||||
|
||||
<!-- -->
|
||||
|
||||
> Switch 语句语法
|
||||
>
|
||||
> *switch 语句* → **switch** [*表达式*](../chapter3/04_Expressions.html#expression) **{** [*SwitchCase*](../chapter3/10_Statements.html#switch_cases) _可选_ **}**
|
||||
> *SwitchCase 集* → [*SwitchCase*](../chapter3/10_Statements.html#switch_case) [*SwitchCase 集*](../chapter3/10_Statements.html#switch_cases) _可选_
|
||||
> *SwitchCase* → [*case 标签*](../chapter3/10_Statements.html#case_label) [*多条语句(Statements)*](../chapter3/10_Statements.html#statements) | [*default 标签*](../chapter3/10_Statements.html#default_label) [*多条语句(Statements)*](../chapter3/10_Statements.html#statements)
|
||||
@ -122,6 +122,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 标记语句语法
|
||||
>
|
||||
> *标记语句(Labeled Statement)* → [*语句标签*](../chapter3/10_Statements.html#statement_label) [*循环语句*](../chapter3/10_Statements.html#loop_statement) | [*语句标签*](../chapter3/10_Statements.html#statement_label) [*if 语句*](../chapter3/10_Statements.html#switch_statement) | [*语句标签*](TODY) [*switch 语句*](TODY)
|
||||
> *语句标签* → [*标签名称*](../chapter3/10_Statements.html#label_name) **:**
|
||||
> *标签名称* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
@ -129,6 +130,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 控制传递语句(Control Transfer Statement) 语法
|
||||
>
|
||||
> *控制传递语句* → [*break 语句*](../chapter3/10_Statements.html#break_statement)
|
||||
> *控制传递语句* → [*continue 语句*](../chapter3/10_Statements.html#continue_statement)
|
||||
> *控制传递语句* → [*fallthrough 语句*](../chapter3/10_Statements.html#fallthrough_statement)
|
||||
@ -138,69 +140,65 @@
|
||||
<!-- -->
|
||||
|
||||
> Break 语句语法
|
||||
>
|
||||
> *break 语句* → **break** [*标签名称*](../chapter3/10_Statements.html#label_name) _可选_
|
||||
|
||||
<!-- -->
|
||||
|
||||
> Continue 语句语法
|
||||
>
|
||||
> *continue 语句* → **continue** [*标签名称*](../chapter3/10_Statements.html#label_name) _可选_
|
||||
|
||||
<!-- -->
|
||||
|
||||
> Fallthrough 语句语法
|
||||
>
|
||||
> *fallthrough 语句* → **fallthrough**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> Return 语句语法
|
||||
>
|
||||
> *return 语句* → **return** [*表达式*](../chapter3/04_Expressions.html#expression) _可选_
|
||||
|
||||
<!-- -->
|
||||
> 可用条件(Availability Condition)语法
|
||||
|
||||
>
|
||||
> *可用条件(availability-condition)* → **#available** **(** [*多可用参数*(availability-arguments)](TODO) **)**
|
||||
|
||||
> *多可用参数(availability- arguments)* → [*可用参数(availability-argument)*](TODO)|[*可用参数(availability-argument)*](TODO) , [多可用参数(availability-arguments)](TODO)
|
||||
|
||||
> *可用参数(availability- argument)* → [*平台名(platform-name)*](TODO) [*平台版本(platform-version)*](TODO)
|
||||
|
||||
> *可用参数(availability- argument)* → *
|
||||
|
||||
>
|
||||
> *平台名* → **iOS** | **iOSApplicationExtension**
|
||||
|
||||
> *平台名* → **OSX** | **OSXApplicationExtension**
|
||||
|
||||
> *平台名* → **OSX** | **macOSApplicationExtension**
|
||||
> *平台名* → **watchOS**
|
||||
|
||||
> *平台名* → **tvOS**
|
||||
> *平台版本* → [*十进制数(decimal-digits)*](TODO)
|
||||
|
||||
> *平台版本* → [*十进制数(decimal-digits)*](TODO) . [*十进制数(decimal-digits)*](TODO)
|
||||
|
||||
> *平台版本* → [*十进制数(decimal-digits)*](TODO) **.** [*十进制数(decimal-digits)*](TODO) **.** [*十进制数(decimal-digits)*](TODO))
|
||||
|
||||
<!-- -->
|
||||
> 抛出语句(Throw Statement)语法
|
||||
|
||||
>
|
||||
> *抛出语句(throw-statement)* → **throw** [*表达式(expression)*](TODO)
|
||||
|
||||
<!-- -->
|
||||
> 延迟语句 (defer-statement)语法
|
||||
|
||||
>
|
||||
> *延迟语句(defer-statement)* → **defer** [*代码块*](TODO)
|
||||
|
||||
<!-- -->
|
||||
> 执行语句(do-statement)语法
|
||||
|
||||
>
|
||||
> *执行语句(do-statement)* → **do** [*代码块*](TODO) [*catch-clauses*](TODO) _可选_
|
||||
|
||||
> *catch-clauses* → [*catch-clause*](TODO) [*catch-clauses*](TODO) _可选_
|
||||
|
||||
> *catch-clauses* → **catch** [*模式(pattern)*](TODO) _可选_ [*where-clause*](TODO) _可选_ [*代码块(code-block)*](TODO) _可选_
|
||||
|
||||
<a name="generic_parameters_and_arguments"></a>
|
||||
## 泛型参数
|
||||
|
||||
> 泛型形参从句(Generic Parameter Clause) 语法
|
||||
>
|
||||
> *泛型参数从句* → **<** [*泛型参数集*](GenericParametersAndArguments.html#generic_parameter_list) [*约束从句*](GenericParametersAndArguments.html#requirement_clause) _可选_ **>**
|
||||
> *泛型参数集* → [*泛形参数*](GenericParametersAndArguments.html#generic_parameter) | [*泛形参数*](GenericParametersAndArguments.html#generic_parameter) **,** [*泛型参数集*](GenericParametersAndArguments.html#generic_parameter_list)
|
||||
> *泛形参数* → [*类型名称*](../chapter3/03_Types.html#type_name)
|
||||
@ -216,6 +214,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 泛型实参从句语法
|
||||
>
|
||||
> *(泛型参数从句 Generic Argument Clause)* → **<** [*泛型参数集*](GenericParametersAndArguments.html#generic_argument_list) **>**
|
||||
> *泛型参数集* → [*泛型参数*](GenericParametersAndArguments.html#generic_argument) | [*泛型参数*](GenericParametersAndArguments.html#generic_argument) **,** [*泛型参数集*](GenericParametersAndArguments.html#generic_argument_list)
|
||||
> *泛型参数* → [*类型*](../chapter3/03_Types.html#type)
|
||||
@ -224,6 +223,7 @@
|
||||
## 声明 (Declarations)
|
||||
|
||||
> 声明语法
|
||||
>
|
||||
> *声明* → [*导入声明*](../chapter3/05_Declarations.html#import_declaration)
|
||||
> *声明* → [*常量声明*](../chapter3/05_Declarations.html#constant_declaration)
|
||||
> *声明* → [*变量声明*](../chapter3/05_Declarations.html#variable_declaration)
|
||||
@ -244,16 +244,19 @@
|
||||
<!-- -->
|
||||
|
||||
> 顶级(Top Level) 声明语法
|
||||
>
|
||||
> *顶级声明* → [*多条语句(Statements)*](../chapter3/10_Statements.html#statements) _可选_
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 代码块语法
|
||||
>
|
||||
> *代码块* → **{** [*多条语句(Statements)*](../chapter3/10_Statements.html#statements) _可选_ **}**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 导入(Import)声明语法
|
||||
>
|
||||
> *导入声明* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ **import** [*导入类型*](../chapter3/05_Declarations.html#import_kind) _可选_ [*导入路径*](../chapter3/05_Declarations.html#import_path)
|
||||
> *导入类型* → **typealias** | **struct** | **class** | **enum** | **protocol** | **var** | **func**
|
||||
> *导入路径* → [*导入路径标识符*](../chapter3/05_Declarations.html#import_path_identifier) | [*导入路径标识符*](../chapter3/05_Declarations.html#import_path_identifier) **.** [*导入路径*](../chapter3/05_Declarations.html#import_path)
|
||||
@ -262,14 +265,16 @@
|
||||
<!-- -->
|
||||
|
||||
> 常数声明语法
|
||||
>
|
||||
> *常量声明* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*声明修改符(Modifiers)集*](../chapter3/05_Declarations.html#declaration_specifiers) _可选_ **let** [*模式构造器集*](../chapter3/05_Declarations.html#pattern_initializer_list)
|
||||
> *模式构造器集* → [*模式构造器*](../chapter3/05_Declarations.html#pattern_initializer) | [*模式构造器*](../chapter3/05_Declarations.html#pattern_initializer) **,** [*模式构造器集*](../chapter3/05_Declarations.html#pattern_initializer_list)
|
||||
> *模式构造器* → [*模式*](../chapter3/07_Patterns.html#pattern) [*构造器*](../chapter3/05_Declarations.html#initializer) _可选_
|
||||
> *模式构造器* → [*模式*](../ chapter3/07_Patterns.html#pattern) [*构造器*](../chapter3/05_Declarations.html#initializer) _可选_
|
||||
> *构造器* → **=** [*表达式*](../chapter3/04_Expressions.html#expression)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 变量声明语法
|
||||
>
|
||||
> *变量声明* → [*变量声明头(Head)*](../chapter3/05_Declarations.html#variable_declaration_head) [*模式构造器集*](../chapter3/05_Declarations.html#pattern_initializer_list)
|
||||
> *变量声明* → [*变量声明头(Head)*](../chapter3/05_Declarations.html#variable_declaration_head) [*变量名*](../chapter3/05_Declarations.html#variable_name) [*类型注解*](../chapter3/03_Types.html#type_annotation) [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
> *变量声明* → [*变量声明头(Head)*](../chapter3/05_Declarations.html#variable_declaration_head) [*变量名*](../chapter3/05_Declarations.html#variable_name) [*类型注解*](../chapter3/03_Types.html#type_annotation) [*getter-setter 块*](../chapter3/05_Declarations.html#getter_setter_block)
|
||||
@ -289,11 +294,13 @@
|
||||
> *willSet-didSet 代码块* → **{** [*willSet 从句*](../chapter3/05_Declarations.html#willSet_clause) [*didSet 从句*](../chapter3/05_Declarations.html#didSet_clause) _可选_ **}**
|
||||
> *willSet-didSet 代码块* → **{** [*didSet 从句*](../chapter3/05_Declarations.html#didSet_clause) [*willSet 从句*](../chapter3/05_Declarations.html#willSet_clause) **}**
|
||||
> *willSet 从句* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ **willSet** [*setter 名称*](../chapter3/05_Declarations.html#setter_name) _可选_ [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
> *didSet 从句* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ **didSet** [*setter 名称*](../chapter3/05_Declarations.html#setter_name) _可选_ [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
> *didSet 从句* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_
|
||||
**didSet** [*setter 名称*](../chapter3/05_Declarations.html#setter_name) _可选_ [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 类型别名声明语法
|
||||
>
|
||||
> *类型别名声明* → [*类型别名头(Head)*](../chapter3/05_Declarations.html#typealias_head) [*类型别名赋值*](../chapter3/05_Declarations.html#typealias_assignment)
|
||||
> *类型别名头(Head)* → [*属性*](TODO) _可选_ [*访问级别修改符(access-level-modifier)*](TODO) **typealias** [*类型别名名称*](../chapter3/05_Declarations.html#typealias_name)
|
||||
> *类型别名名称* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
@ -302,6 +309,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 函数声明语法
|
||||
>
|
||||
> *函数声明* → [*函数头*](../chapter3/05_Declarations.html#function_head) [*函数名*](../chapter3/05_Declarations.html#function_name) [*泛型参数从句*](GenericParametersAndArguments.html#generic_parameter_clause) _可选_ [*函数签名(Signature)*](../chapter3/05_Declarations.html#function_signature) [*函数体*](../chapter3/05_Declarations.html#function_body)
|
||||
> *函数头* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*声明描述符(Specifiers)集*](../chapter3/05_Declarations.html#declaration_specifiers) _可选_ **func**
|
||||
> *函数名* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier) | [*运算符*](../chapter3/02_Lexical_Structure.html#operator)
|
||||
@ -323,16 +331,12 @@
|
||||
<!-- -->
|
||||
|
||||
> 枚举声明语法
|
||||
>
|
||||
> *枚举声明* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*访问级别修改器(access-level-modifier)*](TODO) _可选_ [*联合式枚举*](../chapter3/05_Declarations.html#union_style_enum)
|
||||
|
||||
> *枚举声明* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*访问级别修改器(access-level-modifier)*](TODO) _可选_ [*原始值式枚举(raw-value-style-enum)*](TODO)
|
||||
|
||||
> *联合式枚举* → **enum** [*枚举名*](../chapter3/05_Declarations.html#enum_name) [*泛型参数从句*](GenericParametersAndArguments.html#generic_parameter_clause) _可选_ [*类型继承从句(type-inheritance-clause)*](TODO) _可选_ **{** [*联合样式枚举成员*](../chapter3/05_Declarations.html#union_style_enum_members) _可选_ **}**
|
||||
|
||||
> *联合样式枚举成员* → [*union-style-enum-member*](../chapter3/05_Declarations.html#union_style_enum_member) [*联合样式枚举成员*](../chapter3/05_Declarations.html#union_style_enum_members) _可选_
|
||||
|
||||
> *联合样式枚举成员* → [*声明*](../chapter3/05_Declarations.html#declaration) | [*联合式(Union Style)的枚举 case 从句*](../chapter3/05_Declarations.html#union_style_enum_case_clause)
|
||||
|
||||
> *联合式(Union Style)的枚举 case 从句* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ **case** [*联合式(Union Style)的枚举 case 集*](../chapter3/05_Declarations.html#union_style_enum_case_list)
|
||||
> *联合式(Union Style)的枚举 case 集* → [*联合式(Union Style)的 case*](../chapter3/05_Declarations.html#union_style_enum_case) | [*联合式(Union Style)的 case*](../chapter3/05_Declarations.html#union_style_enum_case) **,** [*联合式(Union Style)的枚举 case 集*](../chapter3/05_Declarations.html#union_style_enum_case_list)
|
||||
> *联合式(Union Style)的枚举 case* → [*枚举的 case 名*](../chapter3/05_Declarations.html#enum_case_name) [*元组类型*](../chapter3/03_Types.html#tuple_type) _可选_
|
||||
@ -350,6 +354,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 结构体声明语法
|
||||
>
|
||||
> *结构体声明* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*访问级别修改器(access-level-modifier)*](TODO) _可选_ **struct** [*结构体名称*](../chapter3/05_Declarations.html#struct_name) [*泛型参数从句*](GenericParametersAndArguments.html#generic_parameter_clause) _可选_ [*类型继承从句*](../chapter3/03_Types.html#type_inheritance_clause) _可选_ [*结构体主体*](../chapter3/05_Declarations.html#struct_body)
|
||||
> *结构体名称* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
> *结构体主体* → **{** [*声明(Declarations)集*](../chapter3/05_Declarations.html#declarations) _可选_ **}**
|
||||
@ -357,6 +362,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 类声明语法
|
||||
>
|
||||
> *类声明* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*访问级别修改器(access-level-modifier)*](TODO) **class** [*类名*](../chapter3/05_Declarations.html#class_name) [*泛型参数从句*](GenericParametersAndArguments.html#generic_parameter_clause) _可选_ [*类型继承从句*](../chapter3/03_Types.html#type_inheritance_clause) _可选_ [*类主体*](../chapter3/05_Declarations.html#class_body)
|
||||
> *类名* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
> *类主体* → **{** [*声明(Declarations)集*](../chapter3/05_Declarations.html#declarations) _可选_ **}**
|
||||
@ -364,6 +370,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 协议(Protocol)声明语法
|
||||
>
|
||||
> *协议声明* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_[*访问级别修改器(access-level-modifier)*](TODO) **protocol** [*协议名*](../chapter3/05_Declarations.html#protocol_name) [*类型继承从句*](../chapter3/03_Types.html#type_inheritance_clause) _可选_ [*协议主体*](../chapter3/05_Declarations.html#protocol_body)
|
||||
> *协议名* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
> *协议主体* → **{** [*协议成员声明(Declarations)集*](../chapter3/05_Declarations.html#protocol_member_declarations) _可选_ **}**
|
||||
@ -377,53 +384,60 @@
|
||||
<!-- -->
|
||||
|
||||
> 协议属性声明语法
|
||||
>
|
||||
> *协议属性声明* → [*变量声明头(Head)*](../chapter3/05_Declarations.html#variable_declaration_head) [*变量名*](../chapter3/05_Declarations.html#variable_name) [*类型注解*](../chapter3/03_Types.html#type_annotation) [*getter-setter 关键字(Keyword)块*](../chapter3/05_Declarations.html#getter_setter_keyword_block)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 协议方法声明语法
|
||||
>
|
||||
> *协议方法声明* → [*函数头*](../chapter3/05_Declarations.html#function_head) [*函数名*](../chapter3/05_Declarations.html#function_name) [*泛型参数从句*](GenericParametersAndArguments.html#generic_parameter_clause) _可选_ [*函数签名(Signature)*](../chapter3/05_Declarations.html#function_signature)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 协议构造器声明语法
|
||||
>
|
||||
> *协议构造器声明* → [*构造器头(Head)*](../chapter3/05_Declarations.html#initializer_head) [*泛型参数从句*](GenericParametersAndArguments.html#generic_parameter_clause) _可选_ [*参数从句*](../chapter3/05_Declarations.html#parameter_clause)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 协议下标声明语法
|
||||
>
|
||||
> *协议下标声明* → [*下标头(Head)*](../chapter3/05_Declarations.html#subscript_head) [*下标结果(Result)*](../chapter3/05_Declarations.html#subscript_result) [*getter-setter 关键字(Keyword)块*](../chapter3/05_Declarations.html#getter_setter_keyword_block)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 协议关联类型声明语法
|
||||
>
|
||||
> *协议关联类型声明* → [*类型别名头(Head)*](../chapter3/05_Declarations.html#typealias_head) [*类型继承从句*](../chapter3/03_Types.html#type_inheritance_clause) _可选_ [*类型别名赋值*](../chapter3/05_Declarations.html#typealias_assignment) _可选_
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 构造器声明语法
|
||||
>
|
||||
> *构造器声明* → [*构造器头(Head)*](../chapter3/05_Declarations.html#initializer_head) [*泛型参数从句*](GenericParametersAndArguments.html#generic_parameter_clause) _可选_ [*参数从句*](../chapter3/05_Declarations.html#parameter_clause) [*构造器主体*](../chapter3/05_Declarations.html#initializer_body)
|
||||
> *构造器头(Head)* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*声明修改器集(declaration-modifiers)*](TODO) _可选_ **init**
|
||||
> *构造器头(Head)* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*声明修改器集(declaration-modifiers)*](TODO) _可选_ **init ?**
|
||||
|
||||
> *构造器头(Head)* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*声明修改器集(declaration-modifiers)*](TODO) _可选_ **init !**
|
||||
|
||||
> *构造器主体* → [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 析构器声明语法
|
||||
>
|
||||
> *析构器声明* → [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ **deinit** [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 扩展(Extension)声明语法
|
||||
>
|
||||
> *扩展声明* → [*访问级别修改器*](TODO) _可选_ **extension** [*类型标识*](../chapter3/03_Types.html#type_identifier) [*类型继承从句*](../chapter3/03_Types.html#type_inheritance_clause) _可选_ [*extension-body*](../chapter3/05_Declarations.html#extension_body)
|
||||
> *extension-body* → **{** [*声明(Declarations)集*](../chapter3/05_Declarations.html#declarations) _可选_ **}**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 下标声明语法
|
||||
>
|
||||
> *下标声明* → [*下标头(Head)*](../chapter3/05_Declarations.html#subscript_head) [*下标结果(Result)*](../chapter3/05_Declarations.html#subscript_result) [*代码块*](../chapter3/05_Declarations.html#code_block)
|
||||
> *下标声明* → [*下标头(Head)*](../chapter3/05_Declarations.html#subscript_head) [*下标结果(Result)*](../chapter3/05_Declarations.html#subscript_result) [*getter-setter 块*](../chapter3/05_Declarations.html#getter_setter_block)
|
||||
> *下标声明* → [*下标头(Head)*](../chapter3/05_Declarations.html#subscript_head) [*下标结果(Result)*](../chapter3/05_Declarations.html#subscript_result) [*getter-setter 关键字(Keyword)块*](../chapter3/05_Declarations.html#getter_setter_keyword_block)
|
||||
@ -433,6 +447,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 运算符声明语法
|
||||
>
|
||||
> *运算符声明* → [*前置运算符声明*](../chapter3/05_Declarations.html#prefix_operator_declaration) | [*后置运算符声明*](../chapter3/05_Declarations.html#postfix_operator_declaration) | [*中置运算符声明*](../chapter3/05_Declarations.html#infix_operator_declaration)
|
||||
> *前置运算符声明* → **prefix** **运算符** [*运算符*](../chapter3/02_Lexical_Structure.html#operator) **{** **}**
|
||||
> *后置运算符声明* → **postfix** **运算符** [*运算符*](../chapter3/02_Lexical_Structure.html#operator) **{** **}**
|
||||
@ -444,31 +459,27 @@
|
||||
> *结和性* → **left** | **right** | **none**
|
||||
|
||||
<!-- -->
|
||||
声明修改器语法
|
||||
> 声明修改器语法
|
||||
>
|
||||
> *声明修改器* → **类** | **便捷(convenience)** | **动态(dynamic)** | **final** | **中置(infix)** | **lazy** | **可变(mutating)** | **不可变(nonmutating)** | **可选(optional)** | **改写(override)** | **后置** | **前置** | **required** | **static** | **unowned** | **unowned(safe)** | **unowned(unsafe)** | **弱(weak)**
|
||||
|
||||
> *声明修改器* → [*访问级别声明器(access-level-modifier)*](TODO)
|
||||
|
||||
> *声明修改集* → [*声明修改器*](TODO) [*声明修改器集*](TODO) _可选_
|
||||
|
||||
> *访问级别修改器* → **内部的** | **内部的(set)**
|
||||
|
||||
> *访问级别修改器* → **私有的** | **私有的(set)**
|
||||
|
||||
> *访问级别修改器* → **公共的**
|
||||
| **公共的(set)**
|
||||
|
||||
> *访问级别修改器集* →[*访问级别修改器*](TODO) [*访问级别修改器集*](TODO) _可选_
|
||||
|
||||
<a name="patterns"></a>
|
||||
## 模式
|
||||
|
||||
> 模式(Patterns) 语法
|
||||
>
|
||||
> *模式* → [*通配符模式*](../chapter3/07_Patterns.html#wildcard_pattern) [*类型注解*](../chapter3/03_Types.html#type_annotation) _可选_
|
||||
> *模式* → [*标识符模式*](../chapter3/07_Patterns.html#identifier_pattern) [*类型注解*](../chapter3/03_Types.html#type_annotati Value Bindingon ) _可选_
|
||||
> *模式* → [*值绑定模式*](../chapter3/07_Patterns.html#value_binding_pattern)
|
||||
> *模式* → [*元组模式*](../chapter3/07_Patterns.html#tuple_pattern) [*类型注解*](../chapter3/03_Types.html#type_annotation) _可选_
|
||||
|
||||
>
|
||||
> *模式* → [*枚举个例模式*](../chapter3/07_Patterns.html#enum_case_pattern)
|
||||
> *模式* → [*可选模式*](TODO)
|
||||
> *模式* → [*类型转换模式*](../chapter3/07_Patterns.html#type_casting_pattern)
|
||||
@ -477,21 +488,25 @@
|
||||
<!-- -->
|
||||
|
||||
> 通配符模式语法
|
||||
>
|
||||
> *通配符模式* → **_**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 标识符模式语法
|
||||
>
|
||||
> *标识符模式* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 值绑定(Value Binding)模式语法
|
||||
>
|
||||
> *值绑定模式* → **var** [*模式*](../chapter3/07_Patterns.html#pattern) | **let** [*模式*](../chapter3/07_Patterns.html#pattern)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 元组模式语法
|
||||
>
|
||||
> *元组模式* → **(** [*元组模式元素集*](../chapter3/07_Patterns.html#tuple_pattern_element_list) _可选_ **)**
|
||||
> *元组模式元素集* → [*元组模式元素*](../chapter3/07_Patterns.html#tuple_pattern_element) | [*元组模式元素*](../chapter3/07_Patterns.html#tuple_pattern_element) **,** [*元组模式元素集*](../chapter3/07_Patterns.html#tuple_pattern_element_list)
|
||||
> *元组模式元素* → [*模式*](../chapter3/07_Patterns.html#pattern)
|
||||
@ -499,15 +514,18 @@
|
||||
<!-- -->
|
||||
|
||||
> 枚举用例模式语法
|
||||
>
|
||||
> *enum-case-pattern* → [*类型标识*](../chapter3/03_Types.html#type_identifier) _可选_ **.** [*枚举的 case 名*](../chapter3/05_Declarations.html#enum_case_name) [*元组模式*](../chapter3/07_Patterns.html#tuple_pattern) _可选_
|
||||
|
||||
<!-- -->
|
||||
> 可选模式语法
|
||||
>
|
||||
> *可选模式* → [*识别符模式*](TODO) **?**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 类型转换模式语法
|
||||
>
|
||||
> *类型转换模式(type-casting-pattern)* → [*is 模式*](../chapter3/07_Patterns.html#is_pattern) | [*as 模式*](../chapter3/07_Patterns.html#as_pattern)
|
||||
> *is 模式* → **is** [*类型*](../chapter3/03_Types.html#type)
|
||||
> *as 模式* → [*模式*](../chapter3/07_Patterns.html#pattern) **as** [*类型*](../chapter3/03_Types.html#type)
|
||||
@ -515,12 +533,14 @@
|
||||
<!-- -->
|
||||
|
||||
> 表达式模式语法
|
||||
>
|
||||
> *表达式模式* → [*表达式*](../chapter3/04_Expressions.html#expression)
|
||||
|
||||
<a name="attributes"></a>
|
||||
## 属性
|
||||
|
||||
> 属性语法
|
||||
>
|
||||
> *属性* → **@** [*属性名*](../chapter3/06_Attributes.html#attribute_name) [*属性参数从句*](../chapter3/06_Attributes.html#attribute_argument_clause) _可选_
|
||||
> *属性名* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
> *属性参数从句* → **(** [*平衡令牌集*](../chapter3/06_Attributes.html#balanced_tokens) _可选_ **)**
|
||||
@ -536,23 +556,27 @@
|
||||
## 表达式
|
||||
|
||||
> 表达式语法
|
||||
>
|
||||
> *表达式* → [*try-operator*](TODO) _可选_ [*前置表达式*](../chapter3/04_Expressions.html#prefix_expression) [*二元表达式集*](../chapter3/04_Expressions.html#binary_expressions) _可选_
|
||||
> *表达式集* → [*表达式*](../chapter3/04_Expressions.html#expression) | [*表达式*](../chapter3/04_Expressions.html#expression) **,** [*表达式集*](../chapter3/04_Expressions.html#expression_list)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 前置表达式语法
|
||||
>
|
||||
> *前置表达式* → [*前置运算符*](../chapter3/02_Lexical_Structure.html#prefix_operator) _可选_ [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression)
|
||||
> *前置表达式* → [*写入写出(in-out)表达式*](../chapter3/04_Expressions.html#in_out_expression)
|
||||
> *写入写出(in-out)表达式* → **&** [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
|
||||
<!-- -->
|
||||
> try 表达式语法
|
||||
>
|
||||
> *try-operator* → **try** | **try !**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 二元表达式语法
|
||||
>
|
||||
> *二元表达式* → [*二元运算符*](../chapter3/02_Lexical_Structure.html#binary_operator) [*前置表达式*](../chapter3/04_Expressions.html#prefix_expression)
|
||||
> *二元表达式* → [*赋值运算符*](../chapter3/04_Expressions.html#assignment_operator) [*try 运算符*](TODO) _可选_ [*前置表达式*](../chapter3/04_Expressions.html#prefix_expression)
|
||||
> *二元表达式* → [*条件运算符*](../chapter3/04_Expressions.html#conditional_operator) [*try 运算符*](TODO) _可选_ [*前置表达式*](../chapter3/04_Expressions.html#prefix_expression)
|
||||
@ -562,27 +586,28 @@
|
||||
<!-- -->
|
||||
|
||||
> 赋值运算符语法
|
||||
>
|
||||
> *赋值运算符* → **=**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 三元条件运算符语法
|
||||
>
|
||||
> *三元条件运算符* → **?** [*表达式*](../chapter3/04_Expressions.html#expression) **:**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 类型转换运算符语法
|
||||
>
|
||||
> *类型转换运算符* → **is** [*类型*](../chapter3/03_Types.html#type)
|
||||
|
||||
> *类型转换运算符* → **as** [*类型*](../chapter3/03_Types.html#type)
|
||||
|
||||
> *类型转换运算符* → **as ?** [*类型*](../chapter3/03_Types.html#type)
|
||||
|
||||
> *类型转换运算符* → **as !** [*类型*](../chapter3/03_Types.html#type)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 主表达式语法
|
||||
>
|
||||
> *主表达式* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier) [*泛型参数从句*](GenericParametersAndArguments.html#generic_argument_clause) _可选_
|
||||
> *主表达式* → [*字面量表达式*](../chapter3/04_Expressions.html#literal_expression)
|
||||
> *主表达式* → [*self 表达式*](../chapter3/04_Expressions.html#self_expression)
|
||||
@ -595,6 +620,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 字面量表达式语法
|
||||
>
|
||||
> *字面量表达式* → [*字面量*](../chapter3/02_Lexical_Structure.html#literal)
|
||||
> *字面量表达式* → [*数组字面量*](../chapter3/04_Expressions.html#array_literal) | [*字典字面量*](../chapter3/04_Expressions.html#dictionary_literal)
|
||||
> *字面量表达式* → **__FILE__** | **__LINE__** | **__COLUMN__** | **__FUNCTION__**
|
||||
@ -608,6 +634,7 @@
|
||||
<!-- -->
|
||||
|
||||
> Self 表达式语法
|
||||
>
|
||||
> *self 表达式* → **self**
|
||||
> *self 表达式* → **self** **.** [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
> *self 表达式* → **self** **[** [*表达式*](../chapter3/04_Expressions.html#expression) **]**
|
||||
@ -616,6 +643,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 超类表达式语法
|
||||
>
|
||||
> *超类表达式* → [*超类方法表达式*](../chapter3/04_Expressions.html#superclass_method_expression) | [*超类下标表达式*](../chapter3/04_Expressions.html#超类下标表达式) | [*超类构造器表达式*](../chapter3/04_Expressions.html#superclass_initializer_expression)
|
||||
> *超类方法表达式* → **super** **.** [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
> *超类下标表达式* → **super** **[** [*表达式*](../chapter3/04_Expressions.html#expression) **]**
|
||||
@ -624,6 +652,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 闭包表达式语法
|
||||
>
|
||||
> *闭包表达式* → **{** [*闭包签名(Signational)*](../chapter3/04_Expressions.html#closure_signature) _可选_ [*多条语句(Statements)*](../chapter3/10_Statements.html#statements) **}**
|
||||
> *闭包签名(Signational)* → [*参数从句*](../chapter3/05_Declarations.html#parameter_clause) [*函数结果*](../chapter3/05_Declarations.html#function_result) _可选_ **in**
|
||||
> *闭包签名(Signational)* → [*标识符集*](../chapter3/02_Lexical_Structure.html#identifier_list) [*函数结果*](../chapter3/05_Declarations.html#function_result) _可选_ **in**
|
||||
@ -636,11 +665,13 @@
|
||||
<!-- -->
|
||||
|
||||
> 隐式成员表达式语法
|
||||
>
|
||||
> *隐式成员表达式* → **.** [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 圆括号表达式(Parenthesized Expression)语法
|
||||
>
|
||||
> *圆括号表达式* → **(** [*表达式元素集*](../chapter3/04_Expressions.html#expression_element_list) _可选_ **)**
|
||||
> *表达式元素集* → [*表达式元素*](../chapter3/04_Expressions.html#expression_element) | [*表达式元素*](../chapter3/04_Expressions.html#expression_element) **,** [*表达式元素集*](../chapter3/04_Expressions.html#expression_element_list)
|
||||
> *表达式元素* → [*表达式*](../chapter3/04_Expressions.html#expression) | [*标识符*](../chapter3/02_Lexical_Structure.html#identifier) **:** [*表达式*](../chapter3/04_Expressions.html#expression)
|
||||
@ -648,11 +679,13 @@
|
||||
<!-- -->
|
||||
|
||||
> 通配符表达式语法
|
||||
>
|
||||
> *通配符表达式* → **_**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 后置表达式语法
|
||||
>
|
||||
> *后置表达式* → [*主表达式*](../chapter3/04_Expressions.html#primary_expression)
|
||||
> *后置表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) [*后置运算符*](../chapter3/02_Lexical_Structure.html#postfix_operator)
|
||||
> *后置表达式* → [*函数调用表达式*](../chapter3/04_Expressions.html#function_call_expression)
|
||||
@ -667,6 +700,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 函数调用表达式语法
|
||||
>
|
||||
> *函数调用表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) [*圆括号表达式*](../chapter3/04_Expressions.html#parenthesized_expression)
|
||||
> *函数调用表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) [*圆括号表达式*](../chapter3/04_Expressions.html#parenthesized_expression) _可选_ [*后置闭包(Trailing Closure)*](../chapter3/04_Expressions.html#trailing_closure)
|
||||
> *后置闭包(Trailing Closure)* → [*闭包表达式*](../chapter3/04_Expressions.html#closure_expression)
|
||||
@ -674,51 +708,57 @@
|
||||
<!-- -->
|
||||
|
||||
> 构造器表达式语法
|
||||
>
|
||||
> *构造器表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) **.** **init**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 显式成员表达式语法
|
||||
>
|
||||
> *显示成员表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) **.** [*十进制数字*](../chapter3/02_Lexical_Structure.html#decimal_digit)
|
||||
> *显示成员表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) **.** [*标识符*](../chapter3/02_Lexical_Structure.html#identifier) [*泛型参数从句*](GenericParametersAndArguments.html#generic_argument_clause) _可选_
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 后置 Self 表达式语法
|
||||
>
|
||||
> *后置 self 表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) **.** **self**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 动态类型表达式语法
|
||||
>
|
||||
> *动态类型表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) **.** **dynamicType**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 附属脚本表达式语法
|
||||
>
|
||||
> *附属脚本表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) **[** [*表达式集*](../chapter3/04_Expressions.html#expression_list) **]**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 强制取值(Forced Value)语法
|
||||
>
|
||||
> *强制取值(Forced Value)表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) **!**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 可选链表达式语法
|
||||
>
|
||||
> *可选链表达式* → [*后置表达式*](../chapter3/04_Expressions.html#postfix_expression) **?**
|
||||
|
||||
<a name="lexical_structure"></a>
|
||||
## 词法结构
|
||||
|
||||
> 标识符语法
|
||||
>
|
||||
> *标识符* → [*标识符头(Head)*](../chapter3/02_Lexical_Structure.html#identifier_head) [*标识符字符集*](../chapter3/02_Lexical_Structure.html#identifier_characters) _可选_
|
||||
> *标识符* → [*标识符头(Head)*](../chapter3/02_Lexical_Structure.html#identifier_head) [*标识符字符集*](../chapter3/02_Lexical_Structure.html#identifier_characters) _可选_
|
||||
> *标识符* → [*隐式参数名*](../chapter3/02_Lexical_Structure.html#implicit_parameter_name)
|
||||
> *标识符集* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier) | [*标识符*](../chapter3/02_Lexical_Structure.html#identifier) **,** [*标识符集*](../chapter3/02_Lexical_Structure.html#identifier_list)
|
||||
> *标识符头(Head)* → Upper- or lowercase letter A through Z
|
||||
|
||||
> *标识符头(Head)* → _
|
||||
|
||||
> *标识符头(Head)* → U+00A8, U+00AA, U+00AD, U+00AF, U+00B2–U+00B5, or U+00B7–U+00BA
|
||||
> *标识符头(Head)* → U+00BC–U+00BE, U+00C0–U+00D6, U+00D8–U+00F6, or U+00F8–U+00FF
|
||||
> *标识符头(Head)* → U+0100–U+02FF, U+0370–U+167F, U+1681–U+180D, or U+180F–U+1DBF
|
||||
@ -742,17 +782,16 @@
|
||||
<!-- -->
|
||||
|
||||
> 字面量语法
|
||||
>
|
||||
> *字面量* → [*数值型字面量*](../chapter3/02_Lexical_Structure.html#integer_literal) | [*字符串字面量*](../chapter3/02_Lexical_Structure.html#floating_point_literal) | [*布尔字面量*](../chapter3/02_Lexical_Structure.html#string_literal) | [*空字面量*](TODO)
|
||||
|
||||
> *数值型字面量* → **-** _可选_ [*整形字面量*](TODO) | **-** _可选_ [*浮点型字面量*](TODO)
|
||||
|
||||
> *布尔字面量* → **true** | **false**
|
||||
|
||||
> *空字面量* → **nil**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 整型字面量语法
|
||||
>
|
||||
> *整型字面量* → [*二进制字面量*](../chapter3/02_Lexical_Structure.html#binary_literal)
|
||||
> *整型字面量* → [*八进制字面量*](../chapter3/02_Lexical_Structure.html#octal_literal)
|
||||
> *整型字面量* → [*十进制字面量*](../chapter3/02_Lexical_Structure.html#decimal_literal)
|
||||
@ -778,6 +817,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 浮点型字面量语法
|
||||
>
|
||||
> *浮点数字面量* → [*十进制字面量*](../chapter3/02_Lexical_Structure.html#decimal_literal) [*十进制分数*](../chapter3/02_Lexical_Structure.html#decimal_fraction) _可选_ [*十进制指数*](../chapter3/02_Lexical_Structure.html#decimal_exponent) _可选_
|
||||
> *浮点数字面量* → [*十六进制字面量*](../chapter3/02_Lexical_Structure.html#hexadecimal_literal) [*十六进制分数*](../chapter3/02_Lexical_Structure.html#hexadecimal_fraction) _可选_ [*十六进制指数*](../chapter3/02_Lexical_Structure.html#hexadecimal_exponent)
|
||||
> *十进制分数* → **.** [*十进制字面量*](../chapter3/02_Lexical_Structure.html#decimal_literal)
|
||||
@ -792,6 +832,7 @@
|
||||
<!-- -->
|
||||
|
||||
> 字符串型字面量语法
|
||||
>
|
||||
> *字符串字面量* → **"** [*引用文本*](../chapter3/02_Lexical_Structure.html#quoted_text) **"**
|
||||
> *引用文本* → [*引用文本条目*](../chapter3/02_Lexical_Structure.html#quoted_text_item) [*引用文本*](../chapter3/02_Lexical_Structure.html#quoted_text) _可选_
|
||||
> *引用文本条目* → [*转义字符*](../chapter3/02_Lexical_Structure.html#escaped_character)
|
||||
@ -804,59 +845,35 @@
|
||||
<!-- -->
|
||||
|
||||
> 运算符语法语法
|
||||
>
|
||||
> *运算符* → [*运算符头*](../chapter3/02_Lexical_Structure.html#operator_character) [*运算符字符集*](../chapter3/02_Lexical_Structure.html#operator) _可选_
|
||||
> *运算符* → [*点运算符头*](TODO) [*点运算符字符集*](TODO) _可选_
|
||||
> *运算符字符* → **/** | **=** | **-** | **+** | **!** | ***** | **%** | **<** | **>** | **&** | **|** | **^** | **~** | **?**
|
||||
> *运算符头* → U+00A1–U+00A7
|
||||
|
||||
> *运算符头* → U+00A9 or U+00AB
|
||||
|
||||
> *运算符头* → U+00AC or U+00AE
|
||||
|
||||
> *运算符头* → U+00B0–U+00B1, U+00B6, U+00BB, U+00BF, U+00D7, or U+00F7
|
||||
|
||||
> *运算符头* → U+2016–U+2017 or U+2020–U+2027
|
||||
|
||||
> *运算符头* → U+2030–U+203E
|
||||
|
||||
> *运算符头* → U+2041–U+2053
|
||||
|
||||
> *运算符头* → U+2055–U+205E
|
||||
|
||||
> *运算符头* → U+2190–U+23FF
|
||||
|
||||
> *运算符头* → U+2500–U+2775
|
||||
|
||||
> *运算符头* → U+2794–U+2BFF
|
||||
|
||||
> *运算符头* → U+2E00–U+2E7F
|
||||
|
||||
> *运算符头* → U+3001–U+3003
|
||||
|
||||
> *运算符头* → U+3008–U+3030
|
||||
|
||||
> *运算符字符* → [*运算符头*](TODO)
|
||||
|
||||
> *运算符字符* → U+0300–U+036F
|
||||
|
||||
> *运算符字符* → U+1DC0–U+1DFF
|
||||
|
||||
> *运算符字符* → U+20D0–U+20FF
|
||||
|
||||
> *运算符字符* → U+FE00–U+FE0F
|
||||
|
||||
> *运算符字符* → U+FE20–U+FE2F
|
||||
|
||||
> *运算符字符* → U+E0100–U+E01EF
|
||||
|
||||
> *运算符字符集* → [*运算符字符*](TODO) [*运算符字符集*](TODO)_可选_
|
||||
|
||||
> *点运算符头* → **..**
|
||||
|
||||
> *点运算符字符* → **.** | [*运算符字符*](TODO)
|
||||
|
||||
> *点运算符字符集* → [*点运算符字符*](TODO) [*点运算符字符集*](TODO) _可选_
|
||||
|
||||
> *二元运算符* → [*运算符*](../chapter3/02_Lexical_Structure.html#operator)
|
||||
> *前置运算符* → [*运算符*](../chapter3/02_Lexical_Structure.html#operator)
|
||||
> *后置运算符* → [*运算符*](../chapter3/02_Lexical_Structure.html#operator)
|
||||
@ -865,22 +882,26 @@
|
||||
## 类型
|
||||
|
||||
> 类型语法
|
||||
>
|
||||
> *类型* → [*数组类型*](../chapter3/03_Types.html#array_type) | [*字典类型*](TODO) | [*函数类型*](../chapter3/03_Types.html#function_type) | [*类型标识符*](../chapter3/03_Types.html#type_identifier) | [*元组类型*](../chapter3/03_Types.html#tuple_type) | [*可选类型*](../chapter3/03_Types.html#optional_type) | [*隐式解析可选类型*](../chapter3/03_Types.html#implicitly_unwrapped_optional_type) | [*协议合成类型*](../chapter3/03_Types.html#protocol_composition_type) | [*元型类型*](../chapter3/03_Types.html#metatype_type)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 类型注解语法
|
||||
>
|
||||
> *类型注解* → **:** [*属性(Attributes)集*](../chapter3/06_Attributes.html#attributes) _可选_ [*类型*](../chapter3/03_Types.html#type)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 类型标识语法
|
||||
>
|
||||
> *类型标识* → [*类型名称*](../chapter3/03_Types.html#type_name) [*泛型参数从句*](GenericParametersAndArguments.html#generic_argument_clause) _可选_ | [*类型名称*](../chapter3/03_Types.html#type_name) [*泛型参数从句*](GenericParametersAndArguments.html#generic_argument_clause) _可选_ **.** [*类型标识符*](../chapter3/03_Types.html#type_identifier)
|
||||
> *类型名* → [*标识符*](../chapter3/02_Lexical_Structure.html#identifier)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 元组类型语法
|
||||
>
|
||||
> *元组类型* → **(** [*元组类型主体*](../chapter3/03_Types.html#tuple_type_body) _可选_ **)**
|
||||
> *元组类型主体* → [*元组类型的元素集*](../chapter3/03_Types.html#tuple_type_element_list) **...** _可选_
|
||||
> *元组类型的元素集* → [*元组类型的元素*](../chapter3/03_Types.html#tuple_type_element) | [*元组类型的元素*](../chapter3/03_Types.html#tuple_type_element) **,** [*元组类型的元素集*](../chapter3/03_Types.html#tuple_type_element_list)
|
||||
@ -890,31 +911,37 @@
|
||||
<!-- -->
|
||||
|
||||
> 函数类型语法
|
||||
>
|
||||
> *函数类型* → [*类型*](../chapter3/03_Types.html#type) **throws** _可选_ **->** [*类型*](../chapter3/03_Types.html#type)
|
||||
> *函数类型* → [*类型*](TODO) **rethrows** **->** [*类型*](TODO)
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 数组类型语法
|
||||
>
|
||||
> *数组类型* → **[** [*类型*](../chapter3/03_Types.html#array_type) **]**
|
||||
|
||||
<!-- -->
|
||||
> 字典类型语法
|
||||
>
|
||||
> *字典类型* → **[** [*类型 **:** 类型*](TODO) **]**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 可选类型语法
|
||||
>
|
||||
> *可选类型* → [*类型*](../chapter3/03_Types.html#type) **?**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 隐式解析可选类型(Implicitly Unwrapped Optional Type)语法
|
||||
>
|
||||
> *隐式解析可选类型* → [*类型*](../chapter3/03_Types.html#type) **!**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 协议合成类型语法
|
||||
>
|
||||
> *协议合成类型* → **protocol** **<** [*协议标识符集*](../chapter3/03_Types.html#protocol_identifier_list) _可选_ **>**
|
||||
> *协议标识符集* → [*协议标识符*](../chapter3/03_Types.html#protocol_identifier) | [*协议标识符*](../chapter3/03_Types.html#protocol_identifier) **,** [*协议标识符集*](../chapter3/03_Types.html#protocol_identifier_list)
|
||||
> *协议标识符* → [*类型标识符*](../chapter3/03_Types.html#type_identifier)
|
||||
@ -922,18 +949,15 @@
|
||||
<!-- -->
|
||||
|
||||
> 元(Metatype)类型语法
|
||||
>
|
||||
> *元类型* → [*类型*](../chapter3/03_Types.html#type) **.** **Type** | [*类型*](../chapter3/03_Types.html#type) **.** **Protocol**
|
||||
|
||||
<!-- -->
|
||||
|
||||
> 类型继承从句语法
|
||||
|
||||
>
|
||||
> *类型继承从句* → **:** [*类条件(class-requirement))*](TODO) **,** [*类型继承集*](../chapter3/03_Types.html#type_inheritance_list)
|
||||
|
||||
> *类型继承从句* → **:** [*类条件(class-requirement))*](TODO)
|
||||
|
||||
> *类型继承从句* → **:** [*类型继承集*](TODO)
|
||||
|
||||
> *类型继承集* → [*类型标识符*](../chapter3/03_Types.html#type_identifier) | [*类型标识符*](../chapter3/03_Types.html#type_identifier) **,** [*类型继承集*](../chapter3/03_Types.html#type_inheritance_list)
|
||||
|
||||
> *类条件* → **class**
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
# Swift 教程
|
||||
|
||||
本章描述了 Swift 的语言参考。
|
||||
Reference in New Issue
Block a user