Translations for Swift 4.1 (#777)
* Translations for Chapter 3 part 01 for Swift 4.0/4.1 * update chapter 3 part 02 for swift 4.1 * update chapter 3 part 03 for Swift 4.1 * update chapter 1 part 01 for Swift 4.1 * update chapter 1 part 02 for Swift 4.1 * update chapter 1 part 03 for Swift 4.1 * update chapter 2 part 01 for Swift 4.1 * update chapter 2 part 02 for Swift 4.1 * update chapter 2 part 3 for Swift 4.1 * update "summary" and corrected file names * update chapter 2 part 4 for Swift 4.1 * update chapter 2 part 5 for Swift 4.1 * update chapter 2 part 6 for Swift 4.1 * update chapter 2 parts 06-11 and other parts' errors * update chapter 2 parts 12-14 * update chapter 2 parts 14-19 * update all chapter 2 * update whole chapter 1 * update some parts of chapter 3
This commit is contained in:
@ -15,7 +15,10 @@
|
||||
> 校对:[175](https://github.com/Brian175)
|
||||
|
||||
> 3.0
|
||||
> 翻译+校对:[chenmingjia](https://github.com/chenmingjia)
|
||||
> 翻译+校对:[chenmingjia](https://github.com/chenmingjia)
|
||||
|
||||
> 4.1
|
||||
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
|
||||
|
||||
本页包含内容:
|
||||
|
||||
@ -48,7 +51,8 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
|
||||
通过前缀表达式和二元表达式可以对简单表达式使用各种运算符。基本表达式从概念上讲是最简单的一种表达式,它是一种访问值的方式。后缀表达式则允许你建立复杂的表达式,例如函数调用和成员访问。每种表达式都在下面有详细论述。
|
||||
|
||||
> 表达式语法
|
||||
> 表达式语法
|
||||
>
|
||||
<a name="expression"></a>
|
||||
> *表达式* → [*try运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
|
||||
<a name="expression-list"></a>
|
||||
@ -65,7 +69,8 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
|
||||
除了标准库运算符,你也可以对某个变量使用 `&` 运算符,从而将其传递给函数的输入输出参数。更多信息,请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。
|
||||
|
||||
> 前缀表达式语法
|
||||
> 前缀表达式语法
|
||||
>
|
||||
<a name="prefix-expression"></a>
|
||||
> *前缀表达式* → [*前缀运算符*](02_Lexical_Structure.md#prefix-operator)<sub>可选</sub> [*后缀表达式*](#postfix-expression)
|
||||
> *前缀表达式* → [*输入输出表达式*](#in-out-expression)
|
||||
@ -73,7 +78,7 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
> *输入输出表达式* → **&** [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
|
||||
<a name="try_operator"></a>
|
||||
### try 运算符
|
||||
### Try 运算符
|
||||
|
||||
try 表达式由 `try` 运算符加上紧随其后的可抛出错误的表达式组成,形式如下:
|
||||
|
||||
@ -102,7 +107,8 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
`try` 表达式不能出现在二进制运算符的的右侧,除非二进制运算符是赋值运算符或者 `try` 表达式是被圆括号括起来的。
|
||||
|
||||
关于 `try`、`try?` 和 `try!` 的更多信息,以及该如何使用的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html)。
|
||||
> try 表达式语法
|
||||
> Try 表达式语法
|
||||
>
|
||||
<a name="try-operator"></a>
|
||||
> *try 运算符* → **try** | **try?** | **try!**
|
||||
|
||||
@ -117,11 +123,13 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
|
||||
关于 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)。
|
||||
|
||||
> 注意
|
||||
> 注意
|
||||
>
|
||||
> 在解析时,一个二元表达式将作为一个扁平列表表示,然后根据运算符的优先级,再进一步进行组合。例如,`2 + 3 * 5` 首先被看作具有五个元素的列表,即 `2`、`+`、`3`、`*`、`5`,随后根据运算符优先级组合为 `(2 + (3 * 5))`。
|
||||
|
||||
<a name="binary-expression"></a>
|
||||
> 二元表达式语法
|
||||
> 二元表达式语法
|
||||
>
|
||||
> *二元表达式* → [*二元运算符*](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)
|
||||
@ -145,7 +153,8 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
|
||||
赋值运算符不返回任何值。
|
||||
|
||||
> 赋值运算符语法
|
||||
> 赋值运算符语法
|
||||
>
|
||||
<a name="assignment-operator"></a>
|
||||
> *赋值运算符* → **=**
|
||||
|
||||
@ -160,7 +169,8 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
|
||||
关于使用三元条件运算符的例子,请参阅 [三元条件运算符](../chapter2/02_Basic_Operators.md#ternary_conditional_operator)。
|
||||
|
||||
> 三元条件运算符语法
|
||||
> 三元条件运算符语法
|
||||
>
|
||||
<a name="conditional-operator"></a>
|
||||
> *三元条件运算符* → **?** [try运算符](#try-operator)<sub>可选</sub> [*表达式*](#expression) **:**
|
||||
|
||||
@ -202,7 +212,8 @@ f(x as Any)
|
||||
关于类型转换的更多内容和例子,请参阅 [类型转换](../chapter2/19_Type_Casting.md)。
|
||||
|
||||
<a name="type-casting-operator"></a>
|
||||
> 类型转换运算符语法
|
||||
> 类型转换运算符语法
|
||||
>
|
||||
> *类型转换运算符* → **is** [*类型*](03_Types.md#type)
|
||||
> *类型转换运算符* → **as** [*类型*](03_Types.md#type)
|
||||
> *类型转换运算符* → **as** **?** [*类型*](03_Types.md#type)
|
||||
@ -213,7 +224,8 @@ f(x as Any)
|
||||
|
||||
基本表达式是最基本的表达式。它们可以单独使用,也可以跟前缀表达式、二元表达式、后缀表达式组合使用。
|
||||
|
||||
> 基本表达式语法
|
||||
> 基本表达式语法
|
||||
>
|
||||
<a name="primary-expression"></a>
|
||||
> *基本表达式* → [*标识符*](02_Lexical_Structure.md#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub>
|
||||
> *基本表达式* → [*字面量表达式*](#literal-expression)
|
||||
@ -273,8 +285,8 @@ var emptyArray: [Double] = []
|
||||
var emptyDictionary: [String : Double] = [:]
|
||||
```
|
||||
|
||||
> 字面量表达式语法
|
||||
|
||||
> 字面量表达式语法
|
||||
>
|
||||
<a name="literal-expression"></a>
|
||||
> *字面量表达式* → [*字面量*](02_Lexical_Structure.md#literal)
|
||||
> *字面量表达式* → [*数组字面量*](#array-literal) | [*字典字面量*](#dictionary-literal)
|
||||
@ -295,7 +307,7 @@ var emptyDictionary: [String : Double] = [:]
|
||||
> *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)
|
||||
|
||||
<a name="self_expression"></a>
|
||||
### self 表达式
|
||||
### Self 表达式
|
||||
|
||||
`self` 表达式是对当前类型或者当前实例的显式引用,它有如下形式:
|
||||
|
||||
@ -329,7 +341,8 @@ struct Point {
|
||||
}
|
||||
```
|
||||
|
||||
> self 表达式语法
|
||||
> Self 表达式语法
|
||||
>
|
||||
<a name="self-expression"></a>
|
||||
> *self 表达式* → **self** | [*self 方法表达式*](#self-method-expression) | [*self 下标表达式*](#self-subscript-expression) | [*self 构造器表达式*](#self-initializer-expression)
|
||||
>
|
||||
@ -341,9 +354,9 @@ struct Point {
|
||||
> *self 构造器表达式* → **self** **.** **init**
|
||||
|
||||
<a name="superclass_expression"></a>
|
||||
### 超类表达式
|
||||
### 父类表达式
|
||||
|
||||
超类表达式可以使我们在某个类中访问它的超类,它有如下形式:
|
||||
*父类*表达式可以使我们在某个类中访问它的超类,它有如下形式:
|
||||
|
||||
> super.`成员名称`
|
||||
> super[`下标索引`]
|
||||
@ -353,9 +366,11 @@ struct Point {
|
||||
|
||||
子类可以通过超类表达式在它们的成员、下标和构造器中使用超类中的实现。
|
||||
|
||||
> 超类表达式语法
|
||||
> 父类表达式语法
|
||||
>
|
||||
<a name="superclass-expression"></a>
|
||||
> *超类表达式* → [*超类方法表达式*](#superclass-method-expression) | [*超类下标表达式*](#superclass-subscript-expression) | [*超类构造器表达式*](#superclass-initializer-expression)
|
||||
> *超类表达式* → [*超类方法表达式*](#superclass-method-expression) | [*超类下标表达式*](#superclass-subscript-expression) | [*超类构造器表达式*](#superclass-initializer-expression)
|
||||
>
|
||||
<a name="superclass-method-expression"></a>
|
||||
> *超类方法表达式* → **super** **.** [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
<a name="superclass-subscript-expression"></a>
|
||||
@ -366,7 +381,7 @@ struct Point {
|
||||
<a name="closure_expression"></a>
|
||||
### 闭包表达式
|
||||
|
||||
闭包表达式会创建一个闭包,在其他语言中也叫 *lambda* 或匿名函数。跟函数一样,闭包包含了待执行的代码,不同的是闭包还会捕获所在环境中的常量和变量。它的形式如下:
|
||||
闭包表达式会创建一个闭包,在其他语言中也叫 *lambda* 或*匿名*函数。跟函数一样,闭包包含了待执行的代码,不同的是闭包还会捕获所在环境中的常量和变量。它的形式如下:
|
||||
|
||||
```swift
|
||||
{ (parameters) -> return type in
|
||||
@ -400,7 +415,9 @@ myFunction { return $0 + $1 }
|
||||
myFunction { $0 + $1 }
|
||||
```
|
||||
|
||||
关于如何将闭包作为参数来传递的内容,请参阅 [函数调用表达式](#function_call_expression)。
|
||||
关于如何将闭包作为参数来传递的内容,请参阅 [函数调用表达式](#function_call_expression)。
|
||||
|
||||
关于逃逸闭包的内容,请参阅[逃逸闭包](./chapter2/07_Closures.md#escaping_closures)
|
||||
|
||||
#### 捕获列表
|
||||
|
||||
@ -460,8 +477,8 @@ 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)。
|
||||
|
||||
> 闭包表达式语法
|
||||
|
||||
> 闭包表达式语法
|
||||
>
|
||||
<a name="closure-expression"></a>
|
||||
> *闭包表达式* → **{** [*闭包签名*](#closure-signature)<sub>可选</sub> [*语句*](10_Statements.md#statements) **}**
|
||||
|
||||
@ -495,26 +512,38 @@ var x = MyEnumeration.SomeValue
|
||||
x = .AnotherValue
|
||||
```
|
||||
|
||||
> 隐式成员表达式语法
|
||||
> 隐式成员表达式语法
|
||||
>
|
||||
<a name="implicit-member-expression"></a>
|
||||
> *隐式成员表达式* → **.** [*标识符*](02_Lexical_Structure.md#identifier)
|
||||
|
||||
<a name="parenthesized_expression"></a>
|
||||
### 圆括号表达式
|
||||
|
||||
*圆括号表达式*是由圆括号包围的表达式。你可以用圆括号说明成组的表达式的先后操作。成组的圆括号不会改变表达式的类型 - 例如`(1)`的类型就是简单的`Int`。
|
||||
|
||||
> 圆括号表达式语法
|
||||
>
|
||||
<a name="parenthesized-expression"></a>
|
||||
> *圆括号表达式* → **( [*表达式*](#expression) )**
|
||||
|
||||
<a name="Tuple_Expression"></a>
|
||||
### 元组表达式
|
||||
|
||||
圆括号表达式由圆括号和其中多个逗号分隔的子表达式组成。每个子表达式前面可以有一个标识符,用冒号隔开。圆括号表达式形式如下:
|
||||
元组表达式由圆括号和其中多个逗号分隔的子表达式组成。每个子表达式前面可以有一个标识符,用冒号隔开。元组表达式形式如下:
|
||||
|
||||
> (`标识符 1` : `表达式 1`, `标识符 2` : `表达式 2`, `...`)
|
||||
|
||||
使用圆括号表达式来创建元组,然后将其作为参数传递给函数。如果某个圆括号表达式中只有一个子表达式,那么它的类型就是子表达式的类型。例如,表达式 `(1)` 的类型是 `Int`,而不是 `(Int)`。
|
||||
元组表达式可以一个表达式都没有,也可以包含两个或是更多的表达式。单个表达式用括号括起来就是括号表达式了。
|
||||
|
||||
> 圆括号表达式语法
|
||||
<a name="parenthesized-expression"></a>
|
||||
> *圆括号表达式* → **(** [*表达式元素列表*](#expression-element-list)<sub>可选</sub> **)**
|
||||
<a name="expression-element-list"></a>
|
||||
> *表达式元素列表* → [*表达式元素*](#expression-element) | [*表达式元素*](#expression-element) **,** [*表达式元素列表*](#expression-element-list)
|
||||
<a name="expression-element"></a>
|
||||
> *表达式元素* → [*表达式*](#expression) | [*标识符*](02_Lexical_Structure.md#identifier) **:** [*表达式*](#expression)
|
||||
> 元组表达式语法
|
||||
>
|
||||
<a name="tuple-expression"></a>
|
||||
> *元组表达式* → **( )** | **(**[*元组元素*](#tuple-element), [*元组元素列表*](#tuple-element-list) **)**
|
||||
<a name="tuple-element-list"></a>
|
||||
> *元组元素列表* → [*元组元素*](#tuple-element) | [*元组元素*](#tuple-element) **,** [*元组元素列表*](#tuple-element-list)
|
||||
<a name="tuple-element"></a>
|
||||
> *元组元素* → [*表达式*](#expression) | [*标识符*](identifier) **:** [*表达式*](#expression)
|
||||
|
||||
<a name="wildcard_expression"></a>
|
||||
### 通配符表达式
|
||||
@ -526,7 +555,8 @@ x = .AnotherValue
|
||||
// x 为 10,20 被忽略
|
||||
```
|
||||
|
||||
> 通配符表达式语法
|
||||
> 通配符表达式语法
|
||||
>
|
||||
<a name="wildcard-expression"></a>
|
||||
> *通配符表达式* → **_**
|
||||
|
||||
|
||||
Reference in New Issue
Block a user