fix all anchor format
This commit is contained in:
@ -4,8 +4,7 @@
|
||||
|
||||
Swift 语言相对较小,这是由于 Swift 代码中常用的类型、函数以及运算符都已经在 Swift 标准库中定义了。虽然这些类型、函数和运算符并不是 Swift 语言自身的一部分,但是它们被广泛应用于本书的讨论和代码范例中。
|
||||
|
||||
<a name="how_to_read_the_grammar"></a>
|
||||
## 如何阅读语法
|
||||
## 如何阅读语法 {#how_to_read_the_grammar}
|
||||
|
||||
用来描述 Swift 编程语言形式语法的符号遵循下面几个约定:
|
||||
|
||||
|
||||
@ -4,8 +4,7 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
|
||||
通常情况下,通过考虑输入文本当中可能的最长子串,并且在随后将介绍的语法约束之下,根据随后将介绍的语法约束生成的,根据 Swift 源文件当中的字符来生成相应的“符号”。这种方法称为*“最长匹配(longest match)”*,或者*“最大适合(maximal munch)”*。
|
||||
|
||||
<a id="whitespace_and_comments"></a>
|
||||
## 空白与注释
|
||||
## 空白与注释 {#whitespace_and_comments}
|
||||
|
||||
空白(whitespace)有两个用途:分隔源文件中的符号以及帮助区分运算符属于前缀还是后缀(参见 [运算符](#operators)),在其他情况下空白则会被忽略。以下的字符会被当作空白:空格(U+0020)、换行符(U+000A)、回车符(U+000D)、水平制表符(U+0009)、垂直制表符(U+000B)、换页符(U+000C)以及空字符(U+0000)。
|
||||
|
||||
@ -14,7 +13,7 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
> 空白语法
|
||||
>
|
||||
|
||||
<a id="whitespace"></a>
|
||||
whitespace {#whitespace}
|
||||
> *空白* → [*空白项*](#whitespace-item) [*空白*](#whitespace)<sub>可选</sub>
|
||||
>
|
||||
> *空白项* → [*断行符*](#line-break)
|
||||
@ -26,7 +25,7 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
> *空白项* → U+0000,U+0009,U+000B,U+000C 或者 U+0020
|
||||
>
|
||||
|
||||
<a id="line-break"></a>
|
||||
line-break {#line-break}
|
||||
> *断行符* → U+000A
|
||||
>
|
||||
> *断行符* → U+000D
|
||||
@ -34,7 +33,7 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
> *断行符* → U+000D 接着是 U+000A
|
||||
>
|
||||
|
||||
<a id="comment"></a>
|
||||
comment {#comment}
|
||||
> *注释* → // [*注释内容 断行*](#comment-text line-break)
|
||||
>
|
||||
> *多行注释* → `/*` [*多行注释内容*](#multiline-commnet-text) `*/`
|
||||
@ -54,8 +53,7 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
|
||||
注释可以包含额外的格式和标记,正如 [*Markup Formatting Reference*](https://developer.apple.com/library/archive/documentation/Xcode/Reference/xcode_markup_formatting_ref/index.html) 所述。
|
||||
|
||||
<a id="identifiers"></a>
|
||||
## 标识符
|
||||
## 标识符 {#identifiers}
|
||||
|
||||
*标识符(identifier)* 可以由以下的字符开始:大写或小写的字母 `A` 到 `Z`、下划线(`_`)、基本多文种平面(Basic Multilingual Plane)中非字符数字组合的 Unicode 字符以及基本多文种平面以外的非个人专用区字符。在首字符之后,允许使用数字和组合 Unicode 字符。
|
||||
|
||||
@ -66,7 +64,7 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
> 标识符语法
|
||||
>
|
||||
|
||||
<a id="identifier"></a>
|
||||
identifier {#identifier}
|
||||
> *标识符* → [*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub>
|
||||
>
|
||||
> *标识符* → \`[*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub>\`
|
||||
@ -74,11 +72,11 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
> *标识符* → [*隐式参数名*](#implicit-parameter-name)
|
||||
>
|
||||
|
||||
<a id="identifier-list"></a>
|
||||
identifier-list {#identifier-list}
|
||||
> *标识符列表* → [*标识符*](#identifier) | [*标识符*](#identifier) **,** [*标识符列表*](#identifier-list)
|
||||
>
|
||||
|
||||
<a id="identifier-head"></a>
|
||||
identifier-head {#identifier-head}
|
||||
> *头部标识符* → 大写或小写字母 A - Z
|
||||
>
|
||||
> *头部标识符* → _
|
||||
@ -112,24 +110,23 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
> *头部标识符* → U+D0000–U+DFFFD 或者 U+E0000–U+EFFFD
|
||||
>
|
||||
|
||||
<a id="identifier-character"></a>
|
||||
identifier-character {#identifier-character}
|
||||
> *标识符字符* → 数值 0 - 9
|
||||
>
|
||||
> *标识符字符* → U+0300–U+036F,U+1DC0–U+1DFF,U+20D0–U+20FF,或者 U+FE20–U+FE2F
|
||||
>
|
||||
> *标识符字符* → [*头部标识符*](#identifier-head)
|
||||
>
|
||||
> <a id="identifier-characters"></a>
|
||||
> identifier-characters {#identifier-characters}
|
||||
>
|
||||
> *标识符字符组* → [*标识符字符*](#identifier-character) [*标识符字符组*](#identifier-characters)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a id="implicit-parameter-name"></a>
|
||||
implicit-parameter-name {#implicit-parameter-name}
|
||||
> *隐式参数名* → **$** [*十进制数字列表*](#decimal-digits)
|
||||
>
|
||||
|
||||
<a id="keywords"></a>
|
||||
## 关键字和标点符号
|
||||
## 关键字和标点符号 {#keywords}
|
||||
|
||||
下面这些被保留的关键字不允许用作标识符,除非使用反引号转义,具体描述请参考 [标识符](#identifiers)。除了 `inout`、`var` 以及 `let` 之外的关键字可以用作某个函数声明或者函数调用当中的外部参数名,不用添加反引号转义。
|
||||
|
||||
@ -142,8 +139,7 @@ Swift 的*“词法结构(lexical structure)”* 描述了能构成该语言
|
||||
|
||||
以下符号被当作保留符号,不能用于自定义运算符: `(`、`)`、`{`、`}`、`[`、`]`、`.`、`,`、`:`、`;`、`=`、`@`、`#`、`&`(作为前缀运算符)、`->`、`` ` ``、`?`、`!`(作为后缀运算符)。
|
||||
|
||||
<a id="literals"></a>
|
||||
## 字面量
|
||||
## 字面量 {#literals}
|
||||
|
||||
*字面量(literal)* 用来表示源码中某种特定类型的值,比如一个数字或字符串。
|
||||
|
||||
@ -165,20 +161,19 @@ true // 布尔值字面量
|
||||
> *字面量* → [*数值字面量*](#numeric-literal) | [*字符串字面量*](#string-literal) | [*布尔值字面量*](#boolean-literal) | [*nil 字面量*](#nil-literal)
|
||||
>
|
||||
|
||||
<a id="numeric-literal"></a>
|
||||
numeric-literal {#numeric-literal}
|
||||
> *数值字面量* → **-**<sub>可选</sub> [*整数字面量*](#integer-literal) | **-**<sub>可选</sub> [*浮点数字面量*](#floating-point-literal)
|
||||
>
|
||||
> <a id="boolean-literal"></a>
|
||||
> boolean-literal {#boolean-literal}
|
||||
>
|
||||
> *布尔值字面量* → **true** | **false**
|
||||
>
|
||||
> <a id="nil-literal"></a>
|
||||
> nil-literal {#nil-literal}
|
||||
>
|
||||
> *nil 字面量* → **nil**
|
||||
>
|
||||
|
||||
<a id="integer_literals"></a>
|
||||
### 整数字面量
|
||||
### 整数字面量 {#integer_literals}
|
||||
|
||||
*整数字面量(Integer Literals)* 表示未指定精度整数的值。整数字面量默认用十进制表示,可以加前缀来指定其他的进制。二进制字面量加 `0b`,八进制字面量加 `0o`,十六进制字面量加 `0x`。
|
||||
|
||||
@ -192,7 +187,7 @@ true // 布尔值字面量
|
||||
|
||||
> 整数字面量语法
|
||||
>
|
||||
<a id="integer-literal"></a>
|
||||
integer-literal {#integer-literal}
|
||||
> *整数字面量* → [*二进制字面量*](#binary-literal)
|
||||
>
|
||||
> *整数字面量* → [*八进制字面量*](#octal-literal)
|
||||
@ -202,76 +197,75 @@ true // 布尔值字面量
|
||||
> *整数字面量* → [*十六进制字面量*](#hexadecimal-literal)
|
||||
>
|
||||
|
||||
<a id="binary-literal"></a>
|
||||
binary-literal {#binary-literal}
|
||||
> *二进制字面量* → **0b** [*二进制数字*](#binary-digit) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
> <a id="binary-digit"></a>
|
||||
> binary-digit {#binary-digit}
|
||||
>
|
||||
> *二进制数字* → 数值 0 到 1
|
||||
>
|
||||
> <a id="binary-literal-character"></a>
|
||||
> binary-literal-character {#binary-literal-character}
|
||||
>
|
||||
> *二进制字面量字符* → [*二进制数字*](#binary-digit) | _
|
||||
>
|
||||
> <a id="binary-literal-characters"></a>
|
||||
> binary-literal-characters {#binary-literal-characters}
|
||||
>
|
||||
> *二进制字面量字符组* → [*二进制字面量字符*](#binary-literal-character) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a id="octal-literal"></a>
|
||||
octal-literal {#octal-literal}
|
||||
> *八进制字面量* → **0o** [*八进字数字*](#octal-digit) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
> <a id="octal-digit"></a>
|
||||
> octal-digit {#octal-digit}
|
||||
>
|
||||
> *八进字数字* → 数值 0 到 7
|
||||
>
|
||||
> <a id="octal-literal-character"></a>
|
||||
> octal-literal-character {#octal-literal-character}
|
||||
>
|
||||
> *八进制字符* → [*八进字数字*](#octal-digit) | _
|
||||
>
|
||||
> <a id="octal-literal-characters"></a>
|
||||
> octal-literal-characters {#octal-literal-characters}
|
||||
>
|
||||
> *八进制字符组* → [*八进制字符*](#octal-literal-character) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a id="decimal-literal"></a>
|
||||
decimal-literal {#decimal-literal}
|
||||
> *十进制字面量* → [*十进制数字*](#decimal-digit) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
> <a id="decimal-digit"></a>
|
||||
> decimal-digit {#decimal-digit}
|
||||
>
|
||||
> *十进制数字* → 数值 0 到 9
|
||||
>
|
||||
> <a id="decimal-digits"></a>
|
||||
> decimal-digits {#decimal-digits}
|
||||
>
|
||||
> *十进制数字组* → [*十进制数字*](#decimal-digit) [*十进制数字组*](#decimal-digits)<sub>可选</sub>
|
||||
>
|
||||
> <a id="decimal-literal-character"></a>
|
||||
> decimal-literal-character {#decimal-literal-character}
|
||||
>
|
||||
> *十进制字符* → [*十进制数字*](#decimal-digit) | _
|
||||
>
|
||||
> <a id="decimal-literal-characters"></a>
|
||||
> decimal-literal-characters {#decimal-literal-characters}
|
||||
>
|
||||
> *十进制字符组* → [*十进制字符*](#decimal-literal-character) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a id="hexadecimal-literal"></a>
|
||||
hexadecimal-literal {#hexadecimal-literal}
|
||||
> *十六进制字面量* → **0x** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
> <a id="hexadecimal-digit"></a>
|
||||
> hexadecimal-digit {#hexadecimal-digit}
|
||||
>
|
||||
> *十六进制数字* → 数值 0 到 9, 字母 a 到 f, 或 A 到 F
|
||||
>
|
||||
> <a id="hexadecimal-literal-character"></a>
|
||||
> hexadecimal-literal-character {#hexadecimal-literal-character}
|
||||
>
|
||||
> *十六进制字符* → [*十六进制数字*](#hexadecimal-digit) | _
|
||||
>
|
||||
> <a id="hexadecimal-literal-characters"></a>
|
||||
> hexadecimal-literal-characters {#hexadecimal-literal-characters}
|
||||
>
|
||||
> *十六进制字面量字符组* → [*十六进制字符*](#hexadecimal-literal-character) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a id="floating_point_literals"></a>
|
||||
### 浮点数字面量
|
||||
### 浮点数字面量 {#floating_point_literals}
|
||||
|
||||
*浮点数字面量(Floating-point literals)* 表示未指定精度浮点数的值。
|
||||
|
||||
@ -289,42 +283,41 @@ true // 布尔值字面量
|
||||
|
||||
> 浮点数字面量语法
|
||||
>
|
||||
<a id="floating-point-literal"></a>
|
||||
floating-point-literal {#floating-point-literal}
|
||||
> *浮点数字面量* → [*十进制字面量*](#decimal-literal) [*十进制分数*](#decimal-fraction)<sub>可选</sub> [*十进制指数*](#decimal-exponent)<sub>可选</sub>
|
||||
>
|
||||
> *浮点数字面量* → [*十六进制字面量*](#hexadecimal-literal) [*十六进制分数*](#hexadecimal-fraction)<sub>可选</sub> [*十六进制指数*](#hexadecimal-exponent)
|
||||
>
|
||||
|
||||
<a id="decimal-fraction"></a>
|
||||
decimal-fraction {#decimal-fraction}
|
||||
> *十进制分数* → **.** [*十进制字面量*](#decimal-literal)
|
||||
>
|
||||
> <a id="decimal-exponent"></a>
|
||||
> decimal-exponent {#decimal-exponent}
|
||||
>
|
||||
> *十进制指数* → [*十进制指数 e*](#floating-point-e) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal)
|
||||
>
|
||||
|
||||
<a id="hexadecimal-fraction"></a>
|
||||
hexadecimal-fraction {#hexadecimal-fraction}
|
||||
> *十六进制分数* → **.** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
|
||||
>
|
||||
> <a id="hexadecimal-exponent"></a>
|
||||
> hexadecimal-exponent {#hexadecimal-exponent}
|
||||
>
|
||||
> *十六进制指数* → [*十六进制指数 p*](#floating-point-p) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal)
|
||||
>
|
||||
|
||||
<a id="floating-point-e"></a>
|
||||
floating-point-e {#floating-point-e}
|
||||
> *十进制指数 e* → **e** | **E**
|
||||
>
|
||||
> <a id="floating-point-p"></a>
|
||||
> floating-point-p {#floating-point-p}
|
||||
>
|
||||
> *十六进制指数 p* → **p** | **P**
|
||||
>
|
||||
> <a id="sign"></a>
|
||||
> sign {#sign}
|
||||
>
|
||||
> *正负号* → **+** | **-**
|
||||
>
|
||||
|
||||
<a id="string_literals"></a>
|
||||
### 字符串字面量
|
||||
### 字符串字面量 {#string_literals}
|
||||
|
||||
字符串字面量是被引号包括的一串字符组成。 单行字符串字面量被包在双引号中的一串字符组成,形式如下:
|
||||
|
||||
@ -385,48 +378,47 @@ let textB = "Hello world"
|
||||
|
||||
> 字符串字面量语法
|
||||
>
|
||||
<a id="string-literal"></a>
|
||||
string-literal {#string-literal}
|
||||
> *字符串字面量* → [*静态字符串字面量*](#static-string-literal) | [*插值字符串字面量*](#interpolated-string-literal)
|
||||
>
|
||||
|
||||
<a id="static-string-literal"></a>
|
||||
static-string-literal {#static-string-literal}
|
||||
> *静态字符串字面量* → **"**[*引用文本*](#quoted-text)<sub>可选</sub>**"**
|
||||
>
|
||||
> <a id="quoted-text"></a>
|
||||
> quoted-text {#quoted-text}
|
||||
>
|
||||
> *引用文本* → [*引用文本项*](#quoted-text-item) [*引用文本*](#quoted-text)<sub>可选</sub>
|
||||
>
|
||||
> <a id="quoted-text-item"></a>
|
||||
> quoted-text-item {#quoted-text-item}
|
||||
>
|
||||
> *引用文本项* → [*转义字符*](#escaped-character)
|
||||
>
|
||||
> *引用文本项* → 除了 **"**、**\\**、U+000A、U+000D 以外的所有 Unicode 字符
|
||||
>
|
||||
|
||||
<a id="interpolated-string-literal"></a>
|
||||
interpolated-string-literal {#interpolated-string-literal}
|
||||
> *插值字符串字面量* → **"**[*插值文本*](#interpolated-text)<sub>可选</sub>**"**
|
||||
>
|
||||
> <a id="interpolated-text"></a>
|
||||
> interpolated-text {#interpolated-text}
|
||||
>
|
||||
> *插值文本* → [*插值文本项*](#interpolated-text-item) [*插值文本*](#interpolated-text)<sub>可选</sub>
|
||||
>
|
||||
> <a id="interpolated-text-item"></a>
|
||||
> interpolated-text-item {#interpolated-text-item}
|
||||
>
|
||||
> *插值文本项* → **\\****(**[*表达式*](./04_Expressions.md)**)** | [*引用文本项*](#quoted-text-item)
|
||||
>
|
||||
|
||||
<a id="escaped-character"></a>
|
||||
escaped-character {#escaped-character}
|
||||
> *转义字符* → **\\****0** | **\\****\\** | **\t** | **\n** | **\r** | **\\"** | **\\'**
|
||||
>
|
||||
> *转义字符* → **\u {** [*unicode 标量数字*](#unicode-scalar-digits) **}**
|
||||
>
|
||||
> <a id="unicode-scalar-digits"></a>
|
||||
> unicode-scalar-digits {#unicode-scalar-digits}
|
||||
>
|
||||
> *unicode 标量数字* → 一到八位的十六进制数字
|
||||
>
|
||||
|
||||
<a id="operators"></a>
|
||||
## 运算符
|
||||
## 运算符 {#operators}
|
||||
|
||||
Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基础运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/26_Advanced_Operators.md) 中进行了阐述。这一小节将描述哪些字符能用于自定义运算符。
|
||||
|
||||
@ -458,13 +450,13 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
|
||||
|
||||
> 运算符语法
|
||||
>
|
||||
<a id="operator"></a>
|
||||
operator {#operator}
|
||||
> *运算符* → [*头部运算符*](#operator-head) [*运算符字符组*](#operator-characters)<sub>可选</sub>
|
||||
>
|
||||
> *运算符* → [*头部点运算符*](#dot-operator-head) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a id="operator-head"></a>
|
||||
operator-head {#operator-head}
|
||||
> *头部运算符* → **/** | **=** | **-** | **+** | **!** | __*__ | **%** | **<** | **>** | **&** | **|** | **^** | **~** | **?**
|
||||
>
|
||||
> *头部运算符* → U+00A1–U+00A7
|
||||
@ -496,7 +488,7 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
|
||||
> *头部运算符* → U+3008–U+3030
|
||||
>
|
||||
|
||||
<a id="operator-character"></a>
|
||||
operator-character {#operator-character}
|
||||
> *运算符字符* → [*头部运算符*](#operator-head)
|
||||
>
|
||||
> *运算符字符* → U+0300–U+036F
|
||||
@ -511,31 +503,31 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
|
||||
>
|
||||
> *运算符字符* → U+E0100–U+E01EF
|
||||
>
|
||||
> <a id="operator-characters"></a>
|
||||
> operator-characters {#operator-characters}
|
||||
>
|
||||
> *运算符字符组* → [*运算符字符*](#operator-character) [*运算符字符组*](#operator-characters)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a id="dot-operator-head"></a>
|
||||
dot-operator-head {#dot-operator-head}
|
||||
> *头部点运算符* → **..**
|
||||
>
|
||||
> <a id="dot-operator-character"></a>
|
||||
> dot-operator-character {#dot-operator-character}
|
||||
>
|
||||
> *点运算符字符* → **.** | [*运算符字符*](#operator-character)
|
||||
>
|
||||
> <a id="dot-operator-characters"></a>
|
||||
> dot-operator-characters {#dot-operator-characters}
|
||||
>
|
||||
> *点运算符字符组* → [*点运算符字符*](#dot-operator-character) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a id="binary-operator"></a>
|
||||
binary-operator {#binary-operator}
|
||||
> *二元运算符* → [*运算符*](#operator)
|
||||
>
|
||||
> <a id="prefix-operator"></a>
|
||||
> prefix-operator {#prefix-operator}
|
||||
>
|
||||
> *前缀运算符* → [*运算符*](#operator)
|
||||
>
|
||||
> <a id="postfix-operator"></a>
|
||||
> postfix-operator {#postfix-operator}
|
||||
>
|
||||
> *后缀运算符* → [*运算符*](#operator)
|
||||
>
|
||||
|
||||
@ -27,7 +27,7 @@ Swift 语言存在两种类型:命名型类型和复合型类型。命名型
|
||||
|
||||
> 类型语法
|
||||
>
|
||||
<a name="type"></a>
|
||||
type {#type}
|
||||
> *类型* → [*数组类型*](#array-type)
|
||||
>
|
||||
> *类型* → [*字典类型*](#dictionary-type)
|
||||
@ -53,7 +53,7 @@ Swift 语言存在两种类型:命名型类型和复合型类型。命名型
|
||||
> *类型* → [*(类型)*](#type)
|
||||
>
|
||||
|
||||
<a name="type_annotation"></a>
|
||||
type_annotation {#type_annotation}
|
||||
## 类型注解
|
||||
|
||||
类型注解显式地指定一个变量或表达式的值。类型注解始于冒号 `:` 终于类型,比如下面两个例子:
|
||||
@ -69,11 +69,11 @@ func someFunction(a: Int) { /* ... */ }
|
||||
|
||||
> 类型注解语法
|
||||
>
|
||||
<a name="type-annotation"></a>
|
||||
type-annotation {#type-annotation}
|
||||
> *类型注解* → **:** [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type)
|
||||
>
|
||||
|
||||
<a name="type_identifier"></a>
|
||||
type_identifier {#type_identifier}
|
||||
## 类型标识符
|
||||
|
||||
类型标识符引用命名型类型,还可引用命名型或复合型类型的别名。
|
||||
@ -95,14 +95,14 @@ var someValue: ExampleModule.MyType
|
||||
|
||||
> 类型标识符语法
|
||||
>
|
||||
<a name="type-identifier"></a>
|
||||
type-identifier {#type-identifier}
|
||||
> *类型标识符* → [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.md#generic_argument_clause)<sub>可选</sub> | [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.md#generic_argument_clause)<sub>可选</sub> **.** [*类型标识符*](#type-identifier)
|
||||
>
|
||||
<a name="type-name"></a>
|
||||
type-name {#type-name}
|
||||
> *类型名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
<a name="tuple_type"></a>
|
||||
tuple_type {#tuple_type}
|
||||
## 元组类型
|
||||
|
||||
元组类型是使用括号括起来的零个或多个类型,类型间用逗号隔开。
|
||||
@ -122,20 +122,20 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配
|
||||
|
||||
> 元组类型语法
|
||||
>
|
||||
<a name="tuple-type"></a>
|
||||
tuple-type {#tuple-type}
|
||||
> *元组类型* → **(** [*元组类型元素列表*](#tuple-type-element-list) <sub>可选</sub> **)**
|
||||
>
|
||||
<a name="tuple-type-element-list"></a>
|
||||
tuple-type-element-list {#tuple-type-element-list}
|
||||
> *元组类型元素列表* → [*元组类型元素*](#tuple-type-element) | [*元组类型元素*](#tuple-type-element) **,** [*元组类型元素列表*](#tuple-type-element-list)
|
||||
>
|
||||
<a name="tuple-type-element"></a>
|
||||
tuple-type-element {#tuple-type-element}
|
||||
> *元组类型元素* → [*元素名*](#element-name) [*类型注解*](#type-annotation) | [*类型*](#type)
|
||||
>
|
||||
<a name="element-name"></a>
|
||||
element-name {#element-name}
|
||||
> *元素名* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
<a name="function_type"></a>
|
||||
function_type {#function_type}
|
||||
## 函数类型
|
||||
|
||||
函数类型表示一个函数、方法或闭包的类型,它由参数类型和返回值类型组成,中间用箭头(`->`)隔开:
|
||||
@ -190,7 +190,7 @@ var operation: (Int, Int) -> Int // 正确
|
||||
|
||||
函数类型若要抛出错误就必须使用 `throws` 关键字来标记,若要重抛错误则必须使用 `rethrows` 关键字来标记。`throws` 关键字是函数类型的一部分,非抛出函数是抛出函数函数的一个子类型。因此,在使用抛出函数的地方也可以使用不抛出函数。抛出和重抛函数的相关描述见章节 [抛出函数与方法](./06_Declarations.md#throwing_functions_and_methods) 和 [重抛函数与方法](./06_Declarations.md#rethrowing_functions_and_methods)。
|
||||
|
||||
<a name="Restrictions for Nonescaping Closures"></a>
|
||||
Restrictions for Nonescaping Closures {#Restrictions for Nonescaping Closures}
|
||||
### 对非逃逸闭包的限制
|
||||
非逃逸闭包函数不能作为参数传递到另一个非逃逸闭包函数的参数。这样的限制可以让 Swift 在编译时就完成更多的内存访问冲突检查, 而不是在运行时。举个例子:
|
||||
|
||||
@ -219,27 +219,27 @@ func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) {
|
||||
|
||||
> 函数类型语法
|
||||
>
|
||||
<a name="function-type"></a>
|
||||
function-type {#function-type}
|
||||
> *函数类型* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*函数类型子句*](#function-type-argument-clause) **throws**<sub>可选</sub> **->** [*类型*](#type)
|
||||
>
|
||||
> *函数类型* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> [*函数类型子句*](#function-type-argument-clause) **rethrows** **->** [*类型*](#type)
|
||||
>
|
||||
<a name="function-type-argument-clause"></a>
|
||||
function-type-argument-clause {#function-type-argument-clause}
|
||||
> *函数类型子句* → ()
|
||||
>
|
||||
> *函数类型子句* → ([*函数类型参数列表*](#function-type-argument-list)*...*<sub>可选</sub>)
|
||||
>
|
||||
<a name="function-type-argument-list"></a>
|
||||
function-type-argument-list {#function-type-argument-list}
|
||||
> *函数类型参数列表* → [*函数类型参数*](function-type-argument) | [*函数类型参数*](function-type-argument), [*函数类型参数列表*](#function-type-argument-list)
|
||||
>
|
||||
<a name="function-type-argument"></a>
|
||||
function-type-argument {#function-type-argument}
|
||||
> *函数类型参数* → [*特性列表*](./07_Attributes.md#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type) | [*参数标签*](#argument-label) [*类型注解*](#type-annotation)
|
||||
>
|
||||
<a name="argument-label"></a>
|
||||
argument-label {#argument-label}
|
||||
> *参数标签* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
<a name="array_type"></a>
|
||||
array_type {#array_type}
|
||||
## 数组类型
|
||||
|
||||
Swift 语言为标准库中定义的 `Array<Element>` 类型提供了如下语法糖:
|
||||
@ -269,11 +269,11 @@ var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
|
||||
|
||||
> 数组类型语法
|
||||
>
|
||||
<a name="array-type"></a>
|
||||
array-type {#array-type}
|
||||
> *数组类型* → **[** [*类型*](#type) **]**
|
||||
>
|
||||
|
||||
<a name="dictionary_type"></a>
|
||||
dictionary_type {#dictionary_type}
|
||||
## 字典类型
|
||||
|
||||
Swift 语言为标准库中定义的 `Dictionary<Key, Value>` 类型提供了如下语法糖:
|
||||
@ -299,11 +299,11 @@ let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39]
|
||||
|
||||
> 字典类型语法
|
||||
>
|
||||
<a name="dictionary-type"></a>
|
||||
dictionary-type {#dictionary-type}
|
||||
> *字典类型* → **[** [*类型*](#type) **:** [*类型*](#type) **]**
|
||||
>
|
||||
|
||||
<a name="optional_type"></a>
|
||||
optional_type {#optional_type}
|
||||
## 可选类型
|
||||
|
||||
Swift 定义后缀 `?` 来作为标准库中的定义的命名型类型 `Optional<Wrapped>` 的语法糖。换句话说,下面两个声明是等价的:
|
||||
@ -332,11 +332,11 @@ optionalInteger! // 42
|
||||
|
||||
> 可选类型语法
|
||||
>
|
||||
<a name="optional-type"></a>
|
||||
optional-type {#optional-type}
|
||||
> *可选类型* → [*类型*](#type) **?**
|
||||
>
|
||||
|
||||
<a name="implicitly_unwrapped_optional_type"></a>
|
||||
implicitly_unwrapped_optional_type {#implicitly_unwrapped_optional_type}
|
||||
## 隐式解析可选类型
|
||||
|
||||
当可以被访问时,Swift 语言定义后缀 `!` 作为标准库中命名类型 `Optional<Wrapped>` 的语法糖,来实现自动解包的功能。换句话说,下面两个声明等价:
|
||||
@ -368,11 +368,11 @@ let implicitlyUnwrappedArray: [Int]! // 正确
|
||||
|
||||
> 隐式解析可选类型语法
|
||||
>
|
||||
<a name="implicitly-unwrapped-optional-type"></a>
|
||||
implicitly-unwrapped-optional-type {#implicitly-unwrapped-optional-type}
|
||||
> *隐式解析可选类型* → [*类型*](#type) **!**
|
||||
>
|
||||
|
||||
<a name="protocol_composition_type"></a>
|
||||
protocol_composition_type {#protocol_composition_type}
|
||||
## 协议合成类型
|
||||
|
||||
协议合成类型是一种符合协议列表中每个指定协议的类型。协议合成类型可能会用在类型注解和泛型参数中。
|
||||
@ -388,17 +388,17 @@ let implicitlyUnwrappedArray: [Int]! // 正确
|
||||
|
||||
> 协议合成类型语法
|
||||
>
|
||||
<a name="protocol-composition-type"></a>
|
||||
protocol-composition-type {#protocol-composition-type}
|
||||
> *协议合成类型* → [*协议标识符*](#protocol-identifier) & [*协议合成延续*](#protocol-composition-continuation)
|
||||
>
|
||||
<a name="protocol-composition-continuation"></a>
|
||||
protocol-composition-continuation {#protocol-composition-continuation}
|
||||
> *协议合成延续* → [*协议标识符*](#protocol-identifier) | [*协议合成类型*](#protocol-composition-type)
|
||||
>
|
||||
<a name="protocol-identifier"></a>
|
||||
protocol-identifier {#protocol-identifier}
|
||||
> *协议标识符* → [*类型标识符*](#type-identifier)
|
||||
>
|
||||
|
||||
<a name="metatype_type"></a>
|
||||
metatype_type {#metatype_type}
|
||||
## 元类型
|
||||
|
||||
元类型是指类型的类型,包括类类型、结构体类型、枚举类型和协议类型。
|
||||
@ -445,11 +445,11 @@ let anotherInstance = metatype.init(string: "some string")
|
||||
|
||||
> 元类型语法
|
||||
>
|
||||
<a name="metatype-type"></a>
|
||||
metatype-type {#metatype-type}
|
||||
> *元类型* → [*类型*](#type) **.** **Type** | [*类型*](#type) **.** **Protocol**
|
||||
>
|
||||
|
||||
<a name="type_inheritance_clause"></a>
|
||||
type_inheritance_clause {#type_inheritance_clause}
|
||||
## 类型继承子句
|
||||
|
||||
类型继承子句被用来指定一个命名型类型继承自哪个类、采纳哪些协议。类型继承子句也用来指定一个类类型专属协议。类型继承子句开始于冒号 `:`,其后是所需要的类、类型标识符列表或两者都有。
|
||||
@ -462,16 +462,16 @@ let anotherInstance = metatype.init(string: "some string")
|
||||
|
||||
> 类型继承子句语法
|
||||
>
|
||||
<a name="type_inheritance_clause"></a>
|
||||
type_inheritance_clause {#type_inheritance_clause}
|
||||
> *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list)
|
||||
>
|
||||
<a name="type-inheritance-list"></a>
|
||||
type-inheritance-list {#type-inheritance-list}
|
||||
> *类型继承列表* → [*类型标识符*](#type-identifier) | [*类型标识符*](#type-identifier) **,** [*类型继承列表*](#type-inheritance-list)
|
||||
>
|
||||
<a name="class-requirement"></a>
|
||||
class-requirement {#class-requirement}
|
||||
|
||||
|
||||
<a name="type_inference"></a>
|
||||
type_inference {#type_inference}
|
||||
## 类型推断
|
||||
|
||||
Swift 广泛使用类型推断,从而允许你省略代码中很多变量和表达式的类型或部分类型。比如,对于 `var x: Int = 0`,你可以完全省略类型而简写成 `var x = 0`,编译器会正确推断出 `x` 的类型 `Int`。类似的,当完整的类型可以从上下文推断出来时,你也可以省略类型的一部分。比如,如果你写了 `let dict: Dictionary = ["A" : 1]`,编译器能推断出 `dict` 的类型是 `Dictionary<String, Int>`。
|
||||
|
||||
@ -6,16 +6,14 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
|
||||
> 表达式语法
|
||||
>
|
||||
<a name="expression"></a>
|
||||
expression {#expression}
|
||||
> *表达式* → [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
|
||||
>
|
||||
<a name="expression-list"></a>
|
||||
expression-list {#expression-list}
|
||||
> *表达式列表* → [*表达式*](#expression) | [*表达式*](#expression) **,** [*表达式列表*](#expression-list)
|
||||
>
|
||||
|
||||
<a name="prefix_expressions"></a>
|
||||
## 前缀表达式
|
||||
|
||||
## 前缀表达式 {#prefix_expressions}
|
||||
前缀表达式由可选的前缀运算符和表达式组成。前缀运算符只接收一个参数,表达式则紧随其后。
|
||||
|
||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/26_Advanced_Operators.md)。
|
||||
@ -26,18 +24,16 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
|
||||
|
||||
> 前缀表达式语法
|
||||
>
|
||||
<a name="prefix-expression"></a>
|
||||
prefix-expression {#prefix-expression}
|
||||
> *前缀表达式* → [*前缀运算符*](./02_Lexical_Structure.md#prefix-operator)<sub>可选</sub> [*后缀表达式*](#postfix-expression)
|
||||
>
|
||||
> *前缀表达式* → [*输入输出表达式*](#in-out-expression)
|
||||
>
|
||||
<a name="in-out-expression"></a>
|
||||
in-out-expression {#in-out-expression}
|
||||
> *输入输出表达式* → **&** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
<a name="try_operator"></a>
|
||||
### Try 运算符
|
||||
|
||||
### Try 运算符 {#try_operator}
|
||||
try 表达式由 `try` 运算符加上紧随其后的可抛出错误的表达式组成,形式如下:
|
||||
|
||||
> try `可抛出错误的表达式`
|
||||
@ -70,13 +66,11 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
关于 `try`、`try?` 和 `try!` 的更多信息,以及该如何使用的例子,请参阅 [错误处理](../chapter2/17_Error_Handling.md)。
|
||||
> Try 表达式语法
|
||||
>
|
||||
<a name="try-operator"></a>
|
||||
try-operator {#try-operator}
|
||||
> *try 运算符* → **try** | **try?** | **try!**
|
||||
>
|
||||
|
||||
<a name="binary_expressions"></a>
|
||||
## 二元表达式
|
||||
|
||||
## 二元表达式 {#binary_expressions}
|
||||
*二元表达式*由中缀运算符和左右参数表达式组成。形式如下:
|
||||
|
||||
> `左侧参数` `二元运算符` `右侧参数`
|
||||
@ -91,7 +85,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
> 在解析时,一个二元表达式将作为一个扁平列表表示,然后根据运算符的优先级,再进一步进行组合。例如,`2 + 3 * 5` 首先被看作具有五个元素的列表,即 `2`、`+`、`3`、`*`、`5`,随后根据运算符优先级组合为 `(2 + (3 * 5))`。
|
||||
>
|
||||
|
||||
<a name="binary-expression"></a>
|
||||
binary-expression {#binary-expression}
|
||||
> 二元表达式语法
|
||||
>
|
||||
> *二元表达式* → [*二元运算符*](./02_Lexical_Structure.md#binary-operator) [*前缀表达式*](#prefix-expression)
|
||||
@ -102,13 +96,11 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
>
|
||||
> *二元表达式* → [*类型转换运算符*](#type-casting-operator)
|
||||
>
|
||||
<a name="binary-expressions"></a>
|
||||
binary-expressions {#binary-expressions}
|
||||
> *二元表达式列表* → [*二元表达式*](#binary-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a name="assignment_operator"></a>
|
||||
### 赋值表达式
|
||||
|
||||
### 赋值表达式 {#assignment_operator}
|
||||
赋值表达式会为某个给定的表达式赋值,形式如下;
|
||||
|
||||
> `表达式` = `值`
|
||||
@ -125,13 +117,11 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
|
||||
> 赋值运算符语法
|
||||
>
|
||||
<a name="assignment-operator"></a>
|
||||
assignment-operator {#assignment-operator}
|
||||
> *赋值运算符* → **=**
|
||||
>
|
||||
|
||||
<a name="ternary_conditional_operator"></a>
|
||||
### 三元条件运算符
|
||||
|
||||
### 三元条件运算符 {#ternary_conditional_operator}
|
||||
*三元条件运算符*会根据条件来对两个给定表达式中的一个进行求值,形式如下:
|
||||
|
||||
> `条件` ? `表达式(条件为真则使用)` : `表达式(条件为假则使用)`
|
||||
@ -143,13 +133,11 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误:try
|
||||
|
||||
> 三元条件运算符语法
|
||||
>
|
||||
<a name="conditional-operator"></a>
|
||||
conditional-operator {#conditional-operator}
|
||||
> *三元条件运算符* → **?** [*表达式*](#expression) **:**
|
||||
>
|
||||
|
||||
<a name="type-casting_operators"></a>
|
||||
### 类型转换运算符
|
||||
|
||||
### 类型转换运算符 {#type-casting_operators}
|
||||
有 4 种类型转换运算符:`is`、`as`、`as? ` 和 `as!`。它们有如下的形式:
|
||||
|
||||
> `表达式` is `类型`
|
||||
@ -188,7 +176,7 @@ f(x as Any)
|
||||
|
||||
关于类型转换的更多内容和例子,请参阅 [类型转换](../chapter2/18_Type_Casting.md)。
|
||||
|
||||
<a name="type-casting-operator"></a>
|
||||
type-casting-operator {#type-casting-operator}
|
||||
> 类型转换运算符语法
|
||||
>
|
||||
> *类型转换运算符* → **is** [*类型*](./03_Types.md#type)
|
||||
@ -200,14 +188,12 @@ f(x as Any)
|
||||
> *类型转换运算符* → **as** **!** [*类型*](./03_Types.md#type)
|
||||
>
|
||||
|
||||
<a name="primary_expressions"></a>
|
||||
## 基本表达式
|
||||
|
||||
## 基本表达式 {#primary_expressions}
|
||||
*基本表达式*是最基本的表达式。它们可以单独使用,也可以跟前缀表达式、二元表达式、后缀表达式组合使用。
|
||||
|
||||
> 基本表达式语法
|
||||
>
|
||||
<a name="primary-expression"></a>
|
||||
primary-expression {#primary-expression}
|
||||
> *基本表达式* → [*标识符*](./02_Lexical_Structure.md#identifier) [*泛型实参子句*](./09_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub>
|
||||
>
|
||||
> *基本表达式* → [*字面量表达式*](#literal-expression)
|
||||
@ -229,9 +215,7 @@ f(x as Any)
|
||||
> *基本表达式* → [*key-path字符串表达式*](#key-patch-string-expression)
|
||||
>
|
||||
|
||||
<a name="literal_expression"></a>
|
||||
### 字面量表达式
|
||||
|
||||
### 字面量表达式 {#literal_expression}
|
||||
*字面量表达式*可由普通字面量(例如字符串或者数字),字典或者数组字面量,或者下面列表中的特殊字面量组成:
|
||||
|
||||
字面量 | 类型 | 值
|
||||
@ -285,7 +269,7 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者
|
||||
> 字面量表达式语法
|
||||
>
|
||||
>
|
||||
> <a name="literal-expression"></a>
|
||||
> literal-expression {#literal-expression}
|
||||
>
|
||||
> *字面量表达式* → [*字面量*](./02_Lexical_Structure.md#literal)
|
||||
>
|
||||
@ -295,38 +279,38 @@ Xcode 使用 playground 字面量对程序编辑器中的颜色、文件或者
|
||||
>
|
||||
|
||||
|
||||
> <a name="array-literal"></a>
|
||||
> array-literal {#array-literal}
|
||||
>
|
||||
> *数组字面量* → [[*数组字面量项列表*](#array-literal-items)<sub>可选</sub> **]**
|
||||
>
|
||||
> <a name="array-literal-items"></a>
|
||||
> array-literal-items {#array-literal-items}
|
||||
>
|
||||
> *数组字面量项列表* → [*数组字面量项*](#array-literal-item) **,**<sub>可选</sub> | [*数组字面量项*](#array-literal-item) **,** [*数组字面量项列表*](#array-literal-items)
|
||||
>
|
||||
> <a name="array-literal-item"></a>
|
||||
> array-literal-item {#array-literal-item}
|
||||
>
|
||||
> *数组字面量项* → [*表达式*](#expression)
|
||||
>
|
||||
>
|
||||
> <a name="dictionary-literal"></a>
|
||||
> dictionary-literal {#dictionary-literal}
|
||||
>
|
||||
> *字典字面量* → [[*字典字面量项列表*](#dictionary-literal-items) **]** | **[** **:** **]**
|
||||
>
|
||||
> <a name="dictionary-literal-items"></a>
|
||||
> dictionary-literal-items {#dictionary-literal-items}
|
||||
>
|
||||
> *字典字面量项列表* → [*字典字面量项*](#dictionary-literal-item) **,**<sub>可选</sub> | [*字典字面量项*](#dictionary-literal-item) **,** [*字典字面量项列表*](#dictionary-literal-items)
|
||||
>
|
||||
> <a name="dictionary-literal-item"></a>
|
||||
> dictionary-literal-item {#dictionary-literal-item}
|
||||
>
|
||||
> *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)。
|
||||
>
|
||||
> <a name="playground-literal"></a>
|
||||
> playground-literal {#playground-literal}
|
||||
>
|
||||
> *playground 字面量* → **#colorLiteral ( red : [*表达式*](#expression) , green :[*表达式*](#expression) [*表达式*](#e[*表达式*](#expression) xpression) , blue :[*表达式*](#expression) , alpha : [*表达式*](#expression) )**
|
||||
>
|
||||
> *playground 字面量* → **#fileLiteral ( resourceName : [*表达式*](#expression) )**
|
||||
>
|
||||
> playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**<a name="self_expression"></a>
|
||||
> playground 字面量* → **#imageLiteral ( resourceName : [*表达式*](#expression) )**self_expression {#self_expression}
|
||||
>
|
||||
|
||||
### Self 表达式
|
||||
@ -370,23 +354,21 @@ struct Point {
|
||||
|
||||
> Self 表达式语法
|
||||
>
|
||||
<a name="self-expression"></a>
|
||||
self-expression {#self-expression}
|
||||
> *self 表达式* → **self** | [*self 方法表达式*](#self-method-expression) | [*self 下标表达式*](#self-subscript-expression) | [*self 构造器表达式*](#self-initializer-expression)
|
||||
>
|
||||
>
|
||||
<a name="self-method-expression"></a>
|
||||
self-method-expression {#self-method-expression}
|
||||
> *self 方法表达式* → **self** **.** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
<a name="self-subscript-expression"></a>
|
||||
self-subscript-expression {#self-subscript-expression}
|
||||
> *self 下标表达式* → **self** **[** [*函数调用参数表*](#function-call-argument-list) **]**
|
||||
>
|
||||
<a name="self-initializer-expression"></a>
|
||||
self-initializer-expression {#self-initializer-expression}
|
||||
> *self 构造器表达式* → **self** **.** **init**
|
||||
>
|
||||
|
||||
<a name="superclass_expression"></a>
|
||||
### 父类表达式
|
||||
|
||||
### 父类表达式 {#superclass_expression}
|
||||
*父类*表达式可以使我们在某个类中访问它的父类,它有如下形式:
|
||||
|
||||
> super.`成员名称`
|
||||
@ -402,22 +384,20 @@ struct Point {
|
||||
|
||||
> 父类表达式语法
|
||||
>
|
||||
<a name="superclass-expression"></a>
|
||||
superclass-expression {#superclass-expression}
|
||||
> *父类表达式* → [*父类方法表达式*](#superclass-method-expression) | [*父类下标表达式*](#superclass-subscript-expression) | [*父类构造器表达式*](#superclass-initializer-expression)
|
||||
>
|
||||
<a name="superclass-method-expression"></a>
|
||||
superclass-method-expression {#superclass-method-expression}
|
||||
> *父类方法表达式* → **super** **.** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
<a name="superclass-subscript-expression"></a>
|
||||
superclass-subscript-expression {#superclass-subscript-expression}
|
||||
> *父类下标表达式* → **super** [[*函数调用参数表*](#function-call-argument-list) **]**
|
||||
>
|
||||
<a name="superclass-initializer-expression"></a>
|
||||
superclass-initializer-expression {#superclass-initializer-expression}
|
||||
> *父类构造器表达式* → **super** **.** **init**
|
||||
>
|
||||
|
||||
<a name="closure_expression"></a>
|
||||
### 闭包表达式
|
||||
|
||||
### 闭包表达式 {#closure_expression}
|
||||
*闭包表达式*会创建一个闭包,在其他语言中也叫 *lambda* 或*匿名*函数。跟函数一样,闭包包含了待执行的代码,不同的是闭包还会捕获所在环境中的常量和变量。它的形式如下:
|
||||
|
||||
```swift
|
||||
@ -460,10 +440,7 @@ myFunction { $0 + $1 }
|
||||
|
||||
关于逃逸闭包的内容,请参阅[逃逸闭包](./chapter2/07_Closures.md#escaping_closures)
|
||||
|
||||
<a name="capture-lists"></a>
|
||||
|
||||
## 捕获列表
|
||||
|
||||
## 捕获列表 {#capture-lists}
|
||||
默认情况下,闭包会捕获附近作用域中的常量和变量,并使用强引用指向它们。你可以通过一个*捕获列表*来显式指定它的捕获行为。
|
||||
|
||||
捕获列表在参数列表之前,由中括号括起来,里面是由逗号分隔的一系列表达式。一旦使用了捕获列表,就必须使用 `in` 关键字,即使省略了参数名、参数类型和返回类型。
|
||||
@ -524,12 +501,12 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
|
||||
> 闭包表达式语法
|
||||
>
|
||||
>
|
||||
> <a name="closure-expression"></a>
|
||||
> closure-expression {#closure-expression}
|
||||
>
|
||||
> *闭包表达式* → **{** [*闭包签名*](#closure-signature)<sub>可选</sub> [*语句*](#statements) **}**
|
||||
>
|
||||
>
|
||||
> <a name="closure-signature"></a>
|
||||
> closure-signature {#closure-signature}
|
||||
>
|
||||
>
|
||||
> 闭包签名* → [*参数子句*](#parameter-clause) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
|
||||
@ -543,27 +520,25 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
|
||||
> *闭包签名* → [*捕获列表*](#capture-list) **in**
|
||||
>
|
||||
>
|
||||
> <a name="capture-list"></a>
|
||||
> capture-list {#capture-list}
|
||||
>
|
||||
>
|
||||
> 捕获列表* → [ [*捕获列表项列表*](#capture-list-items) **]**
|
||||
>
|
||||
> <a name="capture-list-items"></a>
|
||||
> capture-list-items {#capture-list-items}
|
||||
>
|
||||
> *捕获列表项列表* → [*捕获列表项*](#capture-list-item) | [*捕获列表项*](#capture-list-item) **,** [*捕获列表项列表*](#capture-list-items)
|
||||
>
|
||||
> <a name="capture-list-item"></a>
|
||||
> capture-list-item {#capture-list-item}
|
||||
>
|
||||
> *捕获列表项* → [*捕获说明符*](#capture-specifier)<sub>可选</sub> [*表达式*](#expression)
|
||||
>
|
||||
> <a name="capture-specifier"></a>
|
||||
> capture-specifier {#capture-specifier}
|
||||
>
|
||||
> *捕获说明符* → **weak** | **unowned** | **unowned(safe)** | **unowned(unsafe)**
|
||||
>
|
||||
|
||||
<a name="implicit_member_expression"></a>
|
||||
### 隐式成员表达式
|
||||
|
||||
### 隐式成员表达式 {#implicit_member_expression}
|
||||
若类型可被推断出来,可以使用*隐式成员表达式*来访问某个类型的成员(例如某个枚举成员或某个类型方法),形式如下:
|
||||
|
||||
> .`成员名称`
|
||||
@ -578,24 +553,20 @@ x = .AnotherValue
|
||||
|
||||
> 隐式成员表达式语法
|
||||
>
|
||||
<a name="implicit-member-expression"></a>
|
||||
implicit-member-expression {#implicit-member-expression}
|
||||
> *隐式成员表达式* → **.** [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
<a name="parenthesized_expression"></a>
|
||||
### 圆括号表达式
|
||||
|
||||
### 圆括号表达式 {#parenthesized_expression}
|
||||
*圆括号表达式*是由圆括号包围的表达式。你可以用圆括号说明成组的表达式的先后操作。成组的圆括号不会改变表达式的类型 - 例如 `(1)` 的类型就是简单的 `Int`。
|
||||
|
||||
> 圆括号表达式语法
|
||||
>
|
||||
<a name="parenthesized-expression"></a>
|
||||
parenthesized-expression {#parenthesized-expression}
|
||||
> *圆括号表达式* → **( [*表达式*](#expression) )**
|
||||
>
|
||||
|
||||
<a name="Tuple_Expression"></a>
|
||||
### 元组表达式
|
||||
|
||||
### 元组表达式 {#Tuple_Expression}
|
||||
*元组表达式*由圆括号和其中多个逗号分隔的子表达式组成。每个子表达式前面可以有一个标识符,用冒号隔开。元组表达式形式如下:
|
||||
|
||||
> (`标识符 1` : `表达式 1`, `标识符 2` : `表达式 2`, `...`)
|
||||
@ -612,20 +583,17 @@ x = .AnotherValue
|
||||
|
||||
> 元组表达式语法
|
||||
>
|
||||
<a name="tuple-expression"></a>
|
||||
tuple-expression {#tuple-expression}
|
||||
> *元组表达式* → **( )** | **(**[*元组元素*](#tuple-element), [*元组元素列表*](#tuple-element-list) **)**
|
||||
>
|
||||
<a name="tuple-element-list"></a>
|
||||
tuple-element-list {#tuple-element-list}
|
||||
> *元组元素列表* → [*元组元素*](#tuple-element) | [*元组元素*](#tuple-element) **,** [*元组元素列表*](#tuple-element-list)
|
||||
>
|
||||
<a name="tuple-element"></a>
|
||||
tuple-element {#tuple-element}
|
||||
> *元组元素* → [*表达式*](#expression) | [*标识符*](identifier) **:** [*表达式*](#expression)
|
||||
>
|
||||
|
||||
<a name="wildcard_expression"></a>
|
||||
|
||||
### 通配符表达式
|
||||
|
||||
### 通配符表达式 {#wildcard_expression}
|
||||
*通配符表达式*可以在赋值过程中显式忽略某个值。例如下面的代码中,`10` 被赋值给 `x`,而 `20` 则被忽略:
|
||||
|
||||
```swift
|
||||
@ -635,14 +603,12 @@ x = .AnotherValue
|
||||
|
||||
> 通配符表达式语法
|
||||
>
|
||||
<a name="wildcard-expression"></a>
|
||||
wildcard-expression {#wildcard-expression}
|
||||
> *通配符表达式* → **_**
|
||||
>
|
||||
|
||||
|
||||
<a name="key-path_expression"></a>
|
||||
### Key-path 表达式
|
||||
|
||||
### Key-path 表达式 {#key-path_expression}
|
||||
Key-path 表达式引用一个类型的属性或下标。在动态语言中使场景可以使用 Key-path 表达式,例如观察键值对。格式为:
|
||||
|
||||
> **\类型名.路径**
|
||||
@ -774,31 +740,28 @@ print(interestingNumbers[keyPath: \[String: [Int]].["hexagonal"]!.count.bitWidth
|
||||
> key-path 表达式语法
|
||||
>
|
||||
>
|
||||
> <a name="key-path-expression"></a>
|
||||
> key-path-expression {#key-path-expression}
|
||||
>
|
||||
> *key-path 表达式* → **\\** [类型](./03_Types.md#type)<sub>可选</sub> **.** [多个 key-path 组件](#key-path-components)
|
||||
>
|
||||
> <a name="key-path-components"></a>
|
||||
> key-path-components {#key-path-components}
|
||||
>
|
||||
> *多个 key-path 组件* → [key-path 组件](#key-path-component) | [key-path 组件](#key-path-component) **.** [多个 key-path 组件](#key-path-components)
|
||||
>
|
||||
> <a name="key-path-component"></a>
|
||||
> key-path-component {#key-path-component}
|
||||
>
|
||||
> *key-path 组件* → [标识符](./02_Lexical_Structure.md#identifier) [多个 key-path 后缀](#key-path-postfixes)<sub>可选<sub> | [多个 key-path 后缀](#key-path-postfixes)
|
||||
>
|
||||
> <a name="key-path-postfixes"></a>
|
||||
> key-path-postfixes {#key-path-postfixes}
|
||||
>
|
||||
> *多个 key-path 后缀* → [key-path 后缀](#key-path-postfix) [多个 key-path 后缀](#key-path-postfixes)<sub>可选<sub> <a name="key-path-postfixes"></a>
|
||||
> *多个 key-path 后缀* → [key-path 后缀](#key-path-postfix) [多个 key-path 后缀](#key-path-postfixes)<sub>可选<sub> key-path-postfixes {#key-path-postfixes}
|
||||
>
|
||||
> *key-path 后缀* → **?** | **!** | **self** | **\[** [函数调用参数表](#function-call-argument-list) **\]**
|
||||
>
|
||||
|
||||
|
||||
|
||||
<a name="selector_expression"></a>
|
||||
|
||||
### 选择器表达式
|
||||
|
||||
### 选择器表达式 {#selector_expression}
|
||||
*选择器表达式*可以让你通过选择器来引用在 Objective-C 中方法(method)和属性(property)的 setter 和 getter 方法。
|
||||
|
||||
> \#selector(方法名)
|
||||
@ -846,7 +809,7 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
|
||||
|
||||
> 选择器表达式语法
|
||||
>
|
||||
<a name="selector-expression"></a>
|
||||
selector-expression {#selector-expression}
|
||||
> *选择器表达式* → __#selector__ **(** [*表达式*](#expression) **)**
|
||||
>
|
||||
> *选择器表达式* → __#selector__ **(** [*getter:表达式*](#expression) **)**
|
||||
@ -854,10 +817,7 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
|
||||
> *选择器表达式* → __#selector__ **(** [*setter:表达式*](#expression) **)**
|
||||
>
|
||||
|
||||
<a name="key-path_string_expressions"></a>
|
||||
|
||||
## Key-path 字符串表达式
|
||||
|
||||
## Key-path 字符串表达式 {#key-path_string_expressions}
|
||||
key-path 字符串表达式可以访问一个引用 Objective-C 属性的字符串,通常在 key-value 编程和 key-value 观察 APIs 中使用。其格式如下:
|
||||
|
||||
> `#keyPath` ( `属性名` )
|
||||
@ -907,15 +867,12 @@ print(keyPath == c.getSomeKeyPath())
|
||||
|
||||
> key-path 字符串表达式语法
|
||||
>
|
||||
> <a name="key-path-string-expression"></a>
|
||||
> key-path-string-expression {#key-path-string-expression}
|
||||
>
|
||||
> *key-path 字符串表达式* → **#keyPath (** [表达式](#expression) **)**
|
||||
>
|
||||
|
||||
<a name="postfix_expressions"></a>
|
||||
|
||||
## 后缀表达式
|
||||
|
||||
## 后缀表达式 {#postfix_expressions}
|
||||
*后缀表达式*就是在某个表达式的后面运用后缀运算符或其他后缀语法。从语法构成上来看,基本表达式也是后缀表达式。
|
||||
|
||||
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/26_Advanced_Operators.md)。
|
||||
@ -924,7 +881,7 @@ print(keyPath == c.getSomeKeyPath())
|
||||
|
||||
> 后缀表达式语法
|
||||
>
|
||||
<a name="postfix-expression"></a>
|
||||
postfix-expression {#postfix-expression}
|
||||
> *后缀表达式* → [*基本表达式*](#primary-expression)
|
||||
>
|
||||
> *后缀表达式* → [*后缀表达式*](#postfix-expression) [*后缀运算符*](02_Lexical_Structure.md#postfix-operator)
|
||||
@ -946,9 +903,7 @@ print(keyPath == c.getSomeKeyPath())
|
||||
> *后缀表达式* → [*可选链表达式*](#optional-chaining-expression)
|
||||
>
|
||||
|
||||
<a name="function_call_expression"></a>
|
||||
### 函数调用表达式
|
||||
|
||||
### 函数调用表达式 {#function_call_expression}
|
||||
*函数调用表达式*由函数名和参数列表组成,形式如下:
|
||||
|
||||
> `函数名`(`参数 1`, `参数 2`)
|
||||
@ -979,36 +934,34 @@ myData.someMethod {$0 == 13}
|
||||
|
||||
> 函数调用表达式语法
|
||||
>
|
||||
> <a name="function-call-expression"></a>
|
||||
> function-call-expression {#function-call-expression}
|
||||
>
|
||||
> *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause)
|
||||
>
|
||||
> *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*函数调用参数子句*](#function-call-argument-clause)<sub>可选</sub> [*尾随闭包*](#trailing-closure)
|
||||
>
|
||||
>
|
||||
> <a name="function-call-argument-clause"></a>
|
||||
> function-call-argument-clause {#function-call-argument-clause}
|
||||
>
|
||||
> *函数调用参数子句* → **(** **)** | **(** [*函数调用参数表*](#function-call-argument-list) **)**
|
||||
>
|
||||
> <a name="function-call-argument-list"></a>
|
||||
> function-call-argument-list {#function-call-argument-list}
|
||||
>
|
||||
> *函数调用参数表* → [函数调用参数](#function-call-argument) | [函数调用参数](#function-call-argument) **,** [*函数调用参数表*](#function-call-argument-list)
|
||||
>
|
||||
> <a name="function-call-argument"></a>
|
||||
> function-call-argument {#function-call-argument}
|
||||
>
|
||||
> *函数调用参数* → [表达式](#expression) | [标识符](02_Lexical_Structure.md#identifier) **:** [*表达式*](#expression)
|
||||
>
|
||||
> *函数调用参数* → [运算符](./02_Lexical_Structure.md#operator) | [标识符](./02_Lexical_Structure.md#identifier) **:** [*运算符*](./02_Lexical_Structure.md#operator)
|
||||
>
|
||||
>
|
||||
> <a name="trailing-closure"></a>
|
||||
> trailing-closure {#trailing-closure}
|
||||
>
|
||||
> *尾随闭包* → [*闭包表达式*](#closure-expression)
|
||||
>
|
||||
|
||||
<a name="initializer_expression"></a>
|
||||
### 构造器表达式
|
||||
|
||||
### 构造器表达式 {#initializer_expression}
|
||||
*构造器表达式*用于访问某个类型的构造器,形式如下:
|
||||
|
||||
> `表达式`.init(`构造器参数`)
|
||||
@ -1048,15 +1001,13 @@ let s3 = someValue.dynamicType.init(data: 7) // 有效
|
||||
|
||||
> 构造器表达式语法
|
||||
>
|
||||
<a name="initializer-expression"></a>
|
||||
initializer-expression {#initializer-expression}
|
||||
> *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init**
|
||||
>
|
||||
> *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** **(** [*参数名称*](#argument-names) **)**
|
||||
>
|
||||
|
||||
<a name="explicit_member_expression"></a>
|
||||
### 显式成员表达式
|
||||
|
||||
### 显式成员表达式 {#explicit_member_expression}
|
||||
*显式成员表达式*允许我们访问命名类型、元组或者模块的成员,其形式如下:
|
||||
|
||||
> `表达式`.`成员名`
|
||||
@ -1116,23 +1067,21 @@ let x = [10, 3, 20, 15, 4]
|
||||
|
||||
> 显式成员表达式语法
|
||||
>
|
||||
<a name="explicit-member-expression"></a>
|
||||
explicit-member-expression {#explicit-member-expression}
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*十进制数字*] (02_Lexical_Structure.md#decimal-digit)
|
||||
>
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02_Lexical_Structure.md#identifier) [*泛型实参子句*](./09_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub><br/>
|
||||
>
|
||||
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.md#identifier) **(** [*参数名称*](#argument-names) **)**
|
||||
>
|
||||
<a name="argument-names"></a>
|
||||
argument-names {#argument-names}
|
||||
> *参数名称* → [*参数名*](#argument-name) [*参数名称*](#argument-names)<sub>可选</sub><br/>
|
||||
>
|
||||
<a name="argument-name"></a>
|
||||
argument-name {#argument-name}
|
||||
> *参数名* → [*标识符*](./02_Lexical_Structure.md#identifier) **:**
|
||||
>
|
||||
|
||||
<a name="postfix_self_expression"></a>
|
||||
### 后缀 self 表达式
|
||||
|
||||
### 后缀 self 表达式 {#postfix_self_expression}
|
||||
后缀 `self` 表达式由某个表达式或类型名紧跟 `.self` 组成,其形式如下:
|
||||
|
||||
> `表达式`.self
|
||||
@ -1146,14 +1095,12 @@ let x = [10, 3, 20, 15, 4]
|
||||
|
||||
> 后缀 self 表达式语法
|
||||
>
|
||||
<a name="postfix-self-expression"></a>
|
||||
postfix-self-expression {#postfix-self-expression}
|
||||
> *后缀 self 表达式* → [*后缀表达式*](#postfix-expression) **.** **self**
|
||||
>
|
||||
|
||||
|
||||
<a name="subscript_expression"></a>
|
||||
### 下标表达式
|
||||
|
||||
### 下标表达式 {#subscript_expression}
|
||||
可通过*下标表达式*访问相应的下标,形式如下:
|
||||
|
||||
> `表达式`[`索引表达式`]
|
||||
@ -1165,13 +1112,11 @@ let x = [10, 3, 20, 15, 4]
|
||||
|
||||
> 下标表达式语法
|
||||
>
|
||||
<a name="subscript-expression"></a>
|
||||
subscript-expression {#subscript-expression}
|
||||
> *下标表达式* → [*后缀表达式*](#postfix-expression) **[** [*表达式列表*](#expression-list) **]**
|
||||
>
|
||||
|
||||
<a name="forced-Value_expression"></a>
|
||||
### 强制取值表达式
|
||||
|
||||
### 强制取值表达式 {#forced-Value_expression}
|
||||
当你确定可选值不是 `nil` 时,可以使用*强制取值表达式*来强制解包,形式如下:
|
||||
|
||||
> `表达式`!
|
||||
@ -1193,13 +1138,11 @@ someDictionary["a"]![0] = 100
|
||||
|
||||
> 强制取值语法
|
||||
>
|
||||
<a name="forced-value-expression"></a>
|
||||
forced-value-expression {#forced-value-expression}
|
||||
> *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!**
|
||||
>
|
||||
|
||||
<a name="optional-chaining_expression"></a>
|
||||
### 可选链表达式
|
||||
|
||||
### 可选链表达式 {#optional-chaining_expression}
|
||||
*可选链表达式*提供了一种使用可选值的便捷方法,形式如下:
|
||||
|
||||
> `表达式`?
|
||||
@ -1247,6 +1190,6 @@ someDictionary["a"]?[0] = someFunctionWithSideEffects()
|
||||
|
||||
> 可选链表达式语法
|
||||
>
|
||||
<a name="optional-chaining-expression"></a>
|
||||
optional-chaining-expression {#optional-chaining-expression}
|
||||
> *可选链表达式* → [*后缀表达式*](#postfix-expression) **?**
|
||||
>
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
<a name="statement_statements"></a>
|
||||
# 语句(Statements)
|
||||
|
||||
# 语句(Statements) {#statement_statements}
|
||||
在 Swift 中,有三种类型的语句:简单语句、编译器控制语句和控制流语句。简单语句是最常见的,用于构造表达式或者声明。编译器控制语句允许程序改变编译器的行为,包含编译配置语句和行控制语句。
|
||||
|
||||
控制流语句则用于控制程序执行的流程,Swift 中有多种类型的控制流语句:循环语句、分支语句和控制转移语句。循环语句用于重复执行代码块;分支语句用于执行满足特定条件的代码块;控制转移语句则用于改变代码的执行顺序。另外,Swift 提供了 `do` 语句,用于构建局部作用域,还用于错误的捕获和处理;还提供了 `defer` 语句,用于退出当前作用域之前执行清理操作。
|
||||
@ -9,7 +7,7 @@
|
||||
|
||||
> 语句语法
|
||||
>
|
||||
<a name="statement"></a>
|
||||
statement {#statement}
|
||||
> *语句* → [*表达式*](./04_Expressions.md#expression) **;**<sub>可选</sub>
|
||||
>
|
||||
> *语句* → [*声明*](./06_Declarations.md#declaration) **;**<sub>可选</sub>
|
||||
@ -28,13 +26,11 @@
|
||||
>
|
||||
> *语句* → [*编译器控制语句*](#compiler-control-statement)
|
||||
>
|
||||
<a name="statements"></a>
|
||||
statements {#statements}
|
||||
> *多条语句* → [*语句*](#statement) [*多条语句*](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a name="loop_statements"></a>
|
||||
## 循环语句
|
||||
|
||||
## 循环语句 {#loop_statements}
|
||||
循环语句会根据特定的循环条件来重复执行代码块。Swift 提供三种类型的循环语句:`for-in` 语句、`while` 语句和 `repeat-while` 语句。
|
||||
|
||||
通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。
|
||||
@ -42,7 +38,7 @@
|
||||
> 循环语句语法
|
||||
>
|
||||
>
|
||||
<a name="loop-statement"></a>
|
||||
loop-statement {#loop-statement}
|
||||
> *循环语句* → [*for-in 语句*](#for-in-statement)
|
||||
>
|
||||
> *循环语句* → [*while 语句*](#while-statement)
|
||||
@ -50,9 +46,7 @@
|
||||
> *循环语句* → [*repeat-while 语句*](#repeat-while-statement)
|
||||
>
|
||||
|
||||
<a name="for-in_statements"></a>
|
||||
### For-In 语句
|
||||
|
||||
### For-In 语句 {#for-in_statements}
|
||||
`for-in` 语句会为集合(或实现了 `SequenceType` 协议的任意类型)中的每一项执行一次代码块。
|
||||
|
||||
`for-in` 语句的形式如下:
|
||||
@ -68,13 +62,11 @@ for item in collection {
|
||||
> for-in 语句语法
|
||||
>
|
||||
>
|
||||
<a name="for-in-statement"></a>
|
||||
for-in-statement {#for-in-statement}
|
||||
> *for-in 语句* → **for** **case**<sub>可选</sub> [*模式*](./08_Patterns.md#pattern) **in** [*表达式*](./04_Expressions.md#expression) [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
<a name="while_statements"></a>
|
||||
### While 语句
|
||||
|
||||
### While 语句 {#while_statements}
|
||||
只要循环条件为真,`while` 语句就会重复执行代码块。
|
||||
|
||||
`while` 语句的形式如下:
|
||||
@ -97,27 +89,25 @@ while condition {
|
||||
> while 语句语法
|
||||
>
|
||||
>
|
||||
<a name="while-statement"></a>
|
||||
while-statement {#while-statement}
|
||||
> *while 语句* → **while** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
<a name="condition-clause"></a>
|
||||
condition-clause {#condition-clause}
|
||||
> *条件子句* → [*表达式*](./04_Expressions.md#expression) | [*表达式*](./04_Expressions.md#expression) **,** [*条件列表*](#condition-list)
|
||||
>
|
||||
<a name="condition"></a>
|
||||
condition {#condition}
|
||||
> *条件* → [*表达式*](./04_Expressions.md#expression) |[*可用性条件*](#availability-condition) | [*case 条件*](#case-condition) | [*可选绑定条件*](#optional-binding-condition)
|
||||
>
|
||||
>
|
||||
<a name="case-condition"></a>
|
||||
case-condition {#case-condition}
|
||||
> *case 条件* → **case** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer)
|
||||
>
|
||||
<a name="optional-binding-condition"></a>
|
||||
optional-binding-condition {#optional-binding-condition}
|
||||
> *可选绑定条件* → **let** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer) | **var** [*模式*](./08_Patterns.md#pattern) [*构造器*](./06_Declarations.md#initializer)
|
||||
>
|
||||
|
||||
<a name="repeat-while_statements"></a>
|
||||
### Repeat-While 语句
|
||||
|
||||
### Repeat-While 语句 {#repeat-while_statements}
|
||||
`repeat-while` 语句至少执行一次代码块,之后只要循环条件为真,就会重复执行代码块。
|
||||
|
||||
`repeat-while` 语句的形式如下:
|
||||
@ -140,13 +130,11 @@ repeat {
|
||||
> repeat-while 语句语法
|
||||
>
|
||||
>
|
||||
<a name="repeat-while-statement"></a>
|
||||
repeat-while-statement {#repeat-while-statement}
|
||||
> *repeat-while 语句* → **repeat** [*代码块*](./06_Declarations.md#code-block) **while** [*表达式*](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
<a name="branch_statements"></a>
|
||||
## 分支语句
|
||||
|
||||
## 分支语句 {#branch_statements}
|
||||
分支语句会根据一个或者多个条件来执行指定部分的代码。分支语句中的条件将会决定程序如何分支以及执行哪部分代码。Swift 提供三种类型的分支语句:`if` 语句、 `guard` 语句和 `switch` 语句。
|
||||
|
||||
`if` 语句和 `switch` 语句中的控制流可以用 `break` 语句改变,请参阅 [Break 语句](#break_statement)。
|
||||
@ -154,7 +142,7 @@ repeat {
|
||||
> 分支语句语法
|
||||
>
|
||||
>
|
||||
<a name="branch-statement"></a>
|
||||
branch-statement {#branch-statement}
|
||||
> *分支语句* → [*if 语句*](#if-statement)
|
||||
>
|
||||
> *分支语句* → [*guard 语句*](#guard-statement)
|
||||
@ -162,9 +150,7 @@ repeat {
|
||||
> *分支语句* → [*switch 语句*](#switch-statement)
|
||||
>
|
||||
|
||||
<a name="if_statements"></a>
|
||||
### If 语句
|
||||
|
||||
### If 语句 {#if_statements}
|
||||
`if` 语句会根据一个或多个条件来决定执行哪一块代码。
|
||||
|
||||
`if` 语句有两种基本形式,无论哪种形式,都必须有花括号。
|
||||
@ -204,16 +190,14 @@ if condition 1 {
|
||||
> if 语句语法
|
||||
>
|
||||
>
|
||||
<a name="if-statement"></a>
|
||||
if-statement {#if-statement}
|
||||
> *if 语句* → **if** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block) [*else 子句*](#else-clause)<sub>可选</sub>
|
||||
>
|
||||
<a name="else-clause"></a>
|
||||
else-clause {#else-clause}
|
||||
> *else 子句* → **else** [*代码块*](./06_Declarations.md#code-block) | **else** [*if 语句*](#if-statement)
|
||||
>
|
||||
|
||||
<a name="guard_statements"></a>
|
||||
### Guard 语句
|
||||
|
||||
### Guard 语句 {#guard_statements}
|
||||
如果一个或者多个条件不成立,可用 `guard` 语句来退出当前作用域。
|
||||
|
||||
`guard` 语句的格式如下:
|
||||
@ -240,13 +224,11 @@ guard condition else {
|
||||
> guard 语句语法
|
||||
>
|
||||
>
|
||||
<a name="guard-statement"></a>
|
||||
guard-statement {#guard-statement}
|
||||
> *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
<a name="switch_statements"></a>
|
||||
### Switch 语句
|
||||
|
||||
### Switch 语句 {#switch_statements}
|
||||
`switch` 语句会根据控制表达式的值来决定执行哪部分代码。
|
||||
|
||||
`switch` 语句的形式如下:
|
||||
@ -285,9 +267,7 @@ case let (x, y) where x == y:
|
||||
|
||||
在 Swift 中,`switch` 语句中控制表达式的每一个可能的值都必须至少有一个 `case` 与之对应。在某些无法面面俱到的情况下(例如,表达式的类型是 `Int`),你可以使用 `default` 分支满足该要求。
|
||||
|
||||
<a name="future-case"></a>
|
||||
#### 对未来枚举的 `case` 进行 `switch`
|
||||
|
||||
#### 对未来枚举的 `case` 进行 `switch` {#future-case}
|
||||
非冻结枚举(`nonfronzen enumeration`)是一种特殊的枚举类型,它可能在未来会增加新的枚举 `case`,即使这时候你已经编译并且发布了你的应用,所以在 switch 非冻结枚举前需要深思熟虑。当一个库的作者们把一个枚举标记为非冻结的,这意味着他们保留了增加新的枚举 `case` 的权利,并且任何和这个枚举交互的代码都要在不需要重新编译的条件下能够处理那些未来可能新加入的 `case` 。只有那些标准库,比如用 Swift 实现的苹果的一些框架,C 以及 Objective-C 代码才能够声明非冻结枚举。你在 Swift 中声明的枚举不能是非冻结的。
|
||||
|
||||
当你对未来枚举进行 switch 时,你总是需要有一个 `default case`,即使每种枚举类型都已经有对应的 `case` 了。你可以在 default 前标注 `@unknown` ,意思是这个 `case` 应该只匹配未来加入的枚举 `case` 。如果你的 `default case` 中匹配了任何在编译时就能确定的枚举 `case` ,Swift 会抛出一个警告。这可以很好地提醒你库的作者已经新增了一种 `case` ,并且你还没有去处理。
|
||||
@ -316,52 +296,50 @@ case .suppressed:
|
||||
> switch 语句语法
|
||||
>
|
||||
>
|
||||
<a name="switch-statement"></a>
|
||||
switch-statement {#switch-statement}
|
||||
> *switch 语句* → **switch** [*表达式*](./04_Expressions.md#expression) **{** [*switch-case 列表*](#switch-cases)<sub>可选</sub> **}**
|
||||
>
|
||||
<a name="switch-cases"></a>
|
||||
switch-cases {#switch-cases}
|
||||
> *switch case 列表* → [*switch-case*](#switch-case) [*switch-case 列表*](#switch-cases)<sub>可选</sub>
|
||||
>
|
||||
<a name="switch-case"></a>
|
||||
switch-case {#switch-case}
|
||||
> *switch case* → [*case 标签*](#case-label) [*多条语句*](#statements) | [*default 标签*](#default-label) [*多条语句*](#statements) | [*conditional-switch-case*](#conditional-switch-case-label)
|
||||
>
|
||||
|
||||
<a name="case-label"></a>
|
||||
case-label {#case-label}
|
||||
> *case 标签* → [*属性*](#switch-case-attributes-label)<sub>可选</sub> **case** [*case 项列表*](#case-item-list) **:**
|
||||
>
|
||||
<a name="case-item-list"></a>
|
||||
case-item-list {#case-item-list}
|
||||
> *case 项列表* → [*模式*](./08_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> | [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> **,** [*case 项列表*](#case-item-list)
|
||||
>
|
||||
<a name="default-label"></a>
|
||||
default-label {#default-label}
|
||||
> *default 标签* → [*属性*](#switch-case-attributes-label)<sub>可选</sub> **default** **:**
|
||||
>
|
||||
>
|
||||
<a name="where-clause"></a>
|
||||
where-clause {#where-clause}
|
||||
> *where-clause* → **where** [*where 表达式*](#where-expression)
|
||||
>
|
||||
<a name="where-expression"></a>
|
||||
where-expression {#where-expression}
|
||||
> *where-expression* → [*表达式*](./04_Expressions.md#expression)
|
||||
>
|
||||
>
|
||||
<a name="grammar_conditional-switch-case"></a>
|
||||
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)
|
||||
>
|
||||
<a name="grammar_switch-if-directive-clause"></a>
|
||||
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>
|
||||
>
|
||||
<a name="grammar_switch-elseif-directive-clauses"></a>
|
||||
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>
|
||||
>
|
||||
<a name="grammar_switch-elseif-directive-clause"></a>
|
||||
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>
|
||||
>
|
||||
<a name="grammar_switch-else-directive-clause"></a>
|
||||
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>
|
||||
>
|
||||
|
||||
<a name="labeled_statements"></a>
|
||||
## 带标签的语句
|
||||
|
||||
## 带标签的语句 {#labeled_statements}
|
||||
你可以在循环语句或 `switch` 语句前面加上标签,它由标签名和紧随其后的冒号(`:`)组成。在 `break` 和 `continue` 后面跟上标签名可以显式地在循环语句或 `switch` 语句中改变相应的控制流。关于这两条语句用法,请参阅 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。
|
||||
|
||||
标签的作用域在该标签所标记的语句内。可以嵌套使用带标签的语句,但标签名必须唯一。
|
||||
@ -371,7 +349,7 @@ case .suppressed:
|
||||
> 带标签的语句语法
|
||||
>
|
||||
>
|
||||
<a name="labeled-statement"></a>
|
||||
labeled-statement {#labeled-statement}
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#grammar_loop-statement)
|
||||
>
|
||||
> *带标签的语句* → [*语句标签*](#statement-label) [*if 语句*](#if-statement)
|
||||
@ -380,22 +358,20 @@ case .suppressed:
|
||||
>
|
||||
> > *带标签的语句* → [*语句标签*](#statement-label) [*do 语句*](#sdo-statement)
|
||||
>
|
||||
<a name="statement-label"></a>
|
||||
statement-label {#statement-label}
|
||||
> *语句标签* → [*标签名称*](#label-name) **:**
|
||||
>
|
||||
<a name="label-name"></a>
|
||||
label-name {#label-name}
|
||||
> *标签名称* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
<a name="control_transfer_statements"></a>
|
||||
## 控制转移语句
|
||||
|
||||
## 控制转移语句 {#control_transfer_statements}
|
||||
控制转移语句能够无条件地把控制权从一片代码转移到另一片代码,从而改变代码执行的顺序。Swift 提供五种类型的控制转移语句:`break` 语句、`continue` 语句、`fallthrough` 语句、`return` 语句和 `throw` 语句。
|
||||
|
||||
> 控制转移语句语法
|
||||
>
|
||||
>
|
||||
<a name="control-transfer-statement"></a>
|
||||
control-transfer-statement {#control-transfer-statement}
|
||||
> *控制转移语句* → [*break 语句*](#break-statement)
|
||||
>
|
||||
> *控制转移语句* → [*continue 语句*](#continue-statement)
|
||||
@ -407,9 +383,7 @@ case .suppressed:
|
||||
> *控制转移语句* → [*throw 语句*](#throw-statement)
|
||||
>
|
||||
|
||||
<a name="break_statement"></a>
|
||||
### Break 语句
|
||||
|
||||
### Break 语句 {#break_statement}
|
||||
`break` 语句用于终止循环语句、`if` 语句或 `switch` 语句的执行。使用 `break` 语句时,可以只写 `break` 这个关键词,也可以在 `break` 后面跟上标签名,像下面这样:
|
||||
|
||||
> break
|
||||
@ -428,13 +402,11 @@ case .suppressed:
|
||||
> break 语句语法
|
||||
>
|
||||
>
|
||||
<a name="break-statement"></a>
|
||||
break-statement {#break-statement}
|
||||
> *break 语句* → **break** [*标签名称*](#label-name)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a name="continue_statement"></a>
|
||||
### Continue 语句
|
||||
|
||||
### Continue 语句 {#continue_statement}
|
||||
`continue` 语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用 `continue` 语句时,可以只写 `continue` 这个关键词,也可以在 `continue` 后面跟上标签名,像下面这样:
|
||||
|
||||
> continue
|
||||
@ -455,13 +427,11 @@ case .suppressed:
|
||||
> continue 语句语法
|
||||
>
|
||||
>
|
||||
<a name="continue-statement"></a>
|
||||
continue-statement {#continue-statement}
|
||||
> *continue 语句* → **continue** [*标签名称*](#label-name)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a name="fallthrough_statements"></a>
|
||||
### Fallthrough 语句
|
||||
|
||||
### Fallthrough 语句 {#fallthrough_statements}
|
||||
`fallthrough` 语句用于在 `switch` 语句中转移控制权。`fallthrough` 语句会把控制权从 `switch` 语句中的一个 `case` 转移到下一个 `case`。这种控制权转移是无条件的,即使下一个 `case` 的模式与 `switch` 语句的控制表达式的值不匹配。
|
||||
|
||||
`fallthrough` 语句可出现在 `switch` 语句中的任意 `case` 中,但不能出现在最后一个 `case` 中。同时,`fallthrough` 语句也不能把控制权转移到使用了值绑定的 `case`。
|
||||
@ -471,13 +441,11 @@ case .suppressed:
|
||||
> fallthrough 语句语法
|
||||
>
|
||||
>
|
||||
<a name="fallthrough-statement"></a>
|
||||
fallthrough-statement {#fallthrough-statement}
|
||||
> *fallthrough 语句* → **fallthrough**
|
||||
>
|
||||
|
||||
<a name="return_statements"></a>
|
||||
### Return 语句
|
||||
|
||||
### Return 语句 {#return_statements}
|
||||
`return` 语句用于在函数或方法的实现中将控制权转移到调用函数或方法,接着程序将会从调用位置继续向下执行。
|
||||
|
||||
使用 `return` 语句时,可以只写 `return` 这个关键词,也可以在 `return` 后面跟上表达式,像下面这样:
|
||||
@ -500,13 +468,11 @@ case .suppressed:
|
||||
> return 语句语法
|
||||
>
|
||||
>
|
||||
<a name="return-statement"></a>
|
||||
return-statement {#return-statement}
|
||||
> *return 语句* → **return** [*表达式*](./04_Expressions.md#expression)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a name="throw_statements"></a>
|
||||
### Throw 语句
|
||||
|
||||
### Throw 语句 {#throw_statements}
|
||||
`throw` 语句出现在抛出函数或者抛出方法体内,或者类型被 `throws` 关键字标记的闭包表达式体内。
|
||||
|
||||
`throw` 语句使程序在当前作用域结束执行,并向外围作用域传播错误。抛出的错误会一直传递,直到被 `do` 语句的 `catch` 子句处理掉。
|
||||
@ -523,13 +489,11 @@ case .suppressed:
|
||||
> throw 语句语法
|
||||
>
|
||||
>
|
||||
<a name="throw-statement"></a>
|
||||
throw-statement {#throw-statement}
|
||||
> *throw 语句* → **throw** [*表达式*](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
<a name="defer_statements"></a>
|
||||
## Defer 语句
|
||||
|
||||
## Defer 语句 {#defer_statements}
|
||||
`defer` 语句用于在退出当前作用域之前执行代码。
|
||||
|
||||
`defer` 语句形式如下:
|
||||
@ -561,13 +525,11 @@ f()
|
||||
> defer 语句语法
|
||||
>
|
||||
>
|
||||
<a name="defer-statement"></a>
|
||||
defer-statement {#defer-statement}
|
||||
> *延迟语句* → **defer** [*代码块*](./06_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
<a name="do_statements"></a>
|
||||
## Do 语句
|
||||
|
||||
## Do 语句 {#do_statements}
|
||||
`do` 语句用于引入一个新的作用域,该作用域中可以含有一个或多个 `catch` 子句,`catch` 子句中定义了一些匹配错误条件的模式。`do` 语句作用域内定义的常量和变量只能在 `do` 语句作用域内使用。
|
||||
|
||||
Swift 中的 `do` 语句与 C 中限定代码块界限的大括号(`{}`)很相似,也并不会降低程序运行时的性能。
|
||||
@ -594,25 +556,23 @@ do {
|
||||
> do 语句语法
|
||||
>
|
||||
>
|
||||
<a name="do-statement"></a>
|
||||
do-statement {#do-statement}
|
||||
> *do 语句* → **do** [*代码块*](./06_Declarations.md#code-block) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
|
||||
>
|
||||
<a name="catch-clauses"></a>
|
||||
catch-clauses {#catch-clauses}
|
||||
> *多条 catch 子句* → [*catch 子句*](#catch-clause) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
|
||||
>
|
||||
<a name="catch-clause"></a>
|
||||
catch-clause {#catch-clause}
|
||||
> *catch 子句* → **catch** [*模式*](./08_Patterns.md#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
|
||||
>
|
||||
|
||||
<a name="compiler_control_statements"></a>
|
||||
## 编译器控制语句
|
||||
|
||||
## 编译器控制语句 {#compiler_control_statements}
|
||||
编译器控制语句允许程序改变编译器的行为。Swift 有三种编译器控制语句:条件编译语句、线路控制语句和编译时诊断语句。
|
||||
|
||||
> 编译器控制语句语法
|
||||
>
|
||||
>
|
||||
<a name="compiler-control-statement"></a>
|
||||
compiler-control-statement {#compiler-control-statement}
|
||||
> *编译器控制语句* → [*条件编译语句*](#grammar_conditional-compilation-block)
|
||||
>
|
||||
> *编译器控制语句* → [*线路控制语句*](#line-control-statement)
|
||||
@ -620,9 +580,7 @@ do {
|
||||
> *编译器控制语句* → [*诊断语句*](#grammar_diagnostic-statement)
|
||||
>
|
||||
|
||||
<a name="Conditional_Compilation_Block"></a>
|
||||
### 条件编译代码块
|
||||
|
||||
### 条件编译代码块 {#Conditional_Compilation_Block}
|
||||
条件编译代码块可以根据一个或多个配置来有条件地编译代码。
|
||||
|
||||
每一个条件编译代码块都以 `#if` 开始,`#endif` 结束。如下:
|
||||
@ -693,23 +651,23 @@ statements to compile if both compilation conditions are false
|
||||
> 即使没有被编译,编译配置中的语句仍然会被解析。然而,唯一的例外是编译配置语句中包含语言版本检测函数:仅当 `Swift` 编译器版本和语言版本检测函数中指定的版本号匹配时,语句才会被解析。这种设定能确保旧的编译器不会尝试去解析新 Swift 版本的语法。
|
||||
>
|
||||
|
||||
<a name="build-config-statement"></a>
|
||||
build-config-statement {#build-config-statement}
|
||||
> 条件编译代码块语法
|
||||
>
|
||||
>
|
||||
<a name="grammar_conditional-compilation-block"></a>
|
||||
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)
|
||||
>
|
||||
<a name="grammar_if-directive-clause"></a>
|
||||
grammar_if-directive-clause {#grammar_if-directive-clause}
|
||||
> *if-directive 语句* → [*if-directive*](#grammar_if-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
>
|
||||
<a name="grammar_elseif-directive-clauses"></a>
|
||||
grammar_elseif-directive-clauses {#grammar_elseif-directive-clauses}
|
||||
> *elseif-directive 语句(复数)* → [*elseif-directive 语句*](#grammar_elseif-directive-clause) [*elseif-directive 语句(复数)*](#grammar_elseif-directive-clauses)
|
||||
>
|
||||
<a name="grammar_elseif-directive-clauses"></a>
|
||||
grammar_elseif-directive-clauses {#grammar_elseif-directive-clauses}
|
||||
> *elseif-directive 语句* → [*elseif-directive*](#grammar_elseif-directive) [*编译条件*](#compilation-condition) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
>
|
||||
<a name="grammar_else-directive-clause"></a>
|
||||
grammar_else-directive-clause {#grammar_else-directive-clause}
|
||||
> *else-directive 语句* → [*else-directive*](#grammar_else-directive) [*语句(复数)*](#statements)<sub>可选</sub>
|
||||
>
|
||||
|
||||
@ -723,7 +681,7 @@ statements to compile if both compilation conditions are false
|
||||
> *endif-directive* → **#endif**
|
||||
>
|
||||
|
||||
<a name="compilation-condition"></a>
|
||||
compilation-condition {#compilation-condition}
|
||||
> *编译条件* → [*平台条件*](#grammar_platform-condition)
|
||||
>
|
||||
> *编译条件* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
@ -739,47 +697,45 @@ statements to compile if both compilation conditions are false
|
||||
> *编译条件* → [*编译条件*](#compilation-condition) **||** [*编译条件*](#compilation-condition)
|
||||
>
|
||||
|
||||
<a name="grammar_platform-condition"></a>
|
||||
<a name="grammar_platform-condition-os"></a>
|
||||
grammar_platform-condition {#grammar_platform-condition}
|
||||
grammar_platform-condition-os {#grammar_platform-condition-os}
|
||||
> *平台条件* → **os ( [*操作系统*](#operating-system) )**
|
||||
>
|
||||
<a name="grammar_platform-condition-arch"></a>
|
||||
grammar_platform-condition-arch {#grammar_platform-condition-arch}
|
||||
> *平台条件* → **arch ( [*架构*](#architecture) )**
|
||||
>
|
||||
<a name="grammar_platform-condition-swift"></a>
|
||||
grammar_platform-condition-swift {#grammar_platform-condition-swift}
|
||||
> *平台条件* → **swift ( >= [*swift 版本*](#swift-version) )** | **swift ( < [*swift 版本*](#swift-version) )**
|
||||
>
|
||||
<a name="grammar_platform-condition-compiler"></a>
|
||||
grammar_platform-condition-compiler {#grammar_platform-condition-compiler}
|
||||
> *平台条件* → **compiler ( >= [*swift 版本*](#swift-version) )** | **compiler ( < [*swift 版本*](#swift-version) )**
|
||||
>
|
||||
<a name="grammar_platform-condition-canImport"></a>
|
||||
grammar_platform-condition-canImport {#grammar_platform-condition-canImport}
|
||||
> *平台条件* → **canImport ( [*模块名*](#grammar_module-name) )**
|
||||
>
|
||||
<a name="grammar_platform-condition-targetEnvironment"></a>
|
||||
grammar_platform-condition-targetEnvironment {#grammar_platform-condition-targetEnvironment}
|
||||
> *平台条件* → **targetEnvironment ( [*环境*](#grammar_environment) )**
|
||||
>
|
||||
<a name="operating-system"></a>
|
||||
operating-system {#operating-system}
|
||||
> *操作系统* → **macOS** | **iOS** | **watchOS** | **tvOS**
|
||||
>
|
||||
<a name="architecture"></a>
|
||||
architecture {#architecture}
|
||||
> *架构* → **i386** | **x86_64** | **arm** | **arm64**
|
||||
>
|
||||
<a name="swift-version"></a>
|
||||
swift-version {#swift-version}
|
||||
> *swift 版本* → [*十进制数字*](./02_Lexical_Structure.md#decimal-digit) **.** [*swift 版本延续*](#grammar_swift-version-continuation) <sub>可选</sub>
|
||||
>
|
||||
<a name="grammar_swift-version-continuation"></a>
|
||||
grammar_swift-version-continuation {#grammar_swift-version-continuation}
|
||||
> *swift 版本延续* → **.** [*十进制数字*](./02_Lexical_Structure.md#decimal-digit) [*swift 版本延续*](#grammar_swift-version-continuation) <sub>可选</sub>
|
||||
>
|
||||
<a name="grammar_module-name"></a>
|
||||
grammar_module-name {#grammar_module-name}
|
||||
> *模块名* → [*identifier*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
<a name="grammar_environment"></a>
|
||||
grammar_environment {#grammar_environment}
|
||||
> *环境* → **模拟器**
|
||||
>
|
||||
|
||||
<a name="line_control_statements"></a>
|
||||
### 行控制语句
|
||||
|
||||
### 行控制语句 {#line_control_statements}
|
||||
行控制语句可以为被编译的源代码指定行号和文件名,从而改变源代码的定位信息,以便进行分析和调试。
|
||||
|
||||
行控制语句形式如下:
|
||||
@ -794,7 +750,7 @@ statements to compile if both compilation conditions are false
|
||||
|
||||
第二种的行控制语句,`#sourceLocation()`,会将源代码的定位信息重置回默认的行号和文件名。
|
||||
|
||||
<a name="line-control-statement"></a>
|
||||
line-control-statement {#line-control-statement}
|
||||
> 行控制语句语法
|
||||
>
|
||||
>
|
||||
@ -802,10 +758,10 @@ statements to compile if both compilation conditions are false
|
||||
>
|
||||
> *行控制语句* → **#sourceLocation()**
|
||||
>
|
||||
<a name="line-number"></a>
|
||||
line-number {#line-number}
|
||||
> *行号* → 大于 0 的十进制整数
|
||||
>
|
||||
<a name="file-name"></a>
|
||||
file-name {#file-name}
|
||||
> *文件名* → [*静态字符串字面量*](./02_Lexical_Structure.md#static-string-literal)
|
||||
>
|
||||
|
||||
@ -823,18 +779,16 @@ statements to compile if both compilation conditions are false
|
||||
> 编译时诊断语句语法
|
||||
>
|
||||
>
|
||||
<a name="grammar_compile-time-diagnostic-statement"></a>
|
||||
grammar_compile-time-diagnostic-statement {#grammar_compile-time-diagnostic-statement}
|
||||
> *诊断语句* → **#error** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)**
|
||||
>
|
||||
> *诊断语句* → **#warning** **(** [*diagnostic-message*](#grammar_diagnostic-message) **)**
|
||||
>
|
||||
<a name="grammar_diagnostic-message"></a>
|
||||
grammar_diagnostic-message {#grammar_diagnostic-message}
|
||||
> *诊断语句* → [*静态字符串字面量*](./02_Lexical_Structure.md#static-string-literal)
|
||||
>
|
||||
|
||||
<a name="availability_condition"></a>
|
||||
## 可用性条件
|
||||
|
||||
## 可用性条件 {#availability_condition}
|
||||
可用性条件可作为 `if`,`while`,`guard` 语句的条件,可以在运行时基于特定的平台参数来查询 API 的可用性。
|
||||
|
||||
可用性条件的形式如下:
|
||||
@ -856,19 +810,19 @@ if #available(platform name version, ..., *) {
|
||||
> 可用性条件语法
|
||||
>
|
||||
>
|
||||
<a name="availability-condition"></a>
|
||||
availability-condition {#availability-condition}
|
||||
> *可用性条件* → **#available** **(** [*可用性参数列表*](#availability-arguments) **)**
|
||||
>
|
||||
<a name="availability-arguments"></a>
|
||||
availability-arguments {#availability-arguments}
|
||||
> *可用性参数列表* → [*可用性参数*](#availability-argument) | [*可用性参数*](#availability-argument) **,** [*可用性参数列表*](#availability-arguments)
|
||||
>
|
||||
<a name="availability-argument"></a>
|
||||
availability-argument {#availability-argument}
|
||||
> *可用性参数* → [平台名称](#platform-name) [平台版本](#platform-version)
|
||||
>
|
||||
> *可用性条件* → __*__
|
||||
>
|
||||
>
|
||||
<a name="platform-name"></a>
|
||||
platform-name {#platform-name}
|
||||
> *平台名称* → **iOS** | **iOSApplicationExtension**
|
||||
>
|
||||
> *平台名称* → **OSX** | **macOSApplicationExtension**
|
||||
@ -877,7 +831,7 @@ if #available(platform name version, ..., *) {
|
||||
>
|
||||
> *平台名称* → **tvOS**
|
||||
>
|
||||
<a name="platform-version"></a>
|
||||
platform-version {#platform-version}
|
||||
> *平台版本* → [十进制数字](./02_Lexical_Structure.md#decimal-digits)
|
||||
>
|
||||
> *平台版本* → [十进制数字](./02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](./02_Lexical_Structure.md#decimal-digits)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -10,13 +10,11 @@
|
||||
|
||||
有些声明特性通过接收参数来指定特性的更多信息以及它是如何修饰某个特定的声明的。这些_特性的参数_写在圆括号内,它们的格式由它们所属的特性来定义。
|
||||
|
||||
<a name="declaration_attributes"></a>
|
||||
## 声明特性
|
||||
## 声明特性 {#declaration_attributes}
|
||||
|
||||
声明特性只能应用于声明。
|
||||
|
||||
<a name="available"></a>
|
||||
### `available`
|
||||
### `available` {#available}
|
||||
|
||||
将 `available` 特性用于声明时,表示该声明的生命周期是相对于特定的平台和操作系统版本。
|
||||
|
||||
@ -112,13 +110,11 @@ struct MyStruct {
|
||||
}
|
||||
```
|
||||
|
||||
<a name="discardableresult"></a>
|
||||
### `discardableResult`
|
||||
### `discardableResult` {#discardableresult}
|
||||
|
||||
该特性用于的函数或方法声明,以抑制编译器中函数或方法的返回值被调而没有使用其结果的警告。
|
||||
|
||||
<a name="dynamiccallable"></a>
|
||||
### `dynamicCallable`
|
||||
### `dynamicCallable` {#dynamiccallable}
|
||||
|
||||
该特性用于类、结构体、枚举或协议,以将该类型的实例视为可调用的函数。该类型必须实现 `dynamicallyCall(withArguments:)`、`dynamicallyCall(withKeywordArguments:)` 方法之一,或两者同时实现。
|
||||
|
||||
@ -183,8 +179,7 @@ print(repeatLabels(a: 1, b: 2, c: 3, b: 2, a: 1))
|
||||
repeatLabels(a: "four") // Error
|
||||
```
|
||||
|
||||
<a name="dynamicmemberlookup"></a>
|
||||
### `dynamicMemberLookup`
|
||||
### `dynamicMemberLookup` {#dynamicmemberlookup}
|
||||
|
||||
该特性用于类、结构体、枚举或协议,让其能在运行时查找成员。该类型必须实现 `subscript(dynamicMemberLookup:)` 下标。
|
||||
|
||||
@ -213,13 +208,11 @@ print(dynamic == equivalent)
|
||||
// 打印“true”
|
||||
```
|
||||
|
||||
<a name="gkinspectable"></a>
|
||||
### `GKInspectable`
|
||||
### `GKInspectable` {#gkinspectable}
|
||||
|
||||
应用此属性,暴露一个自定义 GameplayKit 组件属性给 SpriteKit 编辑器 UI。
|
||||
|
||||
<a name="inlinable"></a>
|
||||
### `inlinable`
|
||||
### `inlinable` {#inlinable}
|
||||
|
||||
该特性用于函数、方法、计算属性、下标、便利构造器或析构器的声明,以将该声明的实现公开为模块公开接口的一部分。编译器允许在调用处把 `inlinable` 标记的符号替换为符号实现的副本。
|
||||
|
||||
@ -227,8 +220,7 @@ print(dynamic == equivalent)
|
||||
|
||||
该特性不能用于嵌套在函数内的声明,也不能用于 `fileprivate` 或 `private` 访问级别的声明。在内联函数定义的函数和闭包是隐式非内联的,即使他们不能标记该特性。
|
||||
|
||||
<a name="nonobjc"></a>
|
||||
### `nonobjc`
|
||||
### `nonobjc` {#nonobjc}
|
||||
|
||||
该特性用于方法、属性、下标、或构造器的声明,这些声明本可以在 Objective-C 代码中使用,而使用 `nonobjc` 特性则告诉编译器这个声明不能在 Objective-C 代码中使用。
|
||||
|
||||
@ -238,8 +230,7 @@ print(dynamic == equivalent)
|
||||
|
||||
标有 `nonobjc` 特性的方法不能重写标有 `objc` 特性的方法。然而,标有 `objc` 特性的方法可以重写标有 `nonobjc` 特性的方法。同样,标有 `nonobjc` 特性的方法不能满足标有 `@objc` 特性的协议中的方法要求。
|
||||
|
||||
<a name="nsapplicationmain"></a>
|
||||
### `NSApplicationMain`
|
||||
### `NSApplicationMain` {#nsapplicationmain}
|
||||
|
||||
在类上使用该特性表示该类是应用程序委托类,使用该特性与调用 `NSApplicationMain(_:_:)` 函数并且把该类的名字作为委托类的名字传递给函数的效果相同。
|
||||
|
||||
@ -250,20 +241,17 @@ import AppKit
|
||||
NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
|
||||
```
|
||||
|
||||
<a name="nscopying"></a>
|
||||
### `NSCopying`
|
||||
### `NSCopying` {#nscopying}
|
||||
|
||||
该特性用于修饰一个类的存储型变量属性。该特性将使属性的设值方法使用传入值的副本进行赋值,这个值由传入值的 `copyWithZone(_:)` 方法返回。该属性的类型必需符合 `NSCopying` 协议。
|
||||
|
||||
`NSCopying` 特性的行为与 Objective-C 中的 `copy` 特性相似。
|
||||
|
||||
<a name="nsmanaged"></a>
|
||||
### `NSManaged`
|
||||
### `NSManaged` {#nsmanaged}
|
||||
|
||||
该特性用于修饰 `NSManagedObject` 子类中的实例方法或存储型变量属性,表明它们的实现由 `Core Data` 在运行时基于相关实体描述动态提供。对于标记了 `NSManaged` 特性的属性,`Core Data` 也会在运行时为其提供存储。应用这个特性也意味着 `objc` 特性。
|
||||
|
||||
<a name="objc"></a>
|
||||
### `objc`
|
||||
### `objc` {#objc}
|
||||
|
||||
该特性用于修饰任何可以在 Objective-C 中表示的声明。比如,非嵌套类、协议、非泛型枚举(仅限原始值为整型的枚举)、类和协议中的属性和方法(包括存取方法)、构造器、析构器以及下标运算符。`objc` 特性告诉编译器这个声明可以在 Objective-C 代码中使用。
|
||||
|
||||
@ -291,32 +279,27 @@ class ExampleClass: NSObject {
|
||||
}
|
||||
```
|
||||
|
||||
<a name="objcmembers"></a>
|
||||
### `objcMembers`
|
||||
### `objcMembers` {#objcmembers}
|
||||
|
||||
该特性用于类声明,以将 `objc` 特性应用于该类、扩展、子类以及子类的扩展的所有 Objective-C 兼容成员。
|
||||
|
||||
大多数代码应该使用 `objc` 特性,以暴露所需的声明。如果需要暴露多个声明,可以将其分组到添加 `objc` 特性的扩展中。`objcMembers` 特性为大量使用 Objective-C 运行时的内省工具的库提供了便利。添加不必要的 `objc` 特性会增加二进制体积并影响性能。
|
||||
|
||||
<a name="requires_stored_property_inits"></a>
|
||||
### `requires_stored_property_inits`
|
||||
### `requires_stored_property_inits` {#requires_stored_property_inits}
|
||||
|
||||
该特性用于类声明,以要求类中所有存储属性提供默认值作为其定义的一部分。对于从中继承的任何类都推断出 `NSManagedObject` 特性。
|
||||
|
||||
<a name="testable"></a>
|
||||
### `testable`
|
||||
### `testable` {#testable}
|
||||
|
||||
在导入允许测试的编译模块时,该特性用于修饰 `import` 声明,这样就能访问被导入模块中的任何标有 `internal` 访问级别修饰符的实体,犹如它们被标记了 `public` 访问级别修饰符。测试也可以访问使用 `internal` 或者 `public` 访问级别修饰符标记的类和类成员,就像它们是 `open` 访问修饰符声明的。
|
||||
|
||||
<a name="uiapplicationmain"></a>
|
||||
### `UIApplicationMain`
|
||||
### `UIApplicationMain` {#uiapplicationmain}
|
||||
|
||||
在类上使用该特性表示该类是应用程序委托类,使用该特性与调用 `UIApplicationMain` 函数并且把该类的名字作为委托类的名字传递给函数的效果相同。
|
||||
|
||||
如果你不想使用这个特性,可以提供一个 `main.swift` 文件,并在代码顶层调用 `UIApplicationMain(_:_:_:_:)` 函数。比如,如果你的应用程序使用一个继承于 UIApplication 的自定义子类作为主要类,你可以调用 `UIApplicationMain(_:_:_:_:)` 函数而不是使用该特性。
|
||||
|
||||
<a name="usablefrominline"></a>
|
||||
### `usableFromInline`
|
||||
### `usableFromInline` {#usablefrominline}
|
||||
|
||||
该特性用于函数、方法、计算属性、下标、构造器或析构器的声明,以在同一模块中允许该符号用于内联代码的声明。声明必须具有 `internal` 访问级别修饰符。
|
||||
|
||||
@ -324,16 +307,13 @@ class ExampleClass: NSObject {
|
||||
|
||||
标记为 `inlinable` 特性的声明,在内联代码中可以隐式使用。虽然 `inlinable` 或 `usableFromInline` 可以用于 `internal` 声明,但这两者不能同时使用。
|
||||
|
||||
<a name="warn_unqualified_access"></a>
|
||||
### `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` 扩展中使用它们的歧义。
|
||||
|
||||
<a name="declaration_attributes_used_by_interface_builder"></a>
|
||||
|
||||
### Interface Builder 使用的声明特性
|
||||
### Interface Builder 使用的声明特性 {#declaration_attributes_used_by_interface_builder}
|
||||
|
||||
`Interface Builder` 特性是 `Interface Builder` 用来与 Xcode 同步的声明特性。`Swift` 提供了以下的 `Interface Builder` 特性:`IBAction`,`IBOutlet`,`IBDesignable`,以及 `IBInspectable` 。这些特性与 Objective-C 中对应的特性在概念上是相同的。
|
||||
|
||||
@ -341,18 +321,15 @@ class ExampleClass: NSObject {
|
||||
|
||||
应用 `IBAction`、`IBOutlet`、`IBDesignable` 或者 `IBInspectable` 特性都意味着同时应用 `objc` 特性。
|
||||
|
||||
<a name="type_attributes"></a>
|
||||
## 类型特性
|
||||
## 类型特性 {#type_attributes}
|
||||
|
||||
类型特性只能用于修饰类型。
|
||||
|
||||
<a name="autoclosure"></a>
|
||||
### `autoclosure`
|
||||
### `autoclosure` {#autoclosure}
|
||||
|
||||
这个特性通过把表达式自动封装成无参数的闭包来延迟表达式的计算。它可以修饰类型为返回表达式结果类型的无参数函数类型的函数参数。关于如何使用 autoclosure 特性的例子,请参阅 [自动闭包](../chapter2/07_Closures.md#autoclosures) 和 [函数类型](./03_Types.md#function_type)。
|
||||
|
||||
<a name="convention"></a>
|
||||
### `convention`
|
||||
### `convention` {#convention}
|
||||
|
||||
该特性用于修饰函数类型,它指出了函数调用的约定。
|
||||
|
||||
@ -366,13 +343,11 @@ convention 特性总是与下面的参数之一一起出现。
|
||||
|
||||
使用 C 函数调用约定的函数也可用作使用 Objective-C 块调用约定的函数,同时使用 Objective-C 块调用约定的函数也可用作使用 Swift 函数调用约定的函数。然而,只有非泛型的全局函数、局部函数以及未捕获任何局部变量的闭包,才可以被用作使用 C 函数调用约定的函数。
|
||||
|
||||
<a name="escaping"></a>
|
||||
### `escaping`
|
||||
### `escaping` {#escaping}
|
||||
|
||||
在函数或者方法声明上使用该特性,它表示参数将不会被存储以供延迟执行,这将确保参数不会超出函数调用的生命周期。在使用 `escaping` 声明特性的函数类型中访问属性和方法时不需要显式地使用 `self.`。关于如何使用 `escaping` 特性的例子,请参阅 [逃逸闭包](../chapter2/07_Closures.md#escaping_closures)。
|
||||
|
||||
<a name="switch_case_attributes"></a>
|
||||
## Switch Case 特性
|
||||
## Switch Case 特性 {#switch_case_attributes}
|
||||
|
||||
你只能在 switch cases 中使用 switch case 特性。
|
||||
|
||||
@ -383,28 +358,28 @@ convention 特性总是与下面的参数之一一起出现。
|
||||
> 特性语法
|
||||
>
|
||||
>
|
||||
> <a name="attribute"></a>
|
||||
> attribute {#attribute}
|
||||
>
|
||||
> *特性*→ [特性名](#attribute_name) [特性参数子句](#atribute_argument_clause)<sub>可选</sub>
|
||||
>
|
||||
> <a name="attribute_name"></a>
|
||||
> attribute_name {#attribute_name}
|
||||
>
|
||||
> *特性名* → [标识符](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
> <a name="atribute_argument_clause"></a>
|
||||
> atribute_argument_clause {#atribute_argument_clause}
|
||||
>
|
||||
> *特性参数子句* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)**
|
||||
>
|
||||
> <a name="attributes"></a>
|
||||
> attributes {#attributes}
|
||||
>
|
||||
> *特性列表* → [特性](#attribute) [特性列表](#attributes)<sub>可选</sub>
|
||||
>
|
||||
>
|
||||
> <a name="balanced_tokens"></a>
|
||||
> balanced_tokens {#balanced_tokens}
|
||||
>
|
||||
> *均衡令牌列表* → [均衡令牌](#balanced_token) [均衡令牌列表](#balanced_tokens)<sub>可选</sub>
|
||||
>
|
||||
> <a name="balanced_token"></a>
|
||||
> balanced_token {#balanced_token}
|
||||
>
|
||||
> *均衡令牌* → **(** [均衡令牌列表](#balanced_tokens)<sub>可选</sub> **)**
|
||||
>
|
||||
|
||||
@ -10,7 +10,7 @@ Swift 中的模式分为两类:一种能成功匹配任何类型的值,另
|
||||
|
||||
> 模式语法
|
||||
>
|
||||
<a name="pattern"></a>
|
||||
pattern {#pattern}
|
||||
> *模式* → [*通配符模式*](#wildcard_pattern) [*类型标注*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||
>
|
||||
> *模式* → [*标识符模式*](#identifier_pattern) [*类型标注*](03_Types.md#type-annotation)<sub>可选</sub>
|
||||
@ -40,13 +40,11 @@ for _ in 1...3 {
|
||||
|
||||
> 通配符模式语法
|
||||
>
|
||||
<a name="wildcard-pattern"></a>
|
||||
wildcard-pattern {#wildcard-pattern}
|
||||
> *通配符模式* → **_**
|
||||
>
|
||||
|
||||
<a name="identifier_pattern"></a>
|
||||
## 标识符模式(Identifier Pattern)
|
||||
|
||||
## 标识符模式(Identifier Pattern) {#identifier_pattern}
|
||||
*标识符模式*匹配任何值,并将匹配的值和一个变量或常量绑定起来。例如,在下面的常量声明中,`someValue` 是一个标识符模式,匹配了 `Int` 类型的 `42`:
|
||||
|
||||
```swift
|
||||
@ -59,13 +57,11 @@ let someValue = 42
|
||||
|
||||
> 标识符模式语法
|
||||
>
|
||||
<a name="identifier-pattern"></a>
|
||||
identifier-pattern {#identifier-pattern}
|
||||
> *标识符模式* → [*标识符*](./02_Lexical_Structure.md#identifier)
|
||||
>
|
||||
|
||||
<a name="value-binding_pattern"></a>
|
||||
## 值绑定模式(Value-Binding Pattern)
|
||||
|
||||
## 值绑定模式(Value-Binding Pattern) {#value-binding_pattern}
|
||||
*值绑定模式*把匹配到的值绑定给一个变量或常量。把匹配到的值绑定给常量时,用关键字 `let`,绑定给变量时,用关键字 `var`。
|
||||
|
||||
在值绑定模式中的标识符模式会把新命名的变量或常量与匹配到的值做绑定。例如,你可以拆开一个元组,然后把每个元素绑定到相应的标识符模式中。
|
||||
@ -84,13 +80,11 @@ case let (x, y):
|
||||
|
||||
> 值绑定模式语法
|
||||
>
|
||||
<a name="value-binding-pattern"></a>
|
||||
value-binding-pattern {#value-binding-pattern}
|
||||
> *值绑定模式* → **var** [*模式*](#pattern) | **let** [*模式*](#pattern)
|
||||
>
|
||||
|
||||
<a name="tuple_pattern"></a>
|
||||
## 元组模式
|
||||
|
||||
## 元组模式 {#tuple_pattern}
|
||||
*元组模式*是由逗号分隔的,具有零个或多个模式的列表,并由一对圆括号括起来。元组模式匹配相应元组类型的值。
|
||||
|
||||
你可以使用类型标注去限制一个元组模式能匹配哪种元组类型。例如,在常量声明 `let (x, y): (Int, Int) = (1, 2)` 中的元组模式 `(x, y): (Int, Int)` 只匹配两个元素都是 `Int` 类型的元组。
|
||||
@ -115,32 +109,28 @@ let (a): Int = 2 // a: Int = 2
|
||||
|
||||
> 元组模式语法
|
||||
>
|
||||
<a name="tuple-pattern"></a>
|
||||
tuple-pattern {#tuple-pattern}
|
||||
> *元组模式* → **(** [*元组模式元素列表*](#tuple-pattern-element-list)<sub>可选</sub> **)**
|
||||
>
|
||||
<a name="tuple-pattern-element-list"></a>
|
||||
tuple-pattern-element-list {#tuple-pattern-element-list}
|
||||
> *元组模式元素列表* → [*元组模式元素*](#tuple-pattern-element) | [*元组模式元素*](#tuple-pattern-element) **,** [*元组模式元素列表*](#tuple-pattern-element-list)
|
||||
>
|
||||
<a name="tuple-pattern-element"></a>
|
||||
tuple-pattern-element {#tuple-pattern-element}
|
||||
> *元组模式元素* → [*模式*](#pattern)
|
||||
>
|
||||
|
||||
<a name="enumeration_case_pattern"></a>
|
||||
## 枚举用例模式(Enumeration Case Pattern)
|
||||
|
||||
## 枚举用例模式(Enumeration Case Pattern) {#enumeration_case_pattern}
|
||||
*枚举用例模式*匹配现有的某个枚举类型的某个用例。枚举用例模式出现在 `switch` 语句中的 `case` 标签中,以及 `if`、`while`、`guard` 和 `for-in` 语句的 `case` 条件中。
|
||||
|
||||
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../chapter2/08_Enumerations.md#associated_values)。
|
||||
|
||||
> 枚举用例模式语法
|
||||
>
|
||||
<a name="enum-case-pattern"></a>
|
||||
enum-case-pattern {#enum-case-pattern}
|
||||
> *枚举用例模式* → [*类型标识*](./03_Types.md#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](./06_Declarations.md#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
|
||||
>
|
||||
|
||||
<a name="optional_pattern"></a>
|
||||
## 可选模式(Optional Pattern)
|
||||
|
||||
## 可选模式(Optional Pattern) {#optional_pattern}
|
||||
*可选模式*匹配包装在一个 `Optional(Wrapped)` 或者 `ExplicitlyUnwrappedOptional(Wrapped)` 枚举中的 `Some(Wrapped)` 用例中的值。可选模式由一个标识符模式和紧随其后的一个问号组成,可以像枚举用例模式一样使用。
|
||||
|
||||
由于可选模式是 `Optional` 和 `ImplicitlyUnwrappedOptional` 枚举用例模式的语法糖,下面两种写法是等效的:
|
||||
@ -173,13 +163,11 @@ for case let number? in arrayOfOptinalInts {
|
||||
|
||||
> 可选模式语法
|
||||
>
|
||||
<a name="optional-pattern"></a>
|
||||
optional-pattern {#optional-pattern}
|
||||
> *可选模式* → [*标识符模式*](./03_Types.md#type-identifier) **?**
|
||||
>
|
||||
|
||||
<a name="type-casting_patterns"></a>
|
||||
## 类型转换模式(Type-Casting Patterns)
|
||||
|
||||
## 类型转换模式(Type-Casting Patterns) {#type-casting_patterns}
|
||||
有两种类型转换模式,`is` 模式和 `as` 模式。`is` 模式只出现在 `switch` 语句中的 `case` 标签中。`is` 模式和 `as` 模式形式如下:
|
||||
|
||||
> is `类型`
|
||||
@ -195,19 +183,17 @@ for case let number? in arrayOfOptinalInts {
|
||||
|
||||
> 类型转换模式语法
|
||||
>
|
||||
<a name="type-casting-pattern"></a>
|
||||
type-casting-pattern {#type-casting-pattern}
|
||||
> *类型转换模式* → [*is 模式*](#is-pattern) | [*as 模式*](#as-pattern)
|
||||
>
|
||||
<a name="is-pattern"></a>
|
||||
is-pattern {#is-pattern}
|
||||
> *is 模式* → **is** [*类型*](./03_Types.md#type)
|
||||
>
|
||||
<a name="as-pattern"></a>
|
||||
as-pattern {#as-pattern}
|
||||
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.md#type)
|
||||
>
|
||||
|
||||
<a name="expression_pattern"></a>
|
||||
## 表达式模式(Expression Pattern)
|
||||
|
||||
## 表达式模式(Expression Pattern) {#expression_pattern}
|
||||
*表达式模式*代表表达式的值。表达式模式只出现在 `switch` 语句中的 `case` 标签中。
|
||||
|
||||
表达式模式代表的表达式会使用 Swift 标准库中的 `~=` 运算符与输入表达式的值进行比较。如果 `~=` 运算符返回 `true`,则匹配成功。默认情况下,`~=` 运算符使用 `==` 运算符来比较两个相同类型的值。它也可以将一个整型数值与一个 `Range` 实例中的一段整数区间做匹配,正如下面这个例子所示:
|
||||
@ -245,6 +231,6 @@ default:
|
||||
|
||||
> 表达式模式语法
|
||||
>
|
||||
<a name="expression-pattern"></a>
|
||||
expression-pattern {#expression-pattern}
|
||||
> *表达式模式* → [*表达式*](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
@ -4,9 +4,7 @@
|
||||
|
||||
关于 Swift 语言的泛型概述,请参阅 [泛型](../chapter2/22_Generics.md)。
|
||||
|
||||
<a name="generic_parameter"></a>
|
||||
## 泛型形参子句
|
||||
|
||||
## 泛型形参子句 {#generic_parameter}
|
||||
*泛型形参子句*指定泛型类型或函数的类型形参,以及这些参数相关的约束和要求。泛型形参子句用尖括号(`<>`)包住,形式如下:
|
||||
|
||||
> <`泛型形参列表`>
|
||||
@ -38,9 +36,7 @@ simpleMax(17, 42) // T 被推断为 Int 类型
|
||||
simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
```
|
||||
|
||||
<a name="where_clauses"></a>
|
||||
### Where 子句
|
||||
|
||||
### Where 子句 {#where_clauses}
|
||||
要想对类型形参及其关联类型指定额外要求,可以在函数体或者类型的大括号之前添加 `where` 子句。`where` 子句由关键字 `where` 及其后的用逗号分隔的一个或多个要求组成。
|
||||
|
||||
> `where` : `类型要求`
|
||||
@ -60,42 +56,40 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
|
||||
|
||||
> 泛型形参子句语法
|
||||
>
|
||||
<a name="generic-parameter-clause"></a>
|
||||
generic-parameter-clause {#generic-parameter-clause}
|
||||
> *泛型形参子句* → **<** [*泛型形参列表*](#generic-parameter-list) [*约束子句*](#requirement-clause)<sub>可选</sub> **>**
|
||||
>
|
||||
<a name="generic-parameter-list"></a>
|
||||
generic-parameter-list {#generic-parameter-list}
|
||||
> *泛型形参列表* → [*泛形形参*](#generic-parameter) | [*泛形形参*](#generic-parameter) **,** [*泛型形参列表*](#generic-parameter-list)
|
||||
>
|
||||
<a name="generic-parameter"></a>
|
||||
generic-parameter {#generic-parameter}
|
||||
> *泛形形参* → [*类型名称*](./03_Types.md#type-name)
|
||||
>
|
||||
> *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*类型标识符*](./03_Types.md#type-identifier)
|
||||
>
|
||||
> *泛形形参* → [*类型名称*](./03_Types.md#type-name) **:** [*协议合成类型*](./03_Types.md#protocol-composition-type)
|
||||
>
|
||||
> <a name="requirement-clause"></a>
|
||||
> requirement-clause {#requirement-clause}
|
||||
>
|
||||
> *约束子句* → **where** [*约束列表*](#requirement-list)
|
||||
>
|
||||
<a name="requirement-list"></a>
|
||||
requirement-list {#requirement-list}
|
||||
> *约束列表* → [*约束*](#requirement) | [*约束*](#requirement) **,** [*约束列表*](#requirement-list)
|
||||
>
|
||||
<a name="requirement"></a>
|
||||
requirement {#requirement}
|
||||
> *约束* → [*一致性约束*](#conformance-requirement) | [*同类型约束*](#same-type-requirement)
|
||||
>
|
||||
> <a name="conformance-requirement"></a>
|
||||
> conformance-requirement {#conformance-requirement}
|
||||
>
|
||||
> *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*类型标识符*](./03_Types.md#type-identifier)
|
||||
>
|
||||
> *一致性约束* → [*类型标识符*](./03_Types.md#type-identifier) **:** [*协议合成类型*](./03_Types.md#protocol-composition-type)
|
||||
>
|
||||
<a name="same-type-requirement"></a>
|
||||
same-type-requirement {#same-type-requirement}
|
||||
> *同类型约束* → [*类型标识符*](./03_Types.md#type-identifier) **==** [*类型*](./03_Types.md#type)
|
||||
>
|
||||
|
||||
<a name="generic_argument"></a>
|
||||
## 泛型实参子句
|
||||
|
||||
## 泛型实参子句 {#generic_argument}
|
||||
*泛型实参子句*指定泛型类型的类型实参。泛型实参子句用尖括号(`<>`)包住,形式如下:
|
||||
|
||||
> <`泛型实参列表`>
|
||||
@ -122,12 +116,12 @@ let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
|
||||
> 泛型实参子句语法
|
||||
>
|
||||
<a name="generic-argument-clause"></a>
|
||||
generic-argument-clause {#generic-argument-clause}
|
||||
> *泛型实参子句* → **<** [*泛型实参列表*](#generic-argument-list) **>**
|
||||
>
|
||||
<a name="generic-argument-list"></a>
|
||||
generic-argument-list {#generic-argument-list}
|
||||
> *泛型实参列表* → [*泛型实参*](#generic-argument) | [*泛型实参*](#generic-argument) **,** [*泛型实参列表*](#generic-argument-list)
|
||||
>
|
||||
<a name="generic-argument"></a>
|
||||
generic-argument {#generic-argument}
|
||||
> *泛型实参* → [*类型*](./03_Types.md#type)
|
||||
>
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
# 语法总结(Summary of the Grammar)
|
||||
|
||||
<a name="statements"></a>
|
||||
## 语句
|
||||
## 语句 {#statements}
|
||||
|
||||
> 语句语法
|
||||
>
|
||||
@ -243,8 +242,7 @@
|
||||
> *catch-clauses* → **catch** [*模式(pattern)*](TODO) _可选_ [*where-clause*](TODO) _可选_ [*代码块(code-block)*](TODO) _可选_
|
||||
>
|
||||
|
||||
<a name="generic_parameters_and_arguments"></a>
|
||||
## 泛型参数
|
||||
## 泛型参数 {#generic_parameters_and_arguments}
|
||||
|
||||
> 泛型形参从句(Generic Parameter Clause)语法
|
||||
>
|
||||
@ -283,8 +281,7 @@
|
||||
> *泛型参数* → [*类型*](./03_Types.md#type)
|
||||
>
|
||||
|
||||
<a name="declarations"></a>
|
||||
## 声明(Declarations)
|
||||
## 声明(Declarations) {#declarations}
|
||||
|
||||
> 声明语法
|
||||
>
|
||||
@ -666,8 +663,7 @@
|
||||
> *访问级别修改器集* →[*访问级别修改器*](TODO) [*访问级别修改器集*](TODO) _可选_
|
||||
>
|
||||
|
||||
<a name="patterns"></a>
|
||||
## 模式
|
||||
## 模式 {#patterns}
|
||||
|
||||
> 模式(Patterns)语法
|
||||
>
|
||||
@ -751,8 +747,7 @@
|
||||
> *表达式模式* → [*表达式*](./04_Expressions.md#expression)
|
||||
>
|
||||
|
||||
<a name="attributes"></a>
|
||||
## 属性
|
||||
## 属性 {#attributes}
|
||||
|
||||
> 属性语法
|
||||
>
|
||||
@ -777,8 +772,7 @@
|
||||
> *平衡令牌* → **任意标点除了(、)、[、]、{ 或 }**
|
||||
>
|
||||
|
||||
<a name="expressions"></a>
|
||||
## 表达式
|
||||
## 表达式 {#expressions}
|
||||
|
||||
> 表达式语法
|
||||
>
|
||||
@ -1049,8 +1043,7 @@
|
||||
> *可选链表达式* → [*后置表达式*](./04_Expressions.md#postfix_expression) **?**
|
||||
>
|
||||
|
||||
<a name="lexical_structure"></a>
|
||||
## 词法结构
|
||||
## 词法结构 {#lexical_structure}
|
||||
|
||||
> 标识符语法
|
||||
>
|
||||
@ -1277,8 +1270,7 @@
|
||||
> *后置运算符* → [*运算符*](./02_Lexical_Structure.md#operator)
|
||||
>
|
||||
|
||||
<a name="types"></a>
|
||||
## 类型
|
||||
## 类型 {#types}
|
||||
|
||||
> 类型语法
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user