replace all _ with -
This commit is contained in:
@ -4,7 +4,7 @@
|
||||
|
||||
Swift 语言相对较小,这是由于 Swift 代码中常用的类型、函数以及运算符都已经在 Swift 标准库中定义了。虽然这些类型、函数和运算符并不是 Swift 语言自身的一部分,但是它们被广泛应用于本书的讨论和代码范例中。
|
||||
|
||||
## 如何阅读语法 {#how_to_read_the_grammar}
|
||||
## 如何阅读语法 {#how-to-read-the-grammar}
|
||||
|
||||
用来描述 Swift 编程语言形式语法的符号遵循下面几个约定:
|
||||
|
||||
|
||||
@ -54,7 +54,7 @@ Swift 语言存在两种类型:命名型类型和复合型类型。*命名型
|
||||
> *类型* → [*(类型)*](#type)
|
||||
>
|
||||
|
||||
## 类型注解 {#type_annotation}
|
||||
## 类型注解 {#type-annotation}
|
||||
*类型注解*显式地指定一个变量或表达式的类型。类型注解始于冒号 `:` 终于类型,比如下面两个例子:
|
||||
|
||||
```swift
|
||||
@ -73,7 +73,7 @@ func someFunction(a: Int) { /* ... */ }
|
||||
> *类型注解* → **:** [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type)
|
||||
>
|
||||
|
||||
## 类型标识符 {#type_identifier}
|
||||
## 类型标识符 {#type-identifier}
|
||||
类型标识符引用命名型类型,还可引用命名型或复合型类型的别名。
|
||||
|
||||
大多数情况下,类型标识符引用的是与之同名的命名型类型。例如类型标识符 `Int` 引用命名型类型 `Int`,同样,类型标识符 `Dictionary<String, Int>` 引用命名型类型 `Dictionary<String, Int>`。
|
||||
@ -102,7 +102,7 @@ var someValue: ExampleModule.MyType
|
||||
> *类型名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 元组类型 {#tuple_type}
|
||||
## 元组类型 {#tuple-type}
|
||||
元组类型是使用括号括起来的零个或多个类型,类型间用逗号隔开。
|
||||
|
||||
你可以使用元组类型作为一个函数的返回类型,这样就可以使函数返回多个值。你也可以命名元组类型中的元素,然后用这些名字来引用每个元素的值。元素的名字由一个标识符紧跟一个冒号 `(:)` 组成。[函数和多返回值](../chapter2/06_Functions.md#functions_with_multiple_return_values) 章节里有一个展示上述特性的例子。
|
||||
@ -137,7 +137,7 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配
|
||||
> *元素名* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 函数类型 {#function_type}
|
||||
## 函数类型 {#function-type}
|
||||
函数类型表示一个函数、方法或闭包的类型,它由参数类型和返回值类型组成,中间用箭头(`->`)隔开:
|
||||
|
||||
> (`参数类型`)->(`返回值类型`)
|
||||
@ -234,7 +234,7 @@ func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) {
|
||||
> *参数标签* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 数组类型 {#array_type}
|
||||
## 数组类型 {#array-type}
|
||||
Swift 语言为标准库中定义的 `Array<Element>` 类型提供了如下语法糖:
|
||||
|
||||
> [`类型`]
|
||||
@ -266,7 +266,7 @@ var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
|
||||
> *数组类型* → **[** [*类型*](#type) **]**
|
||||
>
|
||||
|
||||
## 字典类型 {#dictionary_type}
|
||||
## 字典类型 {#dictionary-type}
|
||||
Swift 语言为标准库中定义的 `Dictionary<Key, Value>` 类型提供了如下语法糖:
|
||||
|
||||
> [`键类型` : `值类型`]
|
||||
@ -294,7 +294,7 @@ let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39]
|
||||
> *字典类型* → **[** [*类型*](#type) **:** [*类型*](#type) **]**
|
||||
>
|
||||
|
||||
## 可选类型 {#optional_type}
|
||||
## 可选类型 {#optional-type}
|
||||
Swift 定义后缀 `?` 来作为标准库中定义的命名型类型 `Optional<Wrapped>` 的语法糖。换句话说,下面两个声明是等价的:
|
||||
|
||||
```swift
|
||||
@ -326,7 +326,7 @@ optionalInteger! // 42
|
||||
> *可选类型* → [*类型*](#type) **?**
|
||||
>
|
||||
|
||||
## 隐式解析可选类型 {#implicitly_unwrapped_optional_type}
|
||||
## 隐式解析可选类型 {#implicitly-unwrapped-optional-type}
|
||||
当可以被访问时,Swift 语言定义后缀 `!` 作为标准库中命名类型 `Optional<Wrapped>` 的语法糖,来实现自动解包的功能。如果尝试对一个值为 `nil` 的可选类型进行隐式解包,将会产生运行时错误。因为隐式解包,下面两个声明等价:
|
||||
|
||||
```swift
|
||||
@ -361,7 +361,7 @@ let implicitlyUnwrappedArray: [Int]! // 正确
|
||||
> *隐式解析可选类型* → [*类型*](#type) **!**
|
||||
>
|
||||
|
||||
## 协议合成类型 {#protocol_composition_type}
|
||||
## 协议合成类型 {#protocol-composition-type}
|
||||
协议合成类型定义了一种遵循协议列表中每个指定协议的类型,或者一个现有类型的子类并遵循协议列表中每个指定协议。协议合成类型只能用在类型注解、泛型参数子句和泛型 `where` 子句中指定类型。
|
||||
|
||||
协议合成类型的形式如下:
|
||||
@ -395,7 +395,7 @@ typealias PQR = PQ & Q & R
|
||||
> *协议合成延续* → [*协议标识符*](#protocol-identifier) | [*协议合成类型*](#protocol-composition-type)
|
||||
>
|
||||
|
||||
## 元类型 {#metatype_type}
|
||||
## 元类型 {#metatype-type}
|
||||
元类型是指任意类型的类型,包括类类型、结构体类型、枚举类型和协议类型。
|
||||
|
||||
类、结构体或枚举类型的元类型是相应的类型名紧跟 `.Type`。协议类型的元类型——并不是运行时遵循该协议的具体类型——是该协议名字紧跟 `.Protocol`。比如,类 `SomeClass` 的元类型就是 `SomeClass.Type`,协议 `SomeProtocol` 的元类型就是 `SomeProtocal.Protocol`。
|
||||
@ -445,7 +445,7 @@ let anotherInstance = metatype.init(string: "some string")
|
||||
> *元类型* → [*类型*](#type) **.** **Type** | [*类型*](#type) **.** **Protocol**
|
||||
>
|
||||
|
||||
## 类型继承子句 {#type_inheritance_clause}
|
||||
## 类型继承子句 {#type-inheritance-clause}
|
||||
类型继承子句被用来指定一个命名型类型继承自哪个类、采纳哪些协议。类型继承子句开始于冒号 `:`,其后是类型标识符列表。
|
||||
|
||||
类可以继承自单个超类,并遵循任意数量的协议。当定义一个类时,超类的名字必须出现在类型标识符列表首位,然后跟上该类需要遵循的任意数量的协议。如果一个类不是从其它类继承而来,那么列表可以以协议开头。关于类继承更多的讨论和例子,请参阅 [继承](../chapter2/13_Inheritance.md)。
|
||||
@ -457,7 +457,7 @@ let anotherInstance = metatype.init(string: "some string")
|
||||
> 类型继承子句语法
|
||||
>
|
||||
|
||||
#### type_inheritance_clause {#type_inheritance_clause}
|
||||
#### type_inheritance_clause {#type-inheritance-clause}
|
||||
> *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list)
|
||||
>
|
||||
|
||||
@ -468,7 +468,7 @@ let anotherInstance = metatype.init(string: "some string")
|
||||
#### class-requirement {#class-requirement}
|
||||
|
||||
|
||||
## 类型推断 {#type_inference}
|
||||
## 类型推断 {#type-inference}
|
||||
Swift 广泛使用类型推断,从而允许你省略代码中很多变量和表达式的类型或部分类型。比如,对于 `var x: Int = 0`,你可以完全省略类型而简写成 `var x = 0`,编译器会正确推断出 `x` 的类型 `Int`。类似的,当完整的类型可以从上下文推断出来时,你也可以省略类型的一部分。比如,如果你写了 `let dict: Dictionary = ["A" : 1]`,编译器能推断出 `dict` 的类型是 `Dictionary<String, Int>`。
|
||||
|
||||
在上面的两个例子中,类型信息从表达式树的叶子节点传向根节点。也就是说,`var x: Int = 0` 中 `x` 的类型首先根据 `0` 的类型进行推断,然后将该类型信息传递到根节点(变量 `x`)。
|
||||
|
||||
@ -15,7 +15,7 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
> *表达式列表* → [*表达式*](#expression) | [*表达式*](#expression) **,** [*表达式列表*](#expression-list)
|
||||
>
|
||||
|
||||
## 前缀表达式 {#prefix_expressions}
|
||||
## 前缀表达式 {#prefix-expressions}
|
||||
前缀表达式由可选的前缀运算符和表达式组成。前缀运算符只接收一个参数,表达式则紧随其后。
|
||||
|
||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/26_Advanced_Operators.md)。
|
||||
@ -37,7 +37,7 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
> *输入输出表达式* → **&** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
### Try 运算符 {#try_operator}
|
||||
### Try 运算符 {#try-operator}
|
||||
try 表达式由 `try` 运算符加上紧随其后的可抛出错误的表达式组成,形式如下:
|
||||
|
||||
> try `可抛出错误的表达式`
|
||||
@ -75,7 +75,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
> *try 运算符* → **try** | **try?** | **try!**
|
||||
>
|
||||
|
||||
## 二元表达式 {#binary_expressions}
|
||||
## 二元表达式 {#binary-expressions}
|
||||
*二元表达式*由中缀运算符和左右参数表达式组成。形式如下:
|
||||
|
||||
> `左侧参数` `二元运算符` `右侧参数`
|
||||
@ -107,7 +107,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
> *二元表达式列表* → [*二元表达式*](#binary-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### 赋值表达式 {#assignment_operator}
|
||||
### 赋值表达式 {#assignment-operator}
|
||||
赋值表达式会为某个给定的表达式赋值,形式如下;
|
||||
|
||||
> `表达式` = `值`
|
||||
@ -129,7 +129,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
> *赋值运算符* → **=**
|
||||
>
|
||||
|
||||
### 三元条件运算符 {#ternary_conditional_operator}
|
||||
### 三元条件运算符 {#ternary-conditional-operator}
|
||||
*三元条件运算符*会根据条件来对两个给定表达式中的一个进行求值,形式如下:
|
||||
|
||||
> `条件` ? `表达式(条件为真则使用)` : `表达式(条件为假则使用)`
|
||||
@ -146,7 +146,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
> *三元条件运算符* → **?** [*表达式*](#expression) **:**
|
||||
>
|
||||
|
||||
### 类型转换运算符 {#type-casting_operators}
|
||||
### 类型转换运算符 {#type-casting-operators}
|
||||
有 4 种类型转换运算符:`is`、`as`、`as? ` 和 `as!`。它们有如下的形式:
|
||||
|
||||
> `表达式` is `类型`
|
||||
@ -198,7 +198,7 @@ f(x as Any)
|
||||
> *类型转换运算符* → **as** **!** [*类型*](./03_Types.md#type)
|
||||
>
|
||||
|
||||
## 基本表达式 {#primary_expressions}
|
||||
## 基本表达式 {#primary-expressions}
|
||||
*基本表达式*是最基本的表达式。它们可以单独使用,也可以跟前缀表达式、二元表达式、后缀表达式组合使用。
|
||||
|
||||
> 基本表达式语法
|
||||
@ -226,7 +226,7 @@ f(x as Any)
|
||||
> *基本表达式* → [*key-path字符串表达式*](#key-patch-string-expression)
|
||||
>
|
||||
|
||||
### 字面量表达式 {#literal_expression}
|
||||
### 字面量表达式 {#literal-expression}
|
||||
*字面量表达式*可由普通字面量(例如字符串或者数字),字典或者数组字面量,或者下面列表中的特殊字面量组成:
|
||||
|
||||
字面量 | 类型 | 值
|
||||
@ -330,7 +330,7 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者
|
||||
> *playground 字面量* → **#fileLiteral ( resourceName : [*表达式*](#expression) )**
|
||||
>
|
||||
>
|
||||
#### playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**self_expression {#self_expression}
|
||||
#### playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**self_expression {#self-expression}
|
||||
>
|
||||
|
||||
### Self 表达式
|
||||
@ -392,7 +392,7 @@ struct Point {
|
||||
> *self 构造器表达式* → **self** **.** **init**
|
||||
>
|
||||
|
||||
### 父类表达式 {#superclass_expression}
|
||||
### 父类表达式 {#superclass-expression}
|
||||
*父类*表达式可以使我们在某个类中访问它的父类,它有如下形式:
|
||||
|
||||
> super.`成员名称`
|
||||
@ -425,7 +425,7 @@ struct Point {
|
||||
> *父类构造器表达式* → **super** **.** **init**
|
||||
>
|
||||
|
||||
### 闭包表达式 {#closure_expression}
|
||||
### 闭包表达式 {#closure-expression}
|
||||
*闭包表达式*会创建一个闭包,在其他语言中也叫 *lambda* 或*匿名*函数。跟函数一样,闭包包含了待执行的代码,不同的是闭包还会捕获所在环境中的常量和变量。它的形式如下:
|
||||
|
||||
```swift
|
||||
@ -572,7 +572,7 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
|
||||
> *捕获说明符* → **weak** | **unowned** | **unowned(safe)** | **unowned(unsafe)**
|
||||
>
|
||||
|
||||
### 隐式成员表达式 {#implicit_member_expression}
|
||||
### 隐式成员表达式 {#implicit-member-expression}
|
||||
若类型可被推断出来,可以使用*隐式成员表达式*来访问某个类型的成员(例如某个枚举成员或某个类型方法),形式如下:
|
||||
|
||||
> .`成员名称`
|
||||
@ -592,7 +592,7 @@ x = .AnotherValue
|
||||
> *隐式成员表达式* → **.** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
### 圆括号表达式 {#parenthesized_expression}
|
||||
### 圆括号表达式 {#parenthesized-expression}
|
||||
*圆括号表达式*是由圆括号包围的表达式。你可以用圆括号说明成组的表达式的先后操作。成组的圆括号不会改变表达式的类型 - 例如 `(1)` 的类型就是简单的 `Int`。
|
||||
|
||||
> 圆括号表达式语法
|
||||
@ -602,7 +602,7 @@ x = .AnotherValue
|
||||
> *圆括号表达式* → **( [*表达式*](#expression) )**
|
||||
>
|
||||
|
||||
### 元组表达式 {#Tuple_Expression}
|
||||
### 元组表达式 {#Tuple-Expression}
|
||||
*元组表达式*由圆括号和其中多个逗号分隔的子表达式组成。每个子表达式前面可以有一个标识符,用冒号隔开。元组表达式形式如下:
|
||||
|
||||
> (`标识符 1` : `表达式 1`, `标识符 2` : `表达式 2`, `...`)
|
||||
@ -632,7 +632,7 @@ x = .AnotherValue
|
||||
> *元组元素* → [*表达式*](#expression) | [*标识符*](identifier) **:** [*表达式*](#expression)
|
||||
>
|
||||
|
||||
### 通配符表达式 {#wildcard_expression}
|
||||
### 通配符表达式 {#wildcard-expression}
|
||||
*通配符表达式*可以在赋值过程中显式忽略某个值。例如下面的代码中,`10` 被赋值给 `x`,而 `20` 则被忽略:
|
||||
|
||||
```swift
|
||||
@ -648,7 +648,7 @@ x = .AnotherValue
|
||||
>
|
||||
|
||||
|
||||
### Key-path 表达式 {#key-path_expression}
|
||||
### Key-path 表达式 {#key-path-expression}
|
||||
Key-path 表达式引用一个类型的属性或下标。在动态语言中使场景可以使用 Key-path 表达式,例如观察键值对。格式为:
|
||||
|
||||
> **\类型名.路径**
|
||||
@ -806,7 +806,7 @@ print(interestingNumbers[keyPath: \[String: [Int]].["hexagonal"]!.count.bitWidth
|
||||
|
||||
|
||||
|
||||
### 选择器表达式 {#selector_expression}
|
||||
### 选择器表达式 {#selector-expression}
|
||||
*选择器表达式*可以让你通过选择器来引用在 Objective-C 中方法(method)和属性(property)的 setter 和 getter 方法。
|
||||
|
||||
> \#selector(方法名)
|
||||
@ -863,7 +863,7 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
|
||||
> *选择器表达式* → __#selector__ **(** [*setter:表达式*](#expression) **)**
|
||||
>
|
||||
|
||||
## Key-path 字符串表达式 {#key-path_string_expressions}
|
||||
## Key-path 字符串表达式 {#key-path-string-expressions}
|
||||
key-path 字符串表达式可以访问一个引用 Objective-C 属性的字符串,通常在 key-value 编程和 key-value 观察 APIs 中使用。其格式如下:
|
||||
|
||||
> `#keyPath` ( `属性名` )
|
||||
@ -919,7 +919,7 @@ print(keyPath == c.getSomeKeyPath())
|
||||
> *key-path 字符串表达式* → **#keyPath (** [表达式](#expression) **)**
|
||||
>
|
||||
|
||||
## 后缀表达式 {#postfix_expressions}
|
||||
## 后缀表达式 {#postfix-expressions}
|
||||
*后缀表达式*就是在某个表达式的后面运用后缀运算符或其他后缀语法。从语法构成上来看,基本表达式也是后缀表达式。
|
||||
|
||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/26_Advanced_Operators.md)。
|
||||
@ -951,7 +951,7 @@ print(keyPath == c.getSomeKeyPath())
|
||||
> *后缀表达式* → [*可选链表达式*](#optional-chaining-expression)
|
||||
>
|
||||
|
||||
### 函数调用表达式 {#function_call_expression}
|
||||
### 函数调用表达式 {#function-call-expression}
|
||||
*函数调用表达式*由函数名和参数列表组成,形式如下:
|
||||
|
||||
> `函数名`(`参数 1`, `参数 2`)
|
||||
@ -1014,7 +1014,7 @@ myData.someMethod {$0 == 13}
|
||||
> *尾随闭包* → [*闭包表达式*](#closure-expression)
|
||||
>
|
||||
|
||||
### 构造器表达式 {#initializer_expression}
|
||||
### 构造器表达式 {#initializer-expression}
|
||||
*构造器表达式*用于访问某个类型的构造器,形式如下:
|
||||
|
||||
> `表达式`.init(`构造器参数`)
|
||||
@ -1061,7 +1061,7 @@ let s3 = someValue.dynamicType.init(data: 7) // 有效
|
||||
> *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** **(** [*参数名称*](#argument-names) **)**
|
||||
>
|
||||
|
||||
### 显式成员表达式 {#explicit_member_expression}
|
||||
### 显式成员表达式 {#explicit-member-expression}
|
||||
*显式成员表达式*允许我们访问命名类型、元组或者模块的成员,其形式如下:
|
||||
|
||||
> `表达式`.`成员名`
|
||||
@ -1138,7 +1138,7 @@ let x = [10, 3, 20, 15, 4]
|
||||
> *参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) **:**
|
||||
>
|
||||
|
||||
### 后缀 self 表达式 {#postfix_self_expression}
|
||||
### 后缀 self 表达式 {#postfix-self-expression}
|
||||
后缀 `self` 表达式由某个表达式或类型名紧跟 `.self` 组成,其形式如下:
|
||||
|
||||
> `表达式`.self
|
||||
@ -1158,7 +1158,7 @@ let x = [10, 3, 20, 15, 4]
|
||||
>
|
||||
|
||||
|
||||
### 下标表达式 {#subscript_expression}
|
||||
### 下标表达式 {#subscript-expression}
|
||||
可通过*下标表达式*访问相应的下标,形式如下:
|
||||
|
||||
> `表达式`[`索引表达式`]
|
||||
@ -1175,7 +1175,7 @@ let x = [10, 3, 20, 15, 4]
|
||||
> *下标表达式* → [*后缀表达式*](#postfix-expression) **[** [*表达式列表*](#expression-list) **]**
|
||||
>
|
||||
|
||||
### 强制取值表达式 {#forced-Value_expression}
|
||||
### 强制取值表达式 {#forced-Value-expression}
|
||||
当你确定可选值不是 `nil` 时,可以使用*强制取值表达式*来强制解包,形式如下:
|
||||
|
||||
> `表达式`!
|
||||
@ -1202,7 +1202,7 @@ someDictionary["a"]![0] = 100
|
||||
> *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!**
|
||||
>
|
||||
|
||||
### 可选链表达式 {#optional-chaining_expression}
|
||||
### 可选链表达式 {#optional-chaining-expression}
|
||||
*可选链表达式*提供了一种使用可选值的便捷方法,形式如下:
|
||||
|
||||
> `表达式`?
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
# 语句(Statements){#statement_statements}
|
||||
# 语句(Statements){#statement-statements}
|
||||
|
||||
在 Swift 中,有三种类型的语句:简单语句、编译器控制语句和控制流语句。简单语句是最常见的,用于构造表达式或者声明。编译器控制语句允许程序改变编译器的行为,包含编译配置语句和行控制语句。
|
||||
|
||||
@ -33,7 +33,7 @@
|
||||
> *多条语句* → [*语句*](#statement) [*多条语句*](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
## 循环语句 {#loop_statements}
|
||||
## 循环语句 {#loop-statements}
|
||||
循环语句会根据特定的循环条件来重复执行代码块。Swift 提供三种类型的循环语句:`for-in` 语句、`while` 语句和 `repeat-while` 语句。
|
||||
|
||||
通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。
|
||||
@ -50,7 +50,7 @@
|
||||
> *循环语句* → [*repeat-while 语句*](#repeat-while-statement)
|
||||
>
|
||||
|
||||
### For-In 语句 {#for-in_statements}
|
||||
### For-In 语句 {#for-in-statements}
|
||||
|
||||
`for-in` 语句会为集合(或实现了 [Sequence](https://developer.apple.com/documentation/swift/sequence) 协议的任意类型)中的每一项执行一次代码块。
|
||||
|
||||
@ -72,7 +72,7 @@ for item in collection {
|
||||
> *for-in 语句* → **for** **case**<sub>可选</sub> [*模式*](./08_Patterns.md#pattern) **in** [*表达式*](./04_Expressions.md#expression) [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
### While 语句 {#while_statements}
|
||||
### While 语句 {#while-statements}
|
||||
只要循环条件为真,`while` 语句就会重复执行代码块。
|
||||
|
||||
`while` 语句的形式如下:
|
||||
@ -118,7 +118,7 @@ while condition {
|
||||
> *可选绑定条件* → **let** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer) | **var** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer)
|
||||
>
|
||||
|
||||
### Repeat-While 语句 {#repeat-while_statements}
|
||||
### Repeat-While 语句 {#repeat-while-statements}
|
||||
`repeat-while` 语句至少执行一次代码块,之后只要循环条件为真,就会重复执行代码块。
|
||||
|
||||
`repeat-while` 语句的形式如下:
|
||||
@ -146,7 +146,7 @@ repeat {
|
||||
> *repeat-while 语句* → **repeat** [*代码块*](./06_Declarations.md#code-block) **while** [*表达式*](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
## 分支语句 {#branch_statements}
|
||||
## 分支语句 {#branch-statements}
|
||||
分支语句会根据一个或者多个条件来执行指定部分的代码。分支语句中的条件将会决定程序如何分支以及执行哪部分代码。Swift 提供三种类型的分支语句:`if` 语句、 `guard` 语句和 `switch` 语句。
|
||||
|
||||
`if` 语句和 `switch` 语句中的控制流可以用 `break` 语句改变,请参阅 [Break 语句](#break_statement)。
|
||||
@ -163,7 +163,7 @@ repeat {
|
||||
> *分支语句* → [*switch 语句*](#switch-statement)
|
||||
>
|
||||
|
||||
### If 语句 {#if_statements}
|
||||
### If 语句 {#if-statements}
|
||||
`if` 语句会根据一个或多个条件来决定执行哪一块代码。
|
||||
|
||||
`if` 语句有两种基本形式,无论哪种形式,都必须有花括号。
|
||||
@ -212,7 +212,7 @@ if condition 1 {
|
||||
> *else 子句* → **else** [*代码块*](./06_Declarations.md#code-block) | **else** [*if 语句*](#if-statement)
|
||||
>
|
||||
|
||||
### Guard 语句 {#guard_statements}
|
||||
### Guard 语句 {#guard-statements}
|
||||
如果一个或者多个条件不成立,可用 `guard` 语句来退出当前作用域。
|
||||
|
||||
`guard` 语句的格式如下:
|
||||
@ -244,7 +244,7 @@ guard condition else {
|
||||
> *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
### Switch 语句 {#switch_statements}
|
||||
### Switch 语句 {#switch-statements}
|
||||
`switch` 语句会根据控制表达式的值来决定执行哪部分代码。
|
||||
|
||||
`switch` 语句的形式如下:
|
||||
@ -348,27 +348,27 @@ case .suppressed:
|
||||
>
|
||||
>
|
||||
|
||||
#### grammar_conditional-switch-case {#grammar_conditional-switch-case}
|
||||
#### grammar_conditional-switch-case {#grammar-conditional-switch-case}
|
||||
> *conditional-switch-case* → [*switch-if-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label) <sub>可选</sub> [*switch-else-directive-clause*](#switch-case-attributes-label) <sub>可选</sub> [*endif-directive*](#switch-case-attributes-label)
|
||||
>
|
||||
|
||||
#### grammar_switch-if-directive-clause {#grammar_switch-if-directive-clause}
|
||||
#### grammar_switch-if-directive-clause {#grammar-switch-if-directive-clause}
|
||||
> *switch-if-directive 语句* → [*if-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) <sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_switch-elseif-directive-clauses {#grammar_switch-elseif-directive-clauses}
|
||||
#### grammar_switch-elseif-directive-clauses {#grammar-switch-elseif-directive-clauses}
|
||||
> *switch-elseif-directive 语句(复数)* → [*elseif-directive-clause*](#switch-case-attributes-label) [*switch-elseif-directive-clauses*](#switch-case-attributes-label)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_switch-elseif-directive-clause {#grammar_switch-elseif-directive-clause}
|
||||
#### grammar_switch-elseif-directive-clause {#grammar-switch-elseif-directive-clause}
|
||||
> *switch-elseif-directive 语句* → [*elseif-directive*](#switch-case-attributes-label) [*compilation-condition*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_switch-else-directive-clause {#grammar_switch-else-directive-clause}
|
||||
#### grammar_switch-else-directive-clause {#grammar-switch-else-directive-clause}
|
||||
> *switch-else-directive 语句* → [*else-directive*](#switch-case-attributes-label) [*switch-cases*](#switch-case-attributes-label) <sub>可选</sub>
|
||||
>
|
||||
|
||||
## 带标签的语句 {#labeled_statements}
|
||||
## 带标签的语句 {#labeled-statements}
|
||||
你可以在循环语句或 `switch` 语句前面加上标签,它由标签名和紧随其后的冒号(`:`)组成。在 `break` 和 `continue` 后面跟上标签名可以显式地在循环语句或 `switch` 语句中改变相应的控制流。关于这两条语句用法,请参阅 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。
|
||||
|
||||
标签的作用域在该标签所标记的语句内。可以嵌套使用带标签的语句,但标签名必须唯一。
|
||||
@ -397,7 +397,7 @@ case .suppressed:
|
||||
> *标签名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 控制转移语句 {#control_transfer_statements}
|
||||
## 控制转移语句 {#control-transfer-statements}
|
||||
控制转移语句能够无条件地把控制权从一片代码转移到另一片代码,从而改变代码执行的顺序。Swift 提供五种类型的控制转移语句:`break` 语句、`continue` 语句、`fallthrough` 语句、`return` 语句和 `throw` 语句。
|
||||
|
||||
> 控制转移语句语法
|
||||
@ -416,7 +416,7 @@ case .suppressed:
|
||||
> *控制转移语句* → [*throw 语句*](#throw-statement)
|
||||
>
|
||||
|
||||
### Break 语句 {#break_statement}
|
||||
### Break 语句 {#break-statement}
|
||||
`break` 语句用于终止循环语句、`if` 语句或 `switch` 语句的执行。使用 `break` 语句时,可以只写 `break` 这个关键词,也可以在 `break` 后面跟上标签名,像下面这样:
|
||||
|
||||
> break
|
||||
@ -440,7 +440,7 @@ case .suppressed:
|
||||
> *break 语句* → **break** [*标签名称*](#label-name)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### Continue 语句 {#continue_statement}
|
||||
### Continue 语句 {#continue-statement}
|
||||
`continue` 语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用 `continue` 语句时,可以只写 `continue` 这个关键词,也可以在 `continue` 后面跟上标签名,像下面这样:
|
||||
|
||||
> continue
|
||||
@ -466,7 +466,7 @@ case .suppressed:
|
||||
> *continue 语句* → **continue** [*标签名称*](#label-name)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### Fallthrough 语句 {#fallthrough_statements}
|
||||
### Fallthrough 语句 {#fallthrough-statements}
|
||||
`fallthrough` 语句用于在 `switch` 语句中转移控制权。`fallthrough` 语句会把控制权从 `switch` 语句中的一个 `case` 转移到下一个 `case`。这种控制权转移是无条件的,即使下一个 `case` 的模式与 `switch` 语句的控制表达式的值不匹配。
|
||||
|
||||
`fallthrough` 语句可出现在 `switch` 语句中的任意 `case` 中,但不能出现在最后一个 `case` 中。同时,`fallthrough` 语句也不能把控制权转移到使用了值绑定的 `case`。
|
||||
@ -481,7 +481,7 @@ case .suppressed:
|
||||
> *fallthrough 语句* → **fallthrough**
|
||||
>
|
||||
|
||||
### Return 语句 {#return_statements}
|
||||
### Return 语句 {#return-statements}
|
||||
`return` 语句用于在函数或方法的实现中将控制权转移到调用函数或方法,接着程序将会从调用位置继续向下执行。
|
||||
|
||||
使用 `return` 语句时,可以只写 `return` 这个关键词,也可以在 `return` 后面跟上表达式,像下面这样:
|
||||
@ -508,9 +508,9 @@ case .suppressed:
|
||||
#### return-statement {#return-statement}
|
||||
> *return 语句* → **return** [*表达式*](./04_Expressions.html#expression)<sub>可选</sub>
|
||||
|
||||
### Throw 语句 {#throw_statements}
|
||||
### Throw 语句 {#throw-statements}
|
||||
|
||||
### Throw 语句 {#throw_statements}
|
||||
### Throw 语句 {#throw-statements}
|
||||
`throw` 语句出现在抛出函数或者抛出方法体内,或者类型被 `throws` 关键字标记的闭包表达式体内。
|
||||
|
||||
`throw` 语句使程序在当前作用域结束执行,并向外围作用域传播错误。抛出的错误会一直传递,直到被 `do` 语句的 `catch` 子句处理掉。
|
||||
@ -532,7 +532,7 @@ case .suppressed:
|
||||
> *throw 语句* → **throw** [*表达式*](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
## Defer 语句 {#defer_statements}
|
||||
## Defer 语句 {#defer-statements}
|
||||
`defer` 语句用于在退出当前作用域之前执行代码。
|
||||
|
||||
`defer` 语句形式如下:
|
||||
@ -569,7 +569,7 @@ f()
|
||||
> *延迟语句* → **defer** [*代码块*](./06_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
## Do 语句 {#do_statements}
|
||||
## Do 语句 {#do-statements}
|
||||
`do` 语句用于引入一个新的作用域,该作用域中可以含有一个或多个 `catch` 子句,`catch` 子句中定义了一些匹配错误条件的模式。`do` 语句作用域内定义的常量和变量只能在 `do` 语句作用域内使用。
|
||||
|
||||
Swift 中的 `do` 语句与 C 中限定代码块界限的大括号(`{}`)很相似,也并不会降低程序运行时的性能。
|
||||
@ -609,7 +609,7 @@ do {
|
||||
> *catch 子句* → **catch** [*模式*](./08_Patterns.md#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
## 编译器控制语句 {#compiler_control_statements}
|
||||
## 编译器控制语句 {#compiler-control-statements}
|
||||
编译器控制语句允许程序改变编译器的行为。Swift 有三种编译器控制语句:条件编译语句、线路控制语句和编译时诊断语句。
|
||||
|
||||
> 编译器控制语句语法
|
||||
@ -624,7 +624,7 @@ do {
|
||||
> *编译器控制语句* → [*诊断语句*](#grammar_diagnostic-statement)
|
||||
>
|
||||
|
||||
### 条件编译代码块 {#Conditional_Compilation_Block}
|
||||
### 条件编译代码块 {#Conditional-Compilation-Block}
|
||||
条件编译代码块可以根据一个或多个配置来有条件地编译代码。
|
||||
|
||||
每一个条件编译代码块都以 `#if` 开始,`#endif` 结束。如下:
|
||||
@ -701,23 +701,23 @@ statements to compile if both compilation conditions are false
|
||||
>
|
||||
>
|
||||
|
||||
#### grammar_conditional-compilation-block {#grammar_conditional-compilation-block}
|
||||
#### grammar_conditional-compilation-block {#grammar-conditional-compilation-block}
|
||||
> *条件编译代码块* → [*if-directive 语句*](#grammar_if-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses)<sub>可选</sub> [*else-directive 语句*](#grammar_else-directive-clause)<sub>可选</sub> [*endif-directive*](#grammar_endif-directive)
|
||||
>
|
||||
|
||||
#### grammar_if-directive-clause {#grammar_if-directive-clause}
|
||||
#### grammar_if-directive-clause {#grammar-if-directive-clause}
|
||||
> *if-directive 语句* → [*if-directive*](#grammar_if-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_elseif-directive-clauses {#grammar_elseif-directive-clauses}
|
||||
#### grammar_elseif-directive-clauses {#grammar-elseif-directive-clauses}
|
||||
> *elseif-directive 语句(复数)* → [*elseif-directive 语句*](#grammar_elseif-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses)
|
||||
>
|
||||
|
||||
#### grammar_elseif-directive-clauses {#grammar_elseif-directive-clauses}
|
||||
#### grammar_elseif-directive-clauses {#grammar-elseif-directive-clauses}
|
||||
> *elseif-directive 语句* → [*elseif-directive*](#grammar_elseif-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_else-directive-clause {#grammar_else-directive-clause}
|
||||
#### grammar_else-directive-clause {#grammar-else-directive-clause}
|
||||
> *else-directive 语句* → [*else-directive*](#grammar_else-directive) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
@ -749,29 +749,29 @@ statements to compile if both compilation conditions are false
|
||||
>
|
||||
|
||||
|
||||
#### grammar_platform-condition {#grammar_platform-condition}
|
||||
#### grammar_platform-condition {#grammar-platform-condition}
|
||||
|
||||
#### grammar_platform-condition-os {#grammar_platform-condition-os}
|
||||
#### grammar_platform-condition-os {#grammar-platform-condition-os}
|
||||
> *平台条件* → **os ( [*操作系统*](#operating-system) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-arch {#grammar_platform-condition-arch}
|
||||
#### grammar_platform-condition-arch {#grammar-platform-condition-arch}
|
||||
> *平台条件* → **arch ( [*架构*](#architecture) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-swift {#grammar_platform-condition-swift}
|
||||
#### grammar_platform-condition-swift {#grammar-platform-condition-swift}
|
||||
> *平台条件* → **swift ( >= [*swift 版本*](#swift-version) )** | **swift ( < [*swift 版本*](#swift-version) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-compiler {#grammar_platform-condition-compiler}
|
||||
#### grammar_platform-condition-compiler {#grammar-platform-condition-compiler}
|
||||
> *平台条件* → **compiler ( >= [*swift 版本*](#swift-version) )** | **compiler ( < [*swift 版本*](#swift-version) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-canImport {#grammar_platform-condition-canImport}
|
||||
#### grammar_platform-condition-canImport {#grammar-platform-condition-canImport}
|
||||
> *平台条件* → **canImport ( [*模块名*](#grammar_module-name) )**
|
||||
>
|
||||
|
||||
#### grammar_platform-condition-targetEnvironment {#grammar_platform-condition-targetEnvironment}
|
||||
#### grammar_platform-condition-targetEnvironment {#grammar-platform-condition-targetEnvironment}
|
||||
> *平台条件* → **targetEnvironment ( [*环境*](#grammar_environment) )**
|
||||
>
|
||||
|
||||
@ -787,19 +787,19 @@ statements to compile if both compilation conditions are false
|
||||
> *swift 版本* → [*十进制数字*](./02_Lexical_Structure.md#decimal-digit) **.** [*swift 版本延续*](#grammar_swift-version-continuation) <sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_swift-version-continuation {#grammar_swift-version-continuation}
|
||||
#### grammar_swift-version-continuation {#grammar-swift-version-continuation}
|
||||
> *swift 版本延续* → **.** [*十进制数字*](./02_Lexical_Structure.md#decimal-digit) [*swift 版本延续*](#grammar_swift-version-continuation) <sub>可选</sub>
|
||||
>
|
||||
|
||||
#### grammar_module-name {#grammar_module-name}
|
||||
#### grammar_module-name {#grammar-module-name}
|
||||
> *模块名* → [*identifier*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
#### grammar_environment {#grammar_environment}
|
||||
#### grammar_environment {#grammar-environment}
|
||||
> *环境* → **模拟器**
|
||||
>
|
||||
|
||||
### 行控制语句 {#line_control_statements}
|
||||
### 行控制语句 {#line-control-statements}
|
||||
行控制语句可以为被编译的源代码指定行号和文件名,从而改变源代码的定位信息,以便进行分析和调试。
|
||||
|
||||
行控制语句形式如下:
|
||||
@ -847,17 +847,17 @@ statements to compile if both compilation conditions are false
|
||||
>
|
||||
>
|
||||
|
||||
#### grammar_compile-time-diagnostic-statement {#grammar_compile-time-diagnostic-statement}
|
||||
#### grammar_compile-time-diagnostic-statement {#grammar-compile-time-diagnostic-statement}
|
||||
> *诊断语句* → **#error** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)**
|
||||
>
|
||||
> *诊断语句* → **#warning** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)**
|
||||
>
|
||||
|
||||
#### grammar_diagnostic-message {#grammar_diagnostic-message}
|
||||
#### grammar_diagnostic-message {#grammar-diagnostic-message}
|
||||
> *诊断语句* → [*静态字符串字面量*](./02_Lexical_Structure.md#static-string-literal)
|
||||
>
|
||||
|
||||
## 可用性条件 {#availability_condition}
|
||||
## 可用性条件 {#availability-condition}
|
||||
可用性条件可作为 `if`,`while`,`guard` 语句的条件,可以在运行时基于特定的平台参数来查询 API 的可用性。
|
||||
|
||||
可用性条件的形式如下:
|
||||
|
||||
@ -42,7 +42,7 @@
|
||||
> *多条声明* → [*声明*](#declaration) [*多条声明*](#declarations)<sub>可选</sub>
|
||||
>
|
||||
|
||||
## 顶级代码 {#top-level_code}
|
||||
## 顶级代码 {#top-level-code}
|
||||
Swift 的源文件中的顶级代码(top-level code)由零个或多个语句、声明和表达式组成。默认情况下,在一个源文件的顶层声明的变量,常量和其他具有命名的声明可以被同模块中的每一个源文件中的代码访问。可以使用一个访问级别修饰符来标记声明来覆盖这种默认行为,请参阅 [访问控制级别](#access_control_levels)。
|
||||
|
||||
> 顶级声明语法
|
||||
@ -50,7 +50,7 @@ Swift 的源文件中的顶级代码(top-level code)由零个或多个语句
|
||||
> *顶级声明* → [*多条语句*](./05_Statements.md#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
## 代码块 {#code_blocks}
|
||||
## 代码块 {#code-blocks}
|
||||
*代码块(code block)* 可以将一些声明和控制结构体组织在一起。它有如下的形式:
|
||||
|
||||
```swift
|
||||
@ -69,7 +69,7 @@ Swift 的源文件中的顶级代码(top-level code)由零个或多个语句
|
||||
> *代码块* → **{** [*多条语句*](./05_Statements.md#statements)<sub>可选</sub> **}**
|
||||
>
|
||||
|
||||
## 导入声明 {#import_declaration}
|
||||
## 导入声明 {#import-declaration}
|
||||
*导入声明(import declaration)* 让你可以使用在其他文件中声明的内容。导入语句的基本形式是导入整个模块,它由 `import` 关键字和紧随其后的模块名组成:
|
||||
|
||||
```swift
|
||||
@ -84,7 +84,7 @@ import 模块.子模块
|
||||
```
|
||||
|
||||
|
||||
#### grammer_of_an_import_declaration {#grammer_of_an_import_declaration}
|
||||
#### grammer_of_an_import_declaration {#grammer-of-an-import-declaration}
|
||||
> 导入声明语法
|
||||
>
|
||||
>
|
||||
@ -108,7 +108,7 @@ import 模块.子模块
|
||||
> *导入路径标识符* → [*标识符*](./02_Lexical_Structure.md#identifier) | [*运算符*](./02_Lexical_Structure.md#operator)
|
||||
>
|
||||
|
||||
## 常量声明 {#constant_declaration}
|
||||
## 常量声明 {#constant-declaration}
|
||||
*常量声明(constant declaration)* 可以在程序中引入一个具有命名的常量。常量以关键字 `let` 来声明,遵循如下格式:
|
||||
|
||||
```swift
|
||||
@ -141,7 +141,7 @@ print("The second number is \(secondNumber).")
|
||||
如果还想获得更多关于常量的信息或者想在使用中获得帮助,请参阅 [常量和变量](../chapter2/01_The_Basics.md#constants_and_variables) 和 [存储属性](../chapter2/10_Properties.md#stored_properties)。
|
||||
|
||||
|
||||
#### grammer_of_a_constant_declaration {#grammer_of_a_constant_declaration}
|
||||
#### grammer_of_a_constant_declaration {#grammer-of-a-constant-declaration}
|
||||
> 常量声明语法
|
||||
>
|
||||
>
|
||||
@ -165,7 +165,7 @@ print("The second number is \(secondNumber).")
|
||||
> *构造器* → **=** [*表达式*](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
## 变量声明 {#variable_declaration}
|
||||
## 变量声明 {#variable-declaration}
|
||||
*变量声明(variable declaration)* 可以在程序中引入一个具有命名的变量,它以关键字 `var` 来声明。
|
||||
|
||||
变量声明有几种不同的形式,可以声明不同种类的命名值和可变值,如存储型和计算型变量和属性,属性观察器,以及静态变量属性。所使用的声明形式取决于变量声明的适用范围和打算声明的变量类型。
|
||||
@ -177,7 +177,7 @@ print("The second number is \(secondNumber).")
|
||||
|
||||
可以在子类中重写继承来的变量属性,使用 `override` 声明修饰符标记属性的声明即可,详情请参阅 [重写](../chapter2/13_Inheritance.md#overriding)。
|
||||
|
||||
### 存储型变量和存储型变量属性 {#stored_variables_and_stored_variable_properties}
|
||||
### 存储型变量和存储型变量属性 {#stored-variables-and-stored-variable-properties}
|
||||
使用如下形式声明一个存储型变量或存储型变量属性:
|
||||
|
||||
```swift
|
||||
@ -192,7 +192,7 @@ var 变量名称: 类型 = 表达式
|
||||
|
||||
正如名字所示,存储型变量和存储型变量属性的值会存储在内存中。
|
||||
|
||||
### 计算型变量和计算型属性 {#computed_variables_and_computed_properties}
|
||||
### 计算型变量和计算型属性 {#computed-variables-and-computed-properties}
|
||||
使用如下形式声明一个计算型变量或计算型属性:
|
||||
|
||||
```swift
|
||||
@ -216,7 +216,7 @@ setter 的圆括号以及 setter 名称是可选的。如果提供了 setter 名
|
||||
|
||||
要获得更多关于计算型属性的信息和例子,请参阅 [计算型属性](../chapter2/10_Properties.md#computed_properties)。
|
||||
|
||||
### 存储型变量和属性的观察器 {#stored_variable_observers_and_property_observers}
|
||||
### 存储型变量和属性的观察器 {#stored-variable-observers-and-property-observers}
|
||||
可以在声明存储型变量或属性时提供 `willSet` 和 `didSet` 观察器。一个包含观察器的存储型变量或属性以如下形式声明:
|
||||
|
||||
```swift
|
||||
@ -247,7 +247,7 @@ var 变量名称: 类型 = 表达式 {
|
||||
|
||||
要获得更多信息以及查看如何使用属性观察器的例子,请参阅 [属性观察器](../chapter2/10_Properties.md#property_observers)。
|
||||
|
||||
### 类型变量属性 {#type_variable_properties}
|
||||
### 类型变量属性 {#type-variable-properties}
|
||||
要声明一个类型变量属性,用 `static` 声明修饰符标记该声明。类可以改用 `class` 声明修饰符标记类的类型计算型属性从而允许子类重写超类的实现。类型属性在 [类型属性](../chapter2/10_Properties.md#type_properties) 章节有详细讨论。
|
||||
|
||||
> 注意
|
||||
@ -256,7 +256,7 @@ var 变量名称: 类型 = 表达式 {
|
||||
>
|
||||
|
||||
|
||||
#### grammer_of_a_variable_declaration {#grammer_of_a_variable_declaration}
|
||||
#### grammer_of_a_variable_declaration {#grammer-of-a-variable-declaration}
|
||||
> 变量声明语法
|
||||
>
|
||||
|
||||
@ -342,7 +342,7 @@ var 变量名称: 类型 = 表达式 {
|
||||
> *didSet 子句* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **didSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
|
||||
>
|
||||
|
||||
## 类型别名声明 {#type_alias_declaration}
|
||||
## 类型别名声明 {#type-alias-declaration}
|
||||
*类型别名(type alias)* 声明可以在程序中为一个既有类型声明一个别名。类型别名声明语句使用关键字 `typealias` 声明,遵循如下的形式:
|
||||
|
||||
```swift
|
||||
@ -398,7 +398,7 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
|
||||
另请参阅 [协议关联类型声明](#protocol_associated_type_declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_type_alias_declaration {#grammer_of_a_type_alias_declaration}
|
||||
#### grammer_of_a_type_alias_declaration {#grammer-of-a-type-alias-declaration}
|
||||
> 类型别名声明语法
|
||||
>
|
||||
>
|
||||
@ -418,7 +418,7 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
|
||||
> *类型别名赋值* → **=** [*类型*](./03_Types.md#type)
|
||||
>
|
||||
|
||||
## 函数声明 {#function_declaration}
|
||||
## 函数声明 {#function-declaration}
|
||||
使用*函数声明(function declaration)* 在程序中引入新的函数或者方法。在类、结构体、枚举,或者协议中声明的函数会作为方法。函数声明使用关键字 `func`,遵循如下的形式:
|
||||
|
||||
```swift
|
||||
@ -446,7 +446,7 @@ func 函数名称(参数列表) {
|
||||
|
||||
更多关于嵌套函数的讨论,请参阅 [嵌套函数](../chapter2/06_Functions.md#Nested_Functions)。
|
||||
|
||||
### 参数名 {#parameter_names}
|
||||
### 参数名 {#parameter-names}
|
||||
函数的参数列表由一个或多个函数参数组成,参数间以逗号分隔。函数调用时的参数顺序必须和函数声明时的参数顺序一致。最简单的参数列表有着如下的形式:
|
||||
|
||||
`参数名称`: `参数类型`
|
||||
@ -473,7 +473,7 @@ func repeatGreeting(_ greeting: String, count n: Int) { /* Greet n times */ }
|
||||
repeatGreeting("Hello, world!", count: 2) // count 有标签, greeting 没有
|
||||
```
|
||||
|
||||
### 输入输出参数 {#in-out_parameters}
|
||||
### 输入输出参数 {#in-out-parameters}
|
||||
输入输出参数被传递时遵循如下规则:
|
||||
|
||||
1. 函数调用时,参数的值被拷贝。
|
||||
@ -517,7 +517,7 @@ func multithreadedFunction(queue: DispatchQueue, x: inout Int) {
|
||||
|
||||
关于输入输出参数的详细讨论,请参阅 [输入输出参数](../chapter2/06_Functions.md#in_out_parameters)。
|
||||
|
||||
### 特殊参数 {#special_kinds_of_parameters}
|
||||
### 特殊参数 {#special-kinds-of-parameters}
|
||||
参数可以被忽略,数量可以不固定,还可以为其提供默认值,使用形式如下:
|
||||
|
||||
```swift
|
||||
@ -540,14 +540,14 @@ f(7) // 有效,提供了值
|
||||
f(x: 7) // 无效,该参数没有外部名称
|
||||
```
|
||||
|
||||
### 特殊方法 {#special_kinds_of_methods}
|
||||
### 特殊方法 {#special-kinds-of-methods}
|
||||
枚举或结构体的方法如果会修改 `self`,则必须以 `mutating` 声明修饰符标记。
|
||||
|
||||
子类重写超类中的方法必须以 `override` 声明修饰符标记。重写方法时不使用 `override` 修饰符,或者被 `override` 修饰符修饰的方法并未对超类方法构成重写,都会导致编译错误。
|
||||
|
||||
枚举或者结构体中的类型方法,要以 `static` 声明修饰符标记,而对于类中的类型方法,除了使用 `static`,还可使用 `class` 声明修饰符标记。类中使用 `class` 声明修饰的方法可以被子类实现重写;类中使用 `static` 声明修饰的方法不可被重写。
|
||||
|
||||
### 抛出错误的函数和方法 {#throwing_functions_and_methods}
|
||||
### 抛出错误的函数和方法 {#throwing-functions-and-methods}
|
||||
可以抛出错误的函数或方法必须使用 `throws` 关键字标记。这类函数和方法被称为抛出函数和抛出方法。它们有着下面的形式:
|
||||
|
||||
```swift
|
||||
@ -565,7 +565,7 @@ func 函数名称(参数列表) throws -> 返回类型 {
|
||||
|
||||
抛出方法不能重写非抛出方法,而且抛出方法不能满足协议对于非抛出方法的要求。也就是说,非抛出方法可以重写抛出方法,而且非抛出方法可以满足协议对于抛出方法的要求。
|
||||
|
||||
### 重抛错误的函数和方法 {#rethrowing_functions_and_methods}
|
||||
### 重抛错误的函数和方法 {#rethrowing-functions-and-methods}
|
||||
函数或方法可以使用 `rethrows` 关键字来声明,从而表明仅当该函数或方法的一个函数类型的参数抛出错误时,该函数或方法才抛出错误。这类函数和方法被称为重抛函数和重抛方法。重新抛出错误的函数或方法必须至少有一个参数的类型为抛出函数。
|
||||
|
||||
```swift
|
||||
@ -594,7 +594,7 @@ func someFunction(callback: () throws -> Void) rethrows {
|
||||
|
||||
抛出方法不能重写重抛方法,而且抛出方法不能满足协议对于重抛方法的要求。也就是说,重抛方法可以重写抛出方法,而且重抛方法可以满足协议对于抛出方法的要求。
|
||||
|
||||
### 永不返回的函数 {#functions_that_never_return}
|
||||
### 永不返回的函数 {#functions-that-never-return}
|
||||
Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它的调用者。`Never` 返回类型的函数或方法可以称为不归,不归函数、方法要么引发不可恢复的错误,要么永远不停地运作,这会使调用后本应执行得代码就不再执行了。但即使是不归函数、方法,抛错函数和重抛出函数也可以将程序控制转移到合适的 `catch` 代码块。
|
||||
|
||||
不归函数、方法可以在 guard 语句的 else 字句中调用,具体讨论在[*Guard 语句*](./05_Statements.md#guard_statements)。
|
||||
@ -602,7 +602,7 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
你可以重写一个不归方法,但是新的方法必须保持原有的返回类型和没有返回的行为。
|
||||
|
||||
|
||||
#### grammer_of_a_function_declaration {#grammer_of_a_function_declaration}
|
||||
#### grammer_of_a_function_declaration {#grammer-of-a-function-declaration}
|
||||
> 函数声明语法
|
||||
>
|
||||
|
||||
@ -674,7 +674,7 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
> *默认参数子句* → **=** [*表达式*](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
## 枚举声明 {#enumeration_declaration}
|
||||
## 枚举声明 {#enumeration-declaration}
|
||||
在程序中使用*枚举声明(enumeration declaration)* 来引入一个枚举类型。
|
||||
|
||||
枚举声明有两种基本形式,使用关键字 `enum` 来声明。枚举声明体包含零个或多个值,称为枚举用例,还可包含任意数量的声明,包括计算型属性、实例方法、类型方法、构造器、类型别名,甚至其他枚举、结构体和类。枚举声明不能包含析构器或者协议声明。
|
||||
@ -687,7 +687,7 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
|
||||
|
||||
可以扩展枚举类型,正如在 [扩展声明](#extension_declaration) 中讨论的一样。
|
||||
|
||||
### 任意类型的枚举用例 {#enumerations_with_cases_of_any_type}
|
||||
### 任意类型的枚举用例 {#enumerations-with-cases-of-any-type}
|
||||
如下的形式声明了一个包含任意类型枚举用例的枚举变量:
|
||||
|
||||
```swift
|
||||
@ -719,7 +719,7 @@ let evenInts: [Number] = [0, 2, 4, 6].map(f)
|
||||
|
||||
要获得更多关于具有关联值的枚举用例的信息和例子,请参阅 [关联值](../chapter2/08_Enumerations.md#associated_values)。
|
||||
|
||||
#### 递归枚举 {#enumerations_with_indirection}
|
||||
#### 递归枚举 {#enumerations-with-indirection}
|
||||
枚举类型可以具有递归结构,就是说,枚举用例的关联值类型可以是枚举类型自身。然而,枚举类型的实例具有值语义,这意味着它们在内存中有固定布局。为了支持递归,编译器必须插入一个间接层。
|
||||
|
||||
要让某个枚举用例支持递归,使用 `indirect` 声明修饰符标记该用例。
|
||||
@ -736,7 +736,7 @@ enum Tree<T> {
|
||||
|
||||
被 `indirect` 修饰符标记的枚举用例必须有一个关联值。使用 `indirect` 修饰符标记的枚举类型可以既包含有关联值的用例,同时还可包含没有关联值的用例。但是,它不能再单独使用 `indirect` 修饰符来标记某个用例。
|
||||
|
||||
### 拥有原始值的枚举用例 {#enumerations_with_cases_of_a_raw-value_type}
|
||||
### 拥有原始值的枚举用例 {#enumerations-with-cases-of-a-raw-value-type}
|
||||
以下形式声明了一种枚举类型,其中各个枚举用例的类型均为同一种基本类型:
|
||||
|
||||
```swift
|
||||
@ -770,13 +770,13 @@ enum GamePlayMode: String {
|
||||
|
||||
枚举用例具有原始值的枚举类型隐式地符合定义在 Swift 标准库中的 `RawRepresentable` 协议。所以,它们拥有一个 `rawValue` 属性和一个可失败构造器 `init?(rawValue: RawValue)`。可以使用 `rawValue` 属性去获取枚举用例的原始值,例如 `ExampleEnum.b.rawValue`。还可以根据原始值来创建一个相对应的枚举用例,只需调用枚举的可失败构造器,例如 `ExampleEnum(rawValue: 5)`,这个可失败构造器返回一个可选类型的用例。要获得更多关于具有原始值的枚举用例的信息和例子,请参阅 [原始值](../chapter2/08_Enumerations.md#raw_values)。
|
||||
|
||||
### 访问枚举用例 {#accessing_enumeration_cases}
|
||||
### 访问枚举用例 {#accessing-enumeration-cases}
|
||||
使用点语法(`.`)来引用枚举类型的枚举用例,例如 `EnumerationType.enumerationCase`。当枚举类型可以由上下文推断而出时,可以省略它(但是 `.` 仍然需要),正如 [枚举语法](../chapter2/08_Enumerations.md#enumeration_syntax) 和 [显式成员表达式](./04_Expressions.md#explicit_member_expression) 所述。
|
||||
|
||||
可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../chapter2/08_Enumerations.md#matching_enumeration_values_with_a_switch_statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](./08_Patterns.md#enumeration_case_pattern) 所述。
|
||||
|
||||
|
||||
#### grammer_of_an_enumeration_declaration {#grammer_of_an_enumeration_declaration}
|
||||
#### grammer_of_an_enumeration_declaration {#grammer-of-an-enumeration-declaration}
|
||||
> 枚举声明语法
|
||||
>
|
||||
>
|
||||
@ -867,7 +867,7 @@ enum GamePlayMode: String {
|
||||
> *原始值字面量* → [数字型字面量](./02_Lexical_Structure.md#numeric-literal) | [字符串型字面量](./02_Lexical_Structure.md#static-string-literal) | [布尔型字面量](./02_Lexical_Structure.md#boolean-literal)
|
||||
>
|
||||
|
||||
## 结构体声明 {#structure_declaration}
|
||||
## 结构体声明 {#structure-declaration}
|
||||
使用*结构体声明(structure declaration)* 可以在程序中引入一个结构体类型。结构体声明使用 `struct` 关键字,遵循如下的形式:
|
||||
|
||||
```swift
|
||||
@ -898,7 +898,7 @@ struct 结构体名称: 采纳的协议 {
|
||||
可以使用扩展声明来扩展结构体类型的行为,请参阅 [扩展声明](#extension_declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_structure_declaration {#grammer_of_a_structure_declaration}
|
||||
#### grammer_of_a_structure_declaration {#grammer-of-a-structure-declaration}
|
||||
> 结构体声明语法
|
||||
>
|
||||
>
|
||||
@ -929,7 +929,7 @@ struct 结构体名称: 采纳的协议 {
|
||||
> *结构体成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||
>
|
||||
|
||||
## 类声明 {#class_declaration}
|
||||
## 类声明 {#class-declaration}
|
||||
可以在程序中使用*类声明(class declaration)* 来引入一个类。类声明使用关键字 `class`,遵循如下的形式:
|
||||
|
||||
```swift
|
||||
@ -963,7 +963,7 @@ class 类名: 超类, 采纳的协议 {
|
||||
可以使用扩展声明来扩展类的行为,请参阅 [扩展声明](#extension_declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_class_declaration {#grammer_of_a_class_declaration}
|
||||
#### grammer_of_a_class_declaration {#grammer-of-a-class-declaration}
|
||||
> 类声明语法
|
||||
>
|
||||
>
|
||||
@ -993,7 +993,7 @@ class 类名: 超类, 采纳的协议 {
|
||||
> *类成员* → [*声明*](#declaration) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||
>
|
||||
|
||||
## 协议声明 {#protocol_declaration}
|
||||
## 协议声明 {#protocol-declaration}
|
||||
*协议声明(protocol declaration)* 可以为程序引入一个命名的协议类型。协议声明只能在全局区域使用 `protocol` 关键字来进行声明,并遵循如下形式:
|
||||
|
||||
```swift
|
||||
@ -1035,7 +1035,7 @@ protocol SomeProtocol: AnyObject {
|
||||
可以使用协议来声明作为代理的类或者结构体应该实现的方法,正如 [委托(代理)模式](../chapter2/21_Protocols.md#delegation) 中所述。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_declaration {#grammer_of_a_protocol_declaration}
|
||||
#### grammer_of_a_protocol_declaration {#grammer-of-a-protocol-declaration}
|
||||
> 协议声明语法
|
||||
>
|
||||
>
|
||||
@ -1082,7 +1082,7 @@ protocol SomeProtocol: AnyObject {
|
||||
> *协议成员声明列表* → [*协议成员声明*](#protocol-member-declaration) [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### 协议属性声明 {#protocol_property_declaration}
|
||||
### 协议属性声明 {#protocol-property-declaration}
|
||||
协议可以通过在协议声明主体中引入一个协议属性声明,来声明符合的类型必须实现的属性。协议属性声明有一种特殊的变量声明形式:
|
||||
|
||||
```swift
|
||||
@ -1096,7 +1096,7 @@ var 属性名: 类型 { get set }
|
||||
另请参阅 [变量声明](#variable_declaration)。
|
||||
|
||||
|
||||
#### grammer_of_an_import_declaration {#grammer_of_an_import_declaration}
|
||||
#### grammer_of_an_import_declaration {#grammer-of-an-import-declaration}
|
||||
> 协议属性声明语法
|
||||
>
|
||||
>
|
||||
@ -1105,7 +1105,7 @@ var 属性名: 类型 { get set }
|
||||
> *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.md#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||
>
|
||||
|
||||
### 协议方法声明 {#protocol_method_declaration}
|
||||
### 协议方法声明 {#protocol-method-declaration}
|
||||
协议可以通过在协议声明主体中引入一个协议方法声明,来声明符合的类型必须实现的方法。协议方法声明和函数方法声明有着相同的形式,但有两项例外:它们不包括函数体,也不能包含默认参数。关于如何实现协议中的方法要求的例子,请参阅 [方法要求](../chapter2/21_Protocols.md#method_requirements)。
|
||||
|
||||
使用 `static` 声明修饰符可以在协议声明中声明一个类型方法。类在实现这些方法时使用 `class` 声明修饰符。结构体实现这些方法时必须使用 `static` 声明修饰符。通过扩展实现时亦是如此(类的扩展中使用 `class` 声明修饰符,结构体的扩展中使用 `static` 声明修饰符)。
|
||||
@ -1113,7 +1113,7 @@ var 属性名: 类型 { get set }
|
||||
另请参阅 [函数声明](#function_declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_declaration {#grammer_of_a_protocol_declaration}
|
||||
#### grammer_of_a_protocol_declaration {#grammer-of-a-protocol-declaration}
|
||||
> 协议方法声明语法
|
||||
>
|
||||
>
|
||||
@ -1122,7 +1122,7 @@ var 属性名: 类型 { get set }
|
||||
> *协议方法声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### 协议构造器声明 {#protocol_initializer_declaration}
|
||||
### 协议构造器声明 {#protocol-initializer-declaration}
|
||||
协议可以通过在协议声明主体中引入一个协议构造器声明,来声明符合的类型必须实现的构造器。协议构造器声明
|
||||
除了不包含实现主体外,和构造器声明有着相同的形式。
|
||||
|
||||
@ -1133,7 +1133,7 @@ var 属性名: 类型 { get set }
|
||||
另请参阅 [构造器声明](#initializer_declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_initializer_declaration {#grammer_of_a_protocol_initializer_declaration}
|
||||
#### grammer_of_a_protocol_initializer_declaration {#grammer-of-a-protocol-initializer-declaration}
|
||||
> 协议构造器声明语法
|
||||
>
|
||||
>
|
||||
@ -1144,7 +1144,7 @@ var 属性名: 类型 { get set }
|
||||
> *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.md#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows** [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub>
|
||||
>
|
||||
|
||||
### 协议下标声明 {#protocol_subscript_declaration}
|
||||
### 协议下标声明 {#protocol-subscript-declaration}
|
||||
协议可以通过在协议声明主体中引入一个协议下标声明,来声明符合的类型必须实现的下标。协议下标声明有一个特殊的下标声明形式:
|
||||
|
||||
```swift
|
||||
@ -1157,7 +1157,7 @@ subscript (参数列表) -> 返回类型 { get set }
|
||||
另请参阅 [下标声明](#subscript_declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_subscript_declaration {#grammer_of_a_protocol_subscript_declaration}
|
||||
#### grammer_of_a_protocol_subscript_declaration {#grammer-of-a-protocol-subscript-declaration}
|
||||
> 协议下标声明语法
|
||||
>
|
||||
>
|
||||
@ -1166,7 +1166,7 @@ subscript (参数列表) -> 返回类型 { get set }
|
||||
> *协议下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub> [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
|
||||
>
|
||||
|
||||
### 协议关联类型声明 {#protocol_associated_type_declaration}
|
||||
### 协议关联类型声明 {#protocol-associated-type-declaration}
|
||||
使用关键字 `associatedtype` 来声明协议关联类型。关联类型为作为协议声明的一部分,为某种类型提供了一个别名。关联类型和泛型参数子句中的类型参数很相似,但是它们和 `Self` 一样,用于协议中。`Self` 指代采纳协议的类型。要获得更多信息和例子,请参阅 [关联类型](../chapter2/22_Generics.md#associated_types)。
|
||||
|
||||
在协议声明中使用泛型 `where` 子句来为继承的协议关联类型添加约束,且不需要重新声明关联类型。例如下面代码中的 `SubProtocol` 声明。
|
||||
@ -1190,7 +1190,7 @@ protocol SubProtocolB: SomeProtocol where SomeType: Equatable { }
|
||||
另请参阅 [类型别名声明](#type_alias_declaration)。
|
||||
|
||||
|
||||
#### grammer_of_a_protocol_associated_type_declaration {#grammer_of_a_protocol_associated_type_declaration}
|
||||
#### grammer_of_a_protocol_associated_type_declaration {#grammer-of-a-protocol-associated-type-declaration}
|
||||
> 协议关联类型声明语法
|
||||
>
|
||||
>
|
||||
@ -1199,7 +1199,7 @@ protocol SubProtocolB: SomeProtocol where SomeType: Equatable { }
|
||||
> *协议关联类型声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **associatedtype** [*类型别名头*](#typealias-head) [*类型继承子句*](03_Types.md#type-inheritance-clause)<sub>可选</sub> [*类型别名赋值*](#typealias-assignment)<sub>可选</sub> [*泛型 where 子句*](08_Generic_Parameters_and_Arguments.md#generic-where-clause)<sub>可选</sub>
|
||||
>
|
||||
|
||||
## 构造器声明 {#initializer_declaration}
|
||||
## 构造器声明 {#initializer-declaration}
|
||||
构造器声明会为程序中的类、结构体或枚举引入构造器。构造器使用关键字 `init` 来声明,有两种基本形式。
|
||||
|
||||
结构体、枚举、类可以有任意数量的构造器,但是类的构造器具有不同的规则和行为。不同于结构体和枚举,类有两种构造器,即指定构造器和便利构造器,请参阅 [构造过程](../chapter2/14_Initialization.md)。
|
||||
@ -1243,7 +1243,7 @@ convenience init(参数列表) {
|
||||
|
||||
关于在不同类型中声明构造器的例子,请参阅 [构造过程](../chapter2/14_Initialization.md)。
|
||||
|
||||
### 可失败构造器 {#failable_initializers}
|
||||
### 可失败构造器 {#failable-initializers}
|
||||
可失败构造器可以生成所属类型的可选实例或者隐式解包可选实例,因此,这种构造器通过返回 `nil` 来指明构造过程失败。
|
||||
|
||||
声明生成可选实例的可失败构造器时,在构造器声明的 `init` 关键字后加追加一个问号(`init?`)。声明生成隐式解包可选实例的可失败构造器时,在构造器声明后追加一个叹号(`init!`)。使用 `init?` 可失败构造器生成结构体的一个可选实例的例子如下。
|
||||
@ -1283,7 +1283,7 @@ if let actualInstance = SomeStruct(input: "Hello") {
|
||||
更多关于可失败构造器的信息和例子,请参阅 [可失败构造器](../chapter2/14_Initialization.md#failable_initializers)。
|
||||
|
||||
|
||||
#### grammer_of_an_initializer_declaration {#grammer_of_an_initializer_declaration}
|
||||
#### grammer_of_an_initializer_declaration {#grammer-of-an-initializer-declaration}
|
||||
> 构造器声明语法
|
||||
>
|
||||
>
|
||||
@ -1308,7 +1308,7 @@ if let actualInstance = SomeStruct(input: "Hello") {
|
||||
> *构造器主体* → [*代码块*](#code-block)
|
||||
>
|
||||
|
||||
## 析构器声明 {#deinitializer_declaration}
|
||||
## 析构器声明 {#deinitializer-declaration}
|
||||
*析构器声明(deinitializer declaration)* 可以为类声明一个析构器。析构器没有参数,遵循如下格式:
|
||||
|
||||
```swift
|
||||
@ -1326,7 +1326,7 @@ deinit {
|
||||
关于如何在类声明中使用析构器的例子,请参阅 [析构过程](../chapter2/15_Deinitialization.md)。
|
||||
|
||||
|
||||
#### grammer_of_a_deinitializer_declaration {#grammer_of_a_deinitializer_declaration}
|
||||
#### grammer_of_a_deinitializer_declaration {#grammer-of-a-deinitializer-declaration}
|
||||
> 析构器声明语法
|
||||
>
|
||||
>
|
||||
@ -1335,7 +1335,7 @@ deinit {
|
||||
> *析构器声明* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block)
|
||||
>
|
||||
|
||||
## 扩展声明 {#extension_declaration}
|
||||
## 扩展声明 {#extension-declaration}
|
||||
*扩展声明(extension declaration)* 可以扩展一个现存的类型的行为。扩展声明使用关键字 `extension`,遵循如下格式:
|
||||
|
||||
```swift
|
||||
@ -1364,10 +1364,10 @@ extension 类型名称: 采纳的协议 where 约束条件 {
|
||||
|
||||
协议声明不能为现有的类添加类的继承关系,因此你只能在 “类型名称” 的冒号后面添加一系列协议。
|
||||
|
||||
### 条件遵循 {#conditional_conformance}
|
||||
### 条件遵循 {#conditional-conformance}
|
||||
你可以扩展一个泛型类型并使其有条件地遵循某协议,此后此类型的实例只有在特定的限制条件满足时才遵循此协议。在扩展声明中加入限制条件来为协议添加条件遵循。
|
||||
|
||||
## 已重写的限制条件会在某些泛型上下文中失效 {#overridden_requirements_aren't_Used_in_some_generic_contexts}
|
||||
## 已重写的限制条件会在某些泛型上下文中失效 {#overridden-requirements-aren't-Used-in-some-generic-contexts}
|
||||
对于一些通过条件遵循获得了特定行为的类型,在某些泛型上下文中,并不能够确保能够使用协议限制中的特定实现。为了说明这个行为,下面的例子中定义了两个协议以及一个有条件地遵循两个协议的泛型类型。
|
||||
|
||||
```swift
|
||||
@ -1431,10 +1431,10 @@ doSomething(with: oneAndTwo)
|
||||
|
||||
当传入 `doSomething(_:)` 的实例调用 `log()` 时,打印结果省略了自定义标题。
|
||||
|
||||
### 协议遵循决不能冗余 {#protocol_conformance_must_not_be_redundant}
|
||||
### 协议遵循决不能冗余 {#protocol-conformance-must-not-be-redundant}
|
||||
一个具体的类型只能够遵循某特定协议一次。Swift 会把冗余的协议遵循标记为错误。你会在两种场景中遇到这种错误。第一种场景是,使用不同的限制条件来多次显式地遵循同一协议。第二种场景是,多次隐式地继承同一协议。以上两种场景会在下面章节中讨论。
|
||||
|
||||
## 解决显式冗余 {#resolving_explicit_redundancy}
|
||||
## 解决显式冗余 {#resolving-explicit-redundancy}
|
||||
对同一具体类型的多个扩展不能遵循同一协议,即便这些扩展有不同的显式限制条件。这个限制的具体示例在下面的例子中。两个扩展声明都试图添加对 `Serializable` 的条件遵循,一个为 `Int` 类型元素的数组,另一个为 `String` 类型元素的数组。
|
||||
|
||||
```swift
|
||||
@ -1473,7 +1473,7 @@ extension Array: Serializable where Element: SerializableInArray {
|
||||
}
|
||||
```
|
||||
|
||||
## 解决隐式冗余 {#resolving_implicit_redundancy}
|
||||
## 解决隐式冗余 {#resolving-implicit-redundancy}
|
||||
当一个具体类型有条件地遵循某协议,此类型会隐式地使用相同的条件遵循任一父协议。
|
||||
|
||||
如果你需要让一个类型有条件地遵循两个继承自同一父协议的协议,请显式地声明对父协议的遵循。这可以避免使用不同的限制条件隐式遵循同一父协议两次。
|
||||
@ -1511,7 +1511,7 @@ extension Array: Loggable where Element: MarkedLoggable { }
|
||||
```
|
||||
|
||||
|
||||
#### grammer_of_an_extension_declaration {#grammer_of_an_extension_declaration}
|
||||
#### grammer_of_an_extension_declaration {#grammer-of-an-extension-declaration}
|
||||
> 扩展声明语法
|
||||
>
|
||||
>
|
||||
@ -1530,7 +1530,7 @@ extension Array: Loggable where Element: MarkedLoggable { }
|
||||
> *单条声明* → [声明语句](#declarations) | [*编译控制流语句*](05_Statements.md#compiler-control-statement)
|
||||
>
|
||||
|
||||
## 下标声明 {#subscript_declaration}
|
||||
## 下标声明 {#subscript-declaration}
|
||||
*下标声明(subscript declaration)* 用于为特定类型的对象添加下标支持,通常也用于为访问集合、列表和序列中的元素提供语法便利。下标声明使用关键字 `subscript`,形式如下:
|
||||
|
||||
```swift
|
||||
@ -1562,7 +1562,7 @@ subscript (参数列表) -> 返回类型 {
|
||||
更多关于下标的信息和例子,请参阅 [下标](../chapter2/12_Subscripts.md)。
|
||||
|
||||
|
||||
#### grammer_of_a_subscript_declaration {#grammer_of_a_subscript_declaration}
|
||||
#### grammer_of_a_subscript_declaration {#grammer-of-a-subscript-declaration}
|
||||
> 下标声明语法
|
||||
>
|
||||
>
|
||||
@ -1586,7 +1586,7 @@ subscript (参数列表) -> 返回类型 {
|
||||
> *下标结果* → **->** [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*类型*](./03_Types.md#type)
|
||||
>
|
||||
|
||||
## 运算符声明 {#operator_declaration}
|
||||
## 运算符声明 {#operator-declaration}
|
||||
*运算符声明(operator declaration)* 会向程序中引入中缀、前缀或后缀运算符,使用关键字 `operator` 来声明。
|
||||
|
||||
可以声明三种不同的缀性:中缀、前缀和后缀。运算符的缀性指定了运算符与其运算对象的相对位置。
|
||||
@ -1626,7 +1626,7 @@ postfix operator 运算符名称 {}
|
||||
声明了一个新的运算符以后,需要实现一个跟这个运算符同名的函数来实现这个运算符。如果是实现一个前缀或者后缀运算符,也必须使用相符的 `prefix` 或者 `postfix` 声明修饰符标记函数声明。如果是实现中缀运算符,则不需要使用 `infix` 声明修饰符标记函数声明。关于如何实现一个新的运算符的例子,请参阅 [自定义运算符](../chapter2/26_Advanced_Operators.md#custom_operators)。
|
||||
|
||||
|
||||
#### grammer_of_an_operator_declaration {#grammer_of_an_operator_declaration}
|
||||
#### grammer_of_an_operator_declaration {#grammer-of-an-operator-declaration}
|
||||
> 运算符声明语法
|
||||
>
|
||||
|
||||
@ -1652,7 +1652,7 @@ postfix operator 运算符名称 {}
|
||||
> *中缀运算符组* → [*优先级组名称*](#precedence-group-name)
|
||||
>
|
||||
|
||||
## 优先级组声明 {#precedence_group_declaration_modifiers}
|
||||
## 优先级组声明 {#precedence-group-declaration-modifiers}
|
||||
*优先级组声明(A precedence group declaration)* 会向程序的中缀运算符引入一个全新的优先级组。当没有用圆括号分组时,运算符优先级反应了运算符与它的操作数的关系的紧密程度。
|
||||
优先级组的声明如下所示:
|
||||
|
||||
@ -1679,7 +1679,7 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
优先级组的赋值性表示在包含可选链操作时的运算符优先级。当设为 true 时,与优先级组对应的运算符在可选链操作中使用和标准库中赋值运算符同样的分组规则,当设为 false 或者不设置,该优先级组的运算符与不赋值的运算符遵循同样的可选链规则。
|
||||
|
||||
|
||||
#### grammer_of_a_precedence_group_declaration {#grammer_of_a_precedence_group_declaration}
|
||||
#### grammer_of_a_precedence_group_declaration {#grammer-of-a-precedence-group-declaration}
|
||||
> 优先级组声明语法
|
||||
>
|
||||
|
||||
@ -1727,7 +1727,7 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
> *优先级组名称* →[*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 声明修饰符 {#Declaration_Modifiers}
|
||||
## 声明修饰符 {#Declaration-Modifiers}
|
||||
声明修饰符都是关键字或上下文相关的关键字,可以修改一个声明的行为或者含义。可以在声明的特性(如果存在)和引入该声明的关键字之间,利用声明修饰符的关键字或上下文相关的关键字指定一个声明修饰符。
|
||||
|
||||
`dynamic`
|
||||
@ -1770,7 +1770,7 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
|
||||
|
||||
该修饰符用于修饰变量或存储型变量属性,表示该变量或属性持有其存储的对象的弱引用。这种变量或属性的类型必须是可选的类类型。使用 `weak` 修饰符可避免强引用循环。关于 `weak` 修饰符的更多信息和例子,请参阅 [弱引用](../chapter2/23_Automatic_Reference_Counting.md#resolving_strong_reference_cycles_between_class_instances)。
|
||||
|
||||
### 访问控制级别 {#access_control_levels}
|
||||
### 访问控制级别 {#access-control-levels}
|
||||
Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`。可以使用以下任意一种访问级别修饰符来指定声明的访问级别。访问控制在 [访问控制](../chapter2/25_Access_Control.md) 中有详细讨论。
|
||||
|
||||
`public`
|
||||
@ -1788,7 +1788,7 @@ Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`
|
||||
以上访问级别修饰符都可以选择带上一个参数,该参数由一对圆括号和其中的 `set` 关键字组成(例如,`private(set)`)。使用这种形式的访问级别修饰符来限制某个属性或下标的 setter 的访问级别低于其本身的访问级别,正如 [Getter 和 Setter](../chapter2/25_Access_Control.md#getters_and_setters) 中所讨论的。
|
||||
|
||||
|
||||
#### grammer_of_a_declaration_modifier {#grammer_of_a_declaration_modifier}
|
||||
#### grammer_of_a_declaration_modifier {#grammer-of-a-declaration-modifier}
|
||||
> 声明修饰符的语法
|
||||
>
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
有些声明特性通过接收参数来指定特性的更多信息以及它是如何修饰某个特定的声明的。这些_特性的参数_写在圆括号内,它们的格式由它们所属的特性来定义。
|
||||
|
||||
## 声明特性 {#declaration_attributes}
|
||||
## 声明特性 {#declaration-attributes}
|
||||
|
||||
声明特性只能应用于声明。
|
||||
|
||||
@ -285,7 +285,7 @@ class ExampleClass: NSObject {
|
||||
|
||||
大多数代码应该使用 `objc` 特性,以暴露所需的声明。如果需要暴露多个声明,可以将其分组到添加 `objc` 特性的扩展中。`objcMembers` 特性为大量使用 Objective-C 运行时的内省工具的库提供了便利。添加不必要的 `objc` 特性会增加二进制体积并影响性能。
|
||||
|
||||
### `requires_stored_property_inits` {#requires_stored_property_inits}
|
||||
### `requires_stored_property_inits` {#requires-stored-property-inits}
|
||||
|
||||
该特性用于类声明,以要求类中所有存储属性提供默认值作为其定义的一部分。对于从中继承的任何类都推断出 `NSManagedObject` 特性。
|
||||
|
||||
@ -307,13 +307,13 @@ class ExampleClass: NSObject {
|
||||
|
||||
标记为 `inlinable` 特性的声明,在内联代码中可以隐式使用。虽然 `inlinable` 或 `usableFromInline` 可以用于 `internal` 声明,但这两者不能同时使用。
|
||||
|
||||
### `warn_unqualified_access` {#warn_unqualified_access}
|
||||
### `warn_unqualified_access` {#warn-unqualified-access}
|
||||
|
||||
该特性应用于顶级函数、实例方法、类方法或静态方法,以在没有前置限定符(例如模块名称、类型名称、实例变量或常量)的情况下使用该函数或方法时触发警告。使用该特性可以帮助减少在同一作用于访问同名函数之间的歧义。
|
||||
|
||||
例如,Swift 标准库包含 [`min(_:_:)`](https://developer.apple.com/documentation/swift/1538339-min/) 顶级函数和用于序列比较元素的 [`min()`](https://developer.apple.com/documentation/swift/sequence/1641174-min) 方法。序列方法声明使用了 `warn_unqualified_access`,以减少在 `Sequence` 扩展中使用它们的歧义。
|
||||
|
||||
### Interface Builder 使用的声明特性 {#declaration_attributes_used_by_interface_builder}
|
||||
### Interface Builder 使用的声明特性 {#declaration-attributes-used-by-interface-builder}
|
||||
|
||||
`Interface Builder` 特性是 `Interface Builder` 用来与 Xcode 同步的声明特性。`Swift` 提供了以下的 `Interface Builder` 特性:`IBAction`,`IBOutlet`,`IBDesignable`,以及 `IBInspectable` 。这些特性与 Objective-C 中对应的特性在概念上是相同的。
|
||||
|
||||
@ -321,7 +321,7 @@ class ExampleClass: NSObject {
|
||||
|
||||
应用 `IBAction`、`IBOutlet`、`IBDesignable` 或者 `IBInspectable` 特性都意味着同时应用 `objc` 特性。
|
||||
|
||||
## 类型特性 {#type_attributes}
|
||||
## 类型特性 {#type-attributes}
|
||||
|
||||
类型特性只能用于修饰类型。
|
||||
|
||||
@ -347,7 +347,7 @@ convention 特性总是与下面的参数之一一起出现。
|
||||
|
||||
在函数或者方法声明上使用该特性,它表示参数将不会被存储以供延迟执行,这将确保参数不会超出函数调用的生命周期。在使用 `escaping` 声明特性的函数类型中访问属性和方法时不需要显式地使用 `self.`。关于如何使用 `escaping` 特性的例子,请参阅 [逃逸闭包](../chapter2/07_Closures.md#escaping_closures)。
|
||||
|
||||
## Switch Case 特性 {#switch_case_attributes}
|
||||
## Switch Case 特性 {#switch-case-attributes}
|
||||
|
||||
你只能在 switch cases 中使用 switch case 特性。
|
||||
|
||||
@ -364,12 +364,12 @@ convention 特性总是与下面的参数之一一起出现。
|
||||
> *特性*→ [特性名](#attribute_name) [特性参数子句](#atribute_argument_clause)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### attribute_name {#attribute_name}
|
||||
#### attribute_name {#attribute-name}
|
||||
>
|
||||
> *特性名* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
>
|
||||
#### atribute_argument_clause {#atribute_argument_clause}
|
||||
#### atribute_argument_clause {#atribute-argument-clause}
|
||||
>
|
||||
> *特性参数子句* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)**
|
||||
>
|
||||
@ -380,12 +380,12 @@ convention 特性总是与下面的参数之一一起出现。
|
||||
>
|
||||
>
|
||||
>
|
||||
#### balanced_tokens {#balanced_tokens}
|
||||
#### balanced_tokens {#balanced-tokens}
|
||||
>
|
||||
> *均衡令牌列表* → [均衡令牌](#balanced_token) [均衡令牌列表](#balanced_tokens)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
#### balanced_token {#balanced_token}
|
||||
#### balanced_token {#balanced-token}
|
||||
>
|
||||
> *均衡令牌* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)**
|
||||
>
|
||||
|
||||
@ -29,7 +29,7 @@ Swift 中的模式分为两类:一种能成功匹配任何类型的值,另
|
||||
> *模式* → [*表达式模式*](#expression-pattern)
|
||||
>
|
||||
|
||||
## 通配符模式(Wildcard Pattern) {#wildcard_pattern}
|
||||
## 通配符模式(Wildcard Pattern) {#wildcard-pattern}
|
||||
|
||||
*通配符模式*由一个下划线(`_`)构成,用于匹配并忽略任何值。当你想忽略被匹配的值时可以使用该模式。例如,下面这段代码在闭区间 `1...3` 中迭代,每次迭代都忽略该区间的当前值:
|
||||
|
||||
@ -46,7 +46,7 @@ for _ in 1...3 {
|
||||
> *通配符模式* → **_**
|
||||
>
|
||||
|
||||
## 标识符模式(Identifier Pattern) {#identifier_pattern}
|
||||
## 标识符模式(Identifier Pattern) {#identifier-pattern}
|
||||
*标识符模式*匹配任何值,并将匹配的值和一个变量或常量绑定起来。例如,在下面的常量声明中,`someValue` 是一个标识符模式,匹配了 `Int` 类型的 `42`:
|
||||
|
||||
```swift
|
||||
@ -64,7 +64,7 @@ let someValue = 42
|
||||
> *标识符模式* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
## 值绑定模式(Value-Binding Pattern) {#value-binding_pattern}
|
||||
## 值绑定模式(Value-Binding Pattern) {#value-binding-pattern}
|
||||
*值绑定模式*把匹配到的值绑定给一个变量或常量。把匹配到的值绑定给常量时,用关键字 `let`,绑定给变量时,用关键字 `var`。
|
||||
|
||||
在值绑定模式中的标识符模式会把新命名的变量或常量与匹配到的值做绑定。例如,你可以拆开一个元组,然后把每个元素绑定到相应的标识符模式中。
|
||||
@ -88,7 +88,7 @@ case let (x, y):
|
||||
> *值绑定模式* → **var** [*模式*](#pattern) | **let** [*模式*](#pattern)
|
||||
>
|
||||
|
||||
## 元组模式 {#tuple_pattern}
|
||||
## 元组模式 {#tuple-pattern}
|
||||
*元组模式*是由逗号分隔的,具有零个或多个模式的列表,并由一对圆括号括起来。元组模式匹配相应元组类型的值。
|
||||
|
||||
你可以使用类型标注去限制一个元组模式能匹配哪种元组类型。例如,在常量声明 `let (x, y): (Int, Int) = (1, 2)` 中的元组模式 `(x, y): (Int, Int)` 只匹配两个元素都是 `Int` 类型的元组。
|
||||
@ -126,7 +126,7 @@ let (a): Int = 2 // a: Int = 2
|
||||
> *元组模式元素* → [*模式*](#pattern)
|
||||
>
|
||||
|
||||
## 枚举用例模式(Enumeration Case Pattern) {#enumeration_case_pattern}
|
||||
## 枚举用例模式(Enumeration Case Pattern) {#enumeration-case-pattern}
|
||||
*枚举用例模式*匹配现有的某个枚举类型的某个用例。枚举用例模式出现在 `switch` 语句中的 `case` 标签中,以及 `if`、`while`、`guard` 和 `for-in` 语句的 `case` 条件中。
|
||||
|
||||
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../chapter2/08_Enumerations.md#associated_values)。
|
||||
@ -138,7 +138,7 @@ let (a): Int = 2 // a: Int = 2
|
||||
> *枚举用例模式* → [*类型标识*](./03_Types.md#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](./06_Declarations.md#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
|
||||
>
|
||||
|
||||
## 可选模式(Optional Pattern) {#optional_pattern}
|
||||
## 可选模式(Optional Pattern) {#optional-pattern}
|
||||
*可选模式*匹配包装在一个 `Optional(Wrapped)` 或者 `ExplicitlyUnwrappedOptional(Wrapped)` 枚举中的 `Some(Wrapped)` 用例中的值。可选模式由一个标识符模式和紧随其后的一个问号组成,可以像枚举用例模式一样使用。
|
||||
|
||||
由于可选模式是 `Optional` 和 `ImplicitlyUnwrappedOptional` 枚举用例模式的语法糖,下面两种写法是等效的:
|
||||
@ -176,7 +176,7 @@ for case let number? in arrayOfOptinalInts {
|
||||
> *可选模式* → [*标识符模式*](./03_Types.md#type-identifier) **?**
|
||||
>
|
||||
|
||||
## 类型转换模式(Type-Casting Patterns) {#type-casting_patterns}
|
||||
## 类型转换模式(Type-Casting Patterns) {#type-casting-patterns}
|
||||
有两种类型转换模式,`is` 模式和 `as` 模式。`is` 模式只出现在 `switch` 语句中的 `case` 标签中。`is` 模式和 `as` 模式形式如下:
|
||||
|
||||
> is `类型`
|
||||
@ -205,7 +205,7 @@ for case let number? in arrayOfOptinalInts {
|
||||
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.md#type)
|
||||
>
|
||||
|
||||
## 表达式模式(Expression Pattern) {#expression_pattern}
|
||||
## 表达式模式(Expression Pattern) {#expression-pattern}
|
||||
*表达式模式*代表表达式的值。表达式模式只出现在 `switch` 语句中的 `case` 标签中。
|
||||
|
||||
表达式模式代表的表达式会使用 Swift 标准库中的 `~=` 运算符与输入表达式的值进行比较。如果 `~=` 运算符返回 `true`,则匹配成功。默认情况下,`~=` 运算符使用 `==` 运算符来比较两个相同类型的值。它也可以将一个整型数值与一个 `Range` 实例中的一段整数区间做匹配,正如下面这个例子所示:
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
关于 Swift 语言的泛型概述,请参阅 [泛型](../chapter2/22_Generics.md)。
|
||||
|
||||
## 泛型形参子句 {#generic_parameter}
|
||||
## 泛型形参子句 {#generic-parameter}
|
||||
*泛型形参子句*指定泛型类型或函数的类型形参,以及这些参数相关的约束和要求。泛型形参子句用尖括号(`<>`)包住,形式如下:
|
||||
|
||||
> <`泛型形参列表`>
|
||||
@ -36,7 +36,7 @@ simpleMax(17, 42) // T 被推断为 Int 类型
|
||||
simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
```
|
||||
|
||||
### Where 子句 {#where_clauses}
|
||||
### Where 子句 {#where-clauses}
|
||||
要想对类型形参及其关联类型指定额外要求,可以在函数体或者类型的大括号之前添加 `where` 子句。`where` 子句由关键字 `where` 及其后的用逗号分隔的一个或多个要求组成。
|
||||
|
||||
> `where` : `类型要求`
|
||||
@ -97,7 +97,7 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
> *同类型约束* → [*类型标识符*](./03_Types.md#type-identifier) **==** [*类型*](./03_Types.md#type)
|
||||
>
|
||||
|
||||
## 泛型实参子句 {#generic_argument}
|
||||
## 泛型实参子句 {#generic-argument}
|
||||
*泛型实参子句*指定泛型类型的类型实参。泛型实参子句用尖括号(`<>`)包住,形式如下:
|
||||
|
||||
> <`泛型实参列表`>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
# 语法总结(Summary of the Grammar)
|
||||
|
||||
## 词法结构 {#lexical_structure}
|
||||
## 词法结构 {#lexical-structure}
|
||||
|
||||
> 空白字符语法
|
||||
>
|
||||
@ -1633,7 +1633,7 @@
|
||||
> *表达式模式* → [表达式](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
## 泛型参数 {#generic_parameters_and_arguments}
|
||||
## 泛型参数 {#generic-parameters-and-arguments}
|
||||
|
||||
> 泛型形参子句语法
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user