修复一些链接

This commit is contained in:
从今以后
2015-12-04 23:58:24 +08:00
parent 73113d2f2e
commit f5380d3912
4 changed files with 210 additions and 187 deletions

View File

@ -31,10 +31,10 @@ Swift 语言相对较小,这是由于 Swift 代码中的几乎所有常见类
举个例子getter-setter 的语法块的定义如下:
> getter-setter 方法块语法
> *getter-setter 方法块* → { [*getter 子句*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-clause) [*setter 子句*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-clause)<sub>可选</sub> } | { [*setter 子句*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-clause) [*getter 子句*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-clause) }
> *getter-setter 方法块* → { [*getter 子句*](05_Declarations.html#getter-clause) [*setter 子句*](05_Declarations.html#setter-clause)<sub>可选</sub> } | { [*setter 子句*](05_Declarations.html#setter-clause) [*getter 子句*](05_Declarations.html#getter-clause) }
这个定义表明,一个 getter-setter 方法块可以由一个 getter 子句后跟一个可选的 setter 子句构成,然后用大括号括起来,或者由一个 setter 子句后跟一个 getter 子句构成,然后用大括号括起来。下面的两个语法产式等价于上述的语法产式,并明确指出了如何取舍:
> getter-setter 方法块语法
> getter-setter 方法块 → { [*getter 子句*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-clause) [*setter 子句*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-clause)<sub>可选</sub> }
> getter-setter 方法块 → { [*setter 子句*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-clause) [*getter 子句*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-clause) }
> getter-setter 方法块 → { [*getter 子句*](05_Declarations.html#getter-clause) [*setter 子句*](05_Declarations.html#setter-clause)<sub>可选</sub> }
> getter-setter 方法块 → { [*setter 子句*](05_Declarations.html#setter-clause) [*getter 子句*](05_Declarations.html#getter-clause) }

View File

@ -14,44 +14,47 @@
本页包含内容:
- [空白与注释*Whitespace and Comments*](#whitespace_and_comments)
- [标识符*Identifiers*](#identifiers)
- [关键字和标点符号*Keywords and Punctuation*](#keywords)
- [字面量*Literals*](#literals)
- [运算符(*Operators*](#operators)
- [空白与注释](#whitespace_and_comments)
- [标识符](#identifiers)
- [关键字和标点符号](#keywords)
- [字面量](#literals)
- [整数字面量](#integer_literals)
- [浮点数字面量](#floating_point_literals)
- [字符串字面量](#string_literals)
- [运算符](#operators)
Swift 的“词法结构*lexical structure*”描述了能构成该语言中合法符号的字符序列。这些合法符号组成了语言中最底层的构建基块,并在之后的章节中用于描述语言的其他部分。一个合法符号由一个标识符、关键字、标点符号、字面量或运算符组成。
Swift 的“词法结构”描述了能构成该语言中合法符号的字符序列。这些合法符号组成了语言中最底层的构建基块,并在之后的章节中用于描述语言的其他部分。一个合法符号由一个标识符、关键字、标点符号、字面量或运算符组成。
通常情况下,符号是根据随后将介绍的语法约束,由 Swift 源文件的输入文本中提取可能的最长子串生成。这种方法称为“最长匹配*longest match*)”,或者“最大适合”(*maximal munch*
通常情况下,符号是根据随后将介绍的语法约束,由 Swift 源文件的输入文本中提取可能的最长子串生成。这种方法称为“最长匹配”,或者“最大适合”
<a id="whitespace_and_comments"></a>
## 空白与注释
空白*whitespace*有两个用途:分隔源文件中的符号以及帮助区分运算符属于前缀还是后缀(参见 [运算符](#operators)在其他情况下则会被忽略。以下的字符会被当作空白空格U+0020、换行符U+000A、回车符U+000D、水平制表符U+0009、垂直制表符U+000B、换页符U+000C以及空U+0000
空白有两个用途:分隔源文件中的符号以及帮助区分运算符属于前缀还是后缀(参见 [运算符](#operators)在其他情况下则会被忽略。以下的字符会被当作空白空格U+0020、换行符U+000A、回车符U+000D、水平制表符U+0009、垂直制表符U+000B、换页符U+000C以及空U+0000
注释*comments*被编译器当作空白处理。单行注释由 `//` 开始直至遇到换行符U+000A或者回车符U+000D。多行注释由 `/*` 开始,以 `*/` 结束。注释允许嵌套,但注释标记必须匹配。
注释被编译器当作空白处理。单行注释由 `//` 开始直至遇到换行符U+000A或者回车符U+000D。多行注释由 `/*` 开始,以 `*/` 结束。注释允许嵌套,但注释标记必须匹配。
正如 [标记格式参考(Markup Formatting Reference](https://developer.apple.com/library/prerelease/ios/documentation/Xcode/Reference/xcode_markup_formatting_ref/index.html#//apple_ref/doc/uid/TP40016497) 所述,注释可以包含附加的格式和标记。
正如 [*Markup Formatting Reference*](https://developer.apple.com/library/prerelease/ios/documentation/Xcode/Reference/xcode_markup_formatting_ref/index.html#//apple_ref/doc/uid/TP40016497) 所述,注释可以包含附加的格式和标记。
<a id="identifiers"></a>
## 标识符
标识符*identifiers*可以由以下的字符开始:大写或小写的字母 `A``Z`、下划线 `_`、基本多文种平面*Basic Multilingual Plane*中的 Unicode 非组合字符以及基本多文种平面以外的非个人专用区*Private Use Area*字符。首字符之后,允许使用数字和 Unicode 字符组合。
标识符可以由以下的字符开始:大写或小写的字母 `A``Z`、下划线 `_`、基本多文种平面中的 Unicode 非组合字符以及基本多文种平面以外的非个人专用区字符。首字符之后,允许使用数字和 Unicode 字符组合。
使用保留字*reserved word*作为标识符,需要在其前后增加反引号 `` ` ``。例如,`class` 不是合法的标识符,但可以使用 `` `class` ``。反引号不属于标识符的一部分,`` `x` `` 和 `x` 表示同一标识符。
使用保留字作为标识符,需要在其前后增加反引号 `` ` ``。例如,`class` 不是合法的标识符,但可以使用 `` `class` ``。反引号不属于标识符的一部分,`` `x` `` 和 `x` 表示同一标识符。
闭包*closure*中如果没有明确指定参数名称,参数将被隐式命名为 `$0`、`$1`、`$2` 等等。这些命名在闭包作用域范围内是合法的标识符。
闭包中如果没有明确指定参数名称,参数将被隐式命名为 `$0`、`$1`、`$2` 等等。这些命名在闭包作用域范围内是合法的标识符。
> 标识符语法
<a id="identifier"></a>
> *标识符* → [*头部标识符*](#identifier_head) [*标识符字符组*](#identifier_characters)<sub>可选</sub>
> *标识符* → \`[*头部标识符*](#identifier_head) [*标识符字符组*](#identifier_characters)<sub>可选</sub>\`
> *标识符* → [*隐式参数名*](#implicit_parameter_name)
<a id="identifier_list"></a>
> *标识符列表* → [*标识符*](#identifier) | [*标识符*](#identifier) **,** [*标识符列表*](#identifier_list)
> *标识符* → [*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub>
> *标识符* → \`[*头部标识符*](#identifier-head) [*标识符字符组*](#identifier-characters)<sub>可选</sub>\`
> *标识符* → [*隐式参数名*](#implicit-parameter-name)
<a id="identifier-list"></a>
> *标识符列表* → [*标识符*](#identifier) | [*标识符*](#identifier) **,** [*标识符列表*](#identifier-list)
<a id="identifier_head"></a>
<a id="identifier-head"></a>
> *头部标识符* → 大写或小写字母 A - Z
> *头部标识符* → _
> *头部标识符* → U+00A8U+00AAU+00ADU+00AFU+00B2U+00B5或者 U+00B7U+00BA
@ -69,28 +72,28 @@ Swift 的“词法结构(*lexical structure*)”描述了能构成该语言
> *头部标识符* → U+90000U+9FFFDU+A0000U+AFFFDU+B0000U+BFFFD或者 U+C0000U+CFFFD
> *头部标识符* → U+D0000U+DFFFD 或者 U+E0000U+EFFFD
<a id="identifier_character"></a>
<a id="identifier-character"></a>
> *标识符字符* → 数值 0 - 9
> *标识符字符* → U+0300U+036FU+1DC0U+1DFFU+20D0U+20FF或者 U+FE20U+FE2F
> *标识符字符* → [*头部标识符*](#identifier_head)
<a id="identifier_characters"></a>
> *标识符字符组* → [*标识符字符*](#identifier_character) [*标识符字符组*](#identifier_characters)<sub>可选</sub>
> *标识符字符* → [*头部标识符*](#identifier-head)
<a id="identifier-characters"></a>
> *标识符字符组* → [*标识符字符*](#identifier-character) [*标识符字符组*](#identifier-characters)<sub>可选</sub>
<a id="implicit_parameter_name"></a>
> *隐式参数名* → **$** [*十进制数字列表*](#decimal_digits)
<a id="implicit-parameter-name"></a>
> *隐式参数名* → **$** [*十进制数字列表*](#decimal-digits)
<a id="keywords"></a>
## 关键字和标点符号
下面这些被保留的关键字*keywords*不允许用作标识符,除非被反引号转义,具体描述请参考 [标识符](#identifiers)。
下面这些被保留的关键字不允许用作标识符,除非被反引号转义,具体描述请参考 [标识符](#identifiers)。
* 用在声明中的关键字:`class``deinit``enum``extension``func``import``init``inout``internal``let``operator``private``protocol``public``static``struct``subscript``typealias``var`
* 用在语句中的关键字:`break``case``continue``default``defer``do``else``fallthrough``for``guard``if``in``repeat``return``switch``where``while`
* 用在表达式和类型中的关键字:`as``catch``dynamicType``false``is``nil``rethrows``super``self``Self``throw``throws``true``try``__COLUMN__``__FILE__``__FUNCTION__``__LINE__`
* 用在声明中的关键字: `class``deinit``enum``extension``func``import``init``inout``internal``let``operator``private``protocol``public``static``struct``subscript``typealias``var`
* 用在语句中的关键字: `break``case``continue``default``defer``do``else``fallthrough``for``guard``if``in``repeat``return``switch``where``while`
* 用在表达式和类型中的关键字: `as``catch``dynamicType``false``is``nil``rethrows``super``self``Self``throw``throws``true``try``__COLUMN__``__FILE__``__FUNCTION__``__LINE__`
* 用在模式中的关键字:`_`
* 特定上下文中被保留的关键字:`associativity``convenience``dynamic``didSet``final``get``infix``indirect``lazy``left``mutating``none``nonmutating``optional``override``postfix``precedence``prefix``Protocol``required``right``set``Type``unowned``weak``willSet`,这些关键字在特定上下文之外可以被用做标识符。
* 特定上下文中被保留的关键字: `associativity``convenience``dynamic``didSet``final``get``infix``indirect``lazy``left``mutating``none``nonmutating``optional``override``postfix``precedence``prefix``Protocol``required``right``set``Type``unowned``weak``willSet`,这些关键字在特定上下文之外可以被用做标识符。
以下符号被当作保留符号,不能用于自定义运算符:`(``)``{``}``[``]``.``,``:``;``=``@``#``&`(作为前缀运算符)、`->`、`` ` ``、`?`、`!`(作为后缀运算符)。
以下符号被当作保留符号,不能用于自定义运算符: `(``)``{``}``[``]``.``,``:``;``=``@``#``&`(作为前缀运算符)、`->`、`` ` ``、`?`、`!`(作为后缀运算符)。
<a id="literals"></a>
## 字面量
@ -100,132 +103,136 @@ Swift 的“词法结构(*lexical structure*)”描述了能构成该语言
下面是字面量的一些示例:
```swift
42 // 整字面量
3.14159 // 浮点字面量
"Hello, world!" // 字符串字面量
true // 布尔字面量
42 // 整字面量
3.14159 // 浮点字面量
"Hello, world!" // 字符串字面量
true // 布尔字面量
```
字面量本身并不包含类型信息。事实上,一个字面量会被解析为拥有无限的精度,然后 Swift 的类型推导会尝试去推导出这个字面量的类型。比如,在 `let x: Int8 = 42` 这个声明中Swift 使用了显式类型标注(`: Int8`)来推导出 `42` 这个整字面量的类型是 `Int8`。如果没有可用的类型信息, Swift 则会从标准库中定义的字面量类型中推导出一个默认的类型。整字面量的默认类型是 `Int`,浮点字面量的默认类型是 `Double`,字符串字面量的默认类型是 `String`,布尔字面量的默认类型是 `Bool`。比如,在 `let str = "Hello, world"` 这个声明中,字符串 `"Hello, world"` 的默认推导类型就是 `String`。
字面量本身并不包含类型信息。事实上,一个字面量会被解析为拥有无限的精度,然后 Swift 的类型推导会尝试去推导出这个字面量的类型。比如,在 `let x: Int8 = 42` 这个声明中Swift 使用了显式类型标注(`: Int8`)来推导出 `42` 这个整字面量的类型是 `Int8`。如果没有可用的类型信息, Swift 则会从标准库中定义的字面量类型中推导出一个默认的类型。整字面量的默认类型是 `Int`,浮点字面量的默认类型是 `Double`,字符串字面量的默认类型是 `String`,布尔字面量的默认类型是 `Bool`。比如,在 `let str = "Hello, world"` 这个声明中,字符串 `"Hello, world"` 的默认推导类型就是 `String`。
当为一个字面量值指定了类型标注的时候,这个标注的类型必须能通过这个字面量值实例化。也就是说,这个类型必须符合这些 Swift 标准库协议中的一个:整字面量的 `IntegerLiteralConvertible` 协议、浮点字面量的 `FloatingPointLiteralConvertible` 协议、字符串字面量的 `StringLiteralConvertible` 协议以及布尔字面量的 `BooleanLiteralConvertible` 协议。比如,`Int8` 符合 `IntegerLiteralConvertible` 协议,因此它能在 `let x: Int8 = 42` 这个声明中作为整字面量 `42` 的类型标注。
当为一个字面量值指定了类型标注的时候,这个标注的类型必须能通过这个字面量值实例化。也就是说,这个类型必须符合这些 Swift 标准库协议中的一个:整字面量的 `IntegerLiteralConvertible` 协议、浮点字面量的 `FloatingPointLiteralConvertible` 协议、字符串字面量的 `StringLiteralConvertible` 协议以及布尔字面量的 `BooleanLiteralConvertible` 协议。比如,`Int8` 符合 `IntegerLiteralConvertible` 协议,因此它能在 `let x: Int8 = 42` 这个声明中作为整字面量 `42` 的类型标注。
> 字面量语法
> *字面量* → [*数字型字面量*](#numeric_literal) | [*字符串字面量*](#string_literal) | [*布尔字面量*](#boolean_literal) | [*nil字面量*](#nil_literal)
> *字面量* → [*数字面量*](#numeric-literal) | [*字符串字面量*](#string-literal) | [*布尔字面量*](#boolean-literal) | [*nil 字面量*](#nil-literal)
<a id="numeric_literal"></a>
> *数字型字面量* → **-**<sub>可选</sub> [*整字面量*](#integer_literal) | **-**<sub>可选</sub> [*浮点字面量*](#floating_point_literal)
<a id="boolean_literal"></a>
> *布尔字面量* → **true** | **false**
<a id="nil_literal"></a>
> *nil字面量* → **nil**
<a id="numeric-literal"></a>
> *数字面量* → **-**<sub>可选</sub> [*整字面量*](#integer-literal) | **-**<sub>可选</sub> [*浮点字面量*](#floating-point-literal)
<a id="boolean-literal"></a>
> *布尔字面量* → **true** | **false**
<a id="nil-literal"></a>
> *nil 字面量* → **nil**
### 整型字面量
<a id="integer_literals"></a>
### 整数字面量
字面量*integer literals*表示未指定精度整数的值。整字面量默认用十进制表示,可以加前缀来指定其他的进制,二进制字面量加 `0b`,八进制字面量加 `0o`,十六进制字面量加 `0x`。
字面量表示未指定精度整数的值。整字面量默认用十进制表示,可以加前缀来指定其他的进制,二进制字面量加 `0b`,八进制字面量加 `0o`,十六进制字面量加 `0x`。
十进制字面量包含数字 `0` 至 `9`。二进制字面量只包含 `0` 或 `1`,八进制字面量包含数字 `0` 至 `7`,十六进制字面量包含数字 `0` 至 `9` 以及字母 `A` 至 `F`(大小写均可)。
负整数的字面量在整字面量前加负号 `-`,比如 `-42`。
负整数的字面量在整字面量前加负号 `-`,比如 `-42`。
整型字面面可以使用下划线 `_` 来增加数字的可读性,下划线会被系统忽略,因此不会影响字面量的值。同样地,也可以在数字前加 `0`,并不会影响字面量的值。
除非特别指定,整字面量的默认推导类型为 Swift 标准库类型中的 `Int`。Swift 标准库还定义了其他不同长度以及是否带符号的整数类型,请参考 [整数类型](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID323)。
除非特别指定,整字面量的默认推导类型为 Swift 标准库类型中的 `Int`。Swift 标准库还定义了其他不同长度以及是否带符号的整数类型,请参考 [整数](../chapter2/01_The_Basics.html#integers)。
> 整字面量语法
> 整字面量语法
<a id="integer_literal"></a>
> *整字面量* → [*二进制字面量*](#binary_literal)
> *整字面量* → [*八进制字面量*](#octal_literal)
> *整字面量* → [*十进制字面量*](#decimal_literal)
> *整字面量* → [*十六进制字面量*](#hexadecimal_literal)
<a id="integer-literal"></a>
> *整字面量* → [*二进制字面量*](#binary-literal)
> *整字面量* → [*八进制字面量*](#octal-literal)
> *整字面量* → [*十进制字面量*](#decimal-literal)
> *整字面量* → [*十六进制字面量*](#hexadecimal-literal)
<a id="binary_literal"></a>
> *二进制字面量* → **0b** [*二进制数字*](#binary_digit) [*二进制字面量字符组*](#binary_literal_characters)<sub>可选</sub> <a id="binary_digit"></a>
<a id="binary-literal"></a>
> *二进制字面量* → **0b** [*二进制数字*](#binary-digit) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
> <a id="binary-digit"></a>
> *二进制数字* → 数值 0 到 1
<a id="binary_literal_character"></a>
> *二进制字面量字符* → [*二进制数字*](#binary_digit) | _
<a id="binary_literal_characters"></a>
> *二进制字面量字符组* → [*二进制字面量字符*](#binary_literal_character) [*二进制字面量字符组*](#binary_literal_characters)<sub>可选</sub>
<a id="binary-literal-character"></a>
> *二进制字面量字符* → [*二进制数字*](#binary-digit) | _
<a id="binary-literal-characters"></a>
> *二进制字面量字符组* → [*二进制字面量字符*](#binary-literal-character) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
<a id="octal_literal"></a>
> *八进制字面量* → **0o** [*八进字数字*](#octal_digit) [*八进制字符组*](#octal_literal_characters)<sub>可选</sub>
<a id="octal_digit"></a>
<a id="octal-literal"></a>
> *八进制字面量* → **0o** [*八进字数字*](#octal-digit) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
<a id="octal-digit"></a>
> *八进字数字* → 数值 0 到 7
<a id="octal_literal_character"></a>
> *八进制字符* → [*八进字数字*](#octal_digit) | _
<a id="octal_literal_characters"></a>
> *八进制字符组* → [*八进制字符*](#octal_literal_character) [*八进制字符组*](#octal_literal_characters)<sub>可选</sub>
<a id="octal-literal-character"></a>
> *八进制字符* → [*八进字数字*](#octal-digit) | _
<a id="octal-literal-characters"></a>
> *八进制字符组* → [*八进制字符*](#octal-literal-character) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
<a id="decimal_literal"></a>
> *十进制字面量* → [*十进制数字*](#decimal_digit) [*十进制字符组*](#decimal_literal_characters)<sub>可选</sub>
<a id="decimal_digit"></a>
<a id="decimal-literal"></a>
> *十进制字面量* → [*十进制数字*](#decimal-digit) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
<a id="decimal-digit"></a>
> *十进制数字* → 数值 0 到 9
<a id="decimal_digits"></a>
> *十进制数字组* → [*十进制数字*](#decimal_digit) [*十进制数字组*](#decimal_digits)<sub>可选</sub>
<a id="decimal_literal_character"></a>
> *十进制字符* → [*十进制数字*](#decimal_digit) | _
<a id="decimal_literal_characters"></a>
> *十进制字符组* → [*十进制字符*](#decimal_literal_character) [*十进制字符组*](#decimal_literal_characters)<sub>可选</sub>
<a id="decimal-digits"></a>
> *十进制数字组* → [*十进制数字*](#decimal-digit) [*十进制数字组*](#decimal-digits)<sub>可选</sub>
<a id="decimal-literal-character"></a>
> *十进制字符* → [*十进制数字*](#decimal-digit) | _
<a id="decimal-literal-characters"></a>
> *十进制字符组* → [*十进制字符*](#decimal-literal-character) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
<a id="hexadecimal_literal"></a>
> *十六进制字面量* → **0x** [*十六进制数字*](#hexadecimal_digit) [*十六进制字面量字符组*](#hexadecimal_literal_characters)<sub>可选</sub>
<a id="hexadecimal_digit"></a>
<a id="hexadecimal-literal"></a>
> *十六进制字面量* → **0x** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
<a id="hexadecimal-digit"></a>
> *十六进制数字* → 数值 0 到 9, 字母 a 到 f, 或 A 到 F
<a id="hexadecimal_literal_character"></a>
> *十六进制字符* → [*十六进制数字*](#hexadecimal_digit) | _
<a id="hexadecimal_literal_characters"></a>
> *十六进制字面量字符组* → [*十六进制字符*](#hexadecimal_literal_character) [*十六进制字面量字符组*](#hexadecimal_literal_characters)<sub>可选</sub>
<a id="hexadecimal-literal-character"></a>
> *十六进制字符* → [*十六进制数字*](#hexadecimal-digit) | _
<a id="hexadecimal-literal-characters"></a>
> *十六进制字面量字符组* → [*十六进制字符*](#hexadecimal-literal-character) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
### 浮点型字面量
<a id="floating_point_literals"></a>
### 浮点数字面量
浮点字面量*floating-point literals*表示未指定精度浮点数的值。
浮点字面量表示未指定精度浮点数的值。
浮点字面量默认用十进制表示(无前缀),也可以用十六进制表示(加前缀 `0x`)。
浮点字面量默认用十进制表示(无前缀),也可以用十六进制表示(加前缀 `0x`)。
十进制浮点字面量由十进制数字串后跟小数部分或指数部分(或两者皆有)组成。十进制小数部分由小数点 `.` 后跟十进制数字串组成。指数部分由大写或小写字母 `e` 为前缀后跟十进制数字串组成,这串数字表示 `e` 之前的数量乘以 10 的几次方。例如:`1.25e2` 表示 `1.25 ⨉ 10^2`,也就是 `125.0`;同样,`1.25e2` 表示 `1.25 ⨉ 10^2`,也就是 `0.0125`。
十进制浮点字面量由十进制数字串后跟小数部分或指数部分(或两者皆有)组成。十进制小数部分由小数点 `.` 后跟十进制数字串组成。指数部分由大写或小写字母 `e` 为前缀后跟十进制数字串组成,这串数字表示 `e` 之前的数量乘以 10 的几次方。例如:`1.25e2` 表示 `1.25 ⨉ 10^2`,也就是 `125.0`;同样,`1.25e2` 表示 `1.25 ⨉ 10^2`,也就是 `0.0125`。
十六进制浮点字面量由前缀 `0x` 后跟可选的十六进制小数部分以及十六进制指数部分组成。十六进制小数部分由小数点后跟十六进制数字串组成。指数部分由大写或小写字母 `p` 为前缀后跟十进制数字串组成,这串数字表示 `p` 之前的数量乘以 2 的几次方。例如:`0xFp2` 表示 `15 ⨉ 2^2`,也就是 `60`;同样,`0xFp-2` 表示 `15 ⨉ 2^-2`,也就是 `3.75`。
十六进制浮点字面量由前缀 `0x` 后跟可选的十六进制小数部分以及十六进制指数部分组成。十六进制小数部分由小数点后跟十六进制数字串组成。指数部分由大写或小写字母 `p` 为前缀后跟十进制数字串组成,这串数字表示 `p` 之前的数量乘以 2 的几次方。例如:`0xFp2` 表示 `15 ⨉ 2^2`,也就是 `60`;同样,`0xFp-2` 表示 `15 ⨉ 2^-2`,也就是 `3.75`。
负数的浮点字面量由负号 `-` 和浮点字面量组成,例如 `-42.5`。
负数的浮点字面量由负号 `-` 和浮点字面量组成,例如 `-42.5`。
浮点字面量允许使用下划线 `_` 来增强数字的可读性,下划线会被系统忽略,因此不会影响字面量的值。同样地,也可以在数字前加 `0`,并不会影响字面量的值。
浮点字面量允许使用下划线 `_` 来增强数字的可读性,下划线会被系统忽略,因此不会影响字面量的值。同样地,也可以在数字前加 `0`,并不会影响字面量的值。
除非特别指定,浮点字面量的默认推导类型为 Swift 标准库类型中的 `Double`,表示 64 位浮点数。Swift 标准库也定义了 `Float` 类型,表示 32 位浮点数。
除非特别指定,浮点字面量的默认推导类型为 Swift 标准库类型中的 `Double`,表示 64 位浮点数。Swift 标准库也定义了 `Float` 类型,表示 32 位浮点数。
> 浮点字面量语法
> 浮点字面量语法
<a id="floating_point_literal"></a>
> *浮点数字面量* → [*十进制字面量*](#decimal_literal) [*十进制分数*](#decimal_fraction)<sub>可选</sub> [*十进制指数*](#decimal_exponent)<sub>可选</sub>
> *浮点数字面量* → [*十六进制字面量*](#hexadecimal_literal) [*十六进制分数*](#hexadecimal_fraction)<sub>可选</sub> [*十六进制指数*](#hexadecimal_exponent)
<a id="floating-point-literal"></a>
> *浮点数字面量* → [*十进制字面量*](#decimal-literal) [*十进制分数*](#decimal-fraction)<sub>可选</sub> [*十进制指数*](#decimal-exponent)<sub>可选</sub>
> *浮点数字面量* → [*十六进制字面量*](#hexadecimal-literal) [*十六进制分数*](#hexadecimal-fraction)<sub>可选</sub> [*十六进制指数*](#hexadecimal-exponent)
<a id="decimal_fraction"></a>
> *十进制分数* → **.** [*十进制字面量*](#decimal_literal)
<a id="decimal_exponent"></a>
> *十进制指数* → [*十进制指数 e*](#floating_point_e) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal_literal)
<a id="decimal-fraction"></a>
> *十进制分数* → **.** [*十进制字面量*](#decimal-literal)
<a id="decimal-exponent"></a>
> *十进制指数* → [*十进制指数 e*](#floating-point-e) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal)
<a id="hexadecimal_fraction"></a>
> *十六进制分数* → **.** [*十六进制数字*](#hexadecimal_digit) [*十六进制字面量字符组*](#hexadecimal_literal_characters)<sub>可选</sub>
<a id="hexadecimal_exponent"></a>
> *十六进制指数* → [*十六进制指数 p*](#floating_point_p) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal_literal)
<a id="hexadecimal-fraction"></a>
> *十六进制分数* → **.** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
<a id="hexadecimal-exponent"></a>
> *十六进制指数* → [*十六进制指数 p*](#floating-point-p) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal)
<a id="floating_point_e"></a>
<a id="floating-point-e"></a>
> *十进制指数 e* → **e** | **E**
<a id="floating_point_p"></a>
<a id="floating-point-p"></a>
> *十六进制指数 p* → **p** | **P**
<a id="sign"></a>
> *正负号* → **+** | **-**
### 字符串型字面量
<a id="string_literals"></a>
### 字符串字面量
字符串字面量*string literal*由被包在双引号中的一串字符组成,形式如下:
字符串字面量由被包在双引号中的一串字符组成,形式如下:
```swift
"characters"
```
字符串字面量中不能包含未转义的双引号(`"`)、未转义的反斜线(`\`)、回车符、换行符。
字符串字面量中不能包含未转义的双引号(`"`)、未转义的反斜线(`\`)、回车符、换行符。
可以在字符串字面量中使用的转义特殊符号如下:
@ -250,7 +257,7 @@ true // 布尔型字面量
let x = 3; "1 2 \(x)"
```
字符串字面量的默认推导类型为 `String`。更多有关 `String` 类型的信息请参考 [字符串和字符](../chapter2/03_Strings_and_Characters.html) 以及 [字符串结构体参考](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_String_Structure/index.html#//apple_ref/doc/uid/TP40015181)。
字符串字面量的默认推导类型为 `String`。更多有关 `String` 类型的信息请参考 [字符串和字符](../chapter2/03_Strings_and_Characters.html) 以及 [*String Structure Reference*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_String_Structure/index.html#//apple_ref/doc/uid/TP40015181)。
`` 操作符连接的字符型字面量是在编译时进行连接的。比如下面的 `textA``textB` 是完全一样的,`textA` 没有任何运行时的连接操作。
@ -259,30 +266,30 @@ let textA = "Hello " + "world"
let textB = "Hello world"
```
> 字符串字面量语法
> 字符串字面量语法
<a id="string_literal"></a>
> *字符串字面量* → [*静态字符串字面量*](#static_string_literal) | [*插值字符串字面量*](#interpolated_string_literal)
<a id="string-literal"></a>
> *字符串字面量* → [*静态字符串字面量*](#static-string-literal) | [*插值字符串字面量*](#interpolated-string-literal)
<a id="static_string_literal"></a>
> *静态字符串字面量* → **"**[*引用文本*](#quoted_text)<sub>可选</sub>**"**
<a id="quoted_text"></a>
> *引用文本* → [*引用文本项*](#quoted_text_item) [*引用文本*](#quoted_text)<sub>可选</sub>
<a id="quoted_text_item"></a>
> *引用文本项* → [*转义字符*](#escaped_character)
<a id="static-string-literal"></a>
> *静态字符串字面量* → **"**[*引用文本*](#quoted-text)<sub>可选</sub>**"**
<a id="quoted-text"></a>
> *引用文本* → [*引用文本项*](#quoted-text-item) [*引用文本*](#quoted-text)<sub>可选</sub>
<a id="quoted-text-item"></a>
> *引用文本项* → [*转义字符*](#escaped-character)
> *引用文本项* → 除了 **"**、**\\**、U+000A、U+000D 以外的所有 Unicode 字符
<a id="interpolated_string_literal"></a>
> *插值字符串字面量* → **"**[*插值文本*](#interpolated_text)<sub>可选</sub>**"**
<a id="interpolated_text"></a>
> *插值文本* → [*插值文本项*](#interpolated_text_item) [*插值文本*](#interpolated_text)<sub>可选</sub>
<a id="interpolated_text_item"></a>
> *插值文本项* → **\\****(**[*表达式*](./04_Expressions.html)**)** | [*引用文本项*](#quoted_text_item)
<a id="interpolated-string-literal"></a>
> *插值字符串字面量* → **"**[*插值文本*](#interpolated-text)<sub>可选</sub>**"**
<a id="interpolated-text"></a>
> *插值文本* → [*插值文本项*](#interpolated-text-item) [*插值文本*](#interpolated-text)<sub>可选</sub>
<a id="interpolated-text-item"></a>
> *插值文本项* → **\\****(**[*表达式*](./04_Expressions.html)**)** | [*引用文本项*](#quoted-text-item)
<a id="escaped_character"></a>
<a id="escaped-character"></a>
> *转义字符* → **\\****0** | **\\****\\** | **\t** | **\n** | **\r** | **\\"** | **\\'**
> *转义字符* → **\u {** [*unicode 标量数字*](#unicode_scalar_digits) **}**
<a id="unicode_scalar_digits"></a>
> *转义字符* → **\u {** [*unicode 标量数字*](#unicode-scalar-digits) **}**
<a id="unicode-scalar-digits"></a>
> *unicode 标量数字* → 一到八位的十六进制数字
<a id="operators"></a>
@ -308,15 +315,15 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
在某些特定的设计中 ,以 `<``>` 开头的运算符会被分离成两个或多个符号,剩余部分可能会以同样的方式被再次分离。因此,在 `Dictionary<String, Array<Int>>` 中没有必要添加空白来消除闭合字符 `>` 的歧义。在这个例子中, 闭合字符 `>` 不会被视为单独的符号,因而不会被错误解析为 `>>` 运算符。
要学习如何自定义运算符,请参考 [自定义运算符](../chapter2/25_Advanced_Operators.html#custom_operators) 和 [运算符声明](./05_Declarations.html#operator_declaration)。要学习如何重载运算符,请参考 [运算符方法](../chapter2/25_Advanced_Operators.html#operator_functions)。
要学习如何自定义运算符,请参考 [自定义运算符](../chapter2/25_Advanced_Operators.html#custom_operators) 和 [运算符声明](05_Declarations.html#operator_declaration)。要学习如何重载运算符,请参考 [运算符函数](../chapter2/25_Advanced_Operators.html#operator_functions)。
> 运算符语法
<a id="operator"></a>
> *运算符* → [*头部运算符*](#operator_head) [*运算符字符组*](#operator_characters)<sub>可选</sub>
> *运算符* → [*头部点运算符*](#dot_operator_head) [*点运算符字符组*](#dot_operator_characters)<sub>可选</sub>
> *运算符* → [*头部运算符*](#operator-head) [*运算符字符组*](#operator-characters)<sub>可选</sub>
> *运算符* → [*头部点运算符*](#dot-operator-head) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub>
<a id="operator_head"></a>
<a id="operator-head"></a>
> *头部运算符* → **/** | **=** | **-** | **+** | **!** | __*__ | **%** | **<** | **>** | **&** | **|** | **^** | **~** | **?**
> *头部运算符* → U+00A1U+00A7
> *头部运算符* → U+00A9 或 U+00AB
@ -333,24 +340,27 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
> *头部运算符* → U+3001U+3003
> *头部运算符* → U+3008U+3030
<a id="operator_character"></a>
> *运算符字符* → [*头部运算符*](#operator_head)
<a id="operator-character"></a>
> *运算符字符* → [*头部运算符*](#operator-head)
> *运算符字符* → U+0300U+036F
> *运算符字符* → U+1DC0U+1DFF
> *运算符字符* → U+20D0U+20FF
> *运算符字符* → U+FE00U+FE0F
> *运算符字符* → U+FE20U+FE2F
> *运算符字符* → U+E0100U+E01EF
<a id="operator_characters"></a>
> *运算符字符组* → [*运算符字符*](#operator_character) [*运算符字符组*](#operator_characters)<sub>可选</sub>
<a id="operator-characters"></a>
> *运算符字符组* → [*运算符字符*](#operator-character) [*运算符字符组*](#operator-characters)<sub>可选</sub>
<a id="dot_operator_head"></a>
<a id="dot-operator-head"></a>
> *头部点运算符* → **..**
<a id="dot_operator_character"></a>
> *点运算符字符* → **.** | [*运算符字符*](#operator_character)
<a id="dot_operator_characters"></a>
> *点运算符字符组* → [*点运算符字符*](#dot_operator_character) [*点运算符字符组*](#dot_operator_characters)<sub>可选</sub>
<a id="dot-operator-character"></a>
> *点运算符字符* → **.** | [*运算符字符*](#operator-character)
<a id="dot-operator-characters"></a>
> *点运算符字符组* → [*点运算符字符*](#dot-operator-character) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub>
<a id="binary-operator"></a>
> *二元运算符* → [*运算符*](#operator)
> *前置运算符* → [*运算符*](#operator)
> *后置运算符* → [*运算符*](#operator)
<a id="prefix-operator"></a>
> *前缀运算符* → [*运算符*](#operator)
<a id="postfix-operator"></a>
> *后缀运算符* → [*运算符*](#operator)

View File

@ -13,22 +13,22 @@
本页包含内容:
- [类型注解Type Annotation](#type_annotation)
- [类型标识符Type Identifier](#type_identifier)
- [元组类型Tuple Type](#tuple_type)
- [函数类型Function Type](#function_type)
- [数组类型Array Type](#array_type)
- [字典类型Dictionary Type](#dictionary_type)
- [可选类型Optional Type](#optional_type)
- [隐式解析可选类型Implicitly Unwrapped Optional Type](#implicitly_unwrapped_optional_type)
- [协议合成类型Protocol Composition Type](#protocol_composition_type)
- [元类型Metatype Type](#metatype_type)
- [类型继承子句Type Inheritance Clause](#type_inheritance_clause)
- [类型推断Type Inference](#type_inference)
- [类型注解](#type_annotation)
- [类型标识符](#type_identifier)
- [元组类型](#tuple_type)
- [函数类型](#function_type)
- [数组类型](#array_type)
- [字典类型](#dictionary_type)
- [可选类型](#optional_type)
- [隐式解析可选类型](#implicitly_unwrapped_optional_type)
- [协议合成类型](#protocol_composition_type)
- [元类型](#metatype_type)
- [类型继承子句](#type_inheritance_clause)
- [类型推断](#type_inference)
Swift 语言存在两种类型:命名型类型和复合型类型。命名型类型是指定义时可以给定名字的类型。命名型类型包括类、结构体、枚举和协议。比如,一个用户定义的类 MyClass 的实例拥有类型 MyClass。除了用户定义的命名型类型Swift 标准库也定义了很多常用的命名型类型,包括那些表示数组、字典和可选值的类型。
那些通常被其它语言认为是基本或原始的数据型类型,比如表示数字、字符和字符串的类型,实际上就是命名型类型,这些类型在 Swift 标准库中是使用结构体来定义和实现的。因为它们是命名型类型,因此你可以按照“扩展和扩展声明”章节里讨论的那样,声明一个扩展来增加它们的行为以迎合你程序的需求。
那些通常被其它语言认为是基本或原始的数据型类型,比如表示数字、字符和字符串的类型,实际上就是命名型类型,这些类型在 Swift 标准库中是使用结构体来定义和实现的。因为它们是命名型类型,因此你可以按照 [扩展](../chapter2/21_Extensions.html) 和 [扩展声明](05_Declarations.html#extension_declaration) 中讨论的那样,声明一个扩展来增加它们的行为以满足你程序的需求。
复合型类型是没有名字的类型,它由 Swift 本身定义。Swift 存在两种复合型类型:函数类型和元组类型。一个复合型类型可以包含命名型类型和其它复合型类型。例如,元组类型 `(Int, (Int, Int))` 包含两个元素:第一个是命名型类型 `Int`,第二个是另一个复合型类型 `(Int, Int)`
@ -36,7 +36,7 @@ Swift 语言存在两种类型:命名型类型和复合型类型。命名型
> 类型语法
<a name="type"></a>
> *类型* → [*数组类型*](#array_type) | [*字典类型*](#dictionary_type) | [*函数类型*](#function_type) | [*类型标识*](#type_identifier) | [*元组类型*](#tuple_type) | [*可选类型*](#optional_type) | [*隐式解析可选类型*](#implicitly_unwrapped_optional_type) | [*协议合成类型*](#protocol_composition_type) | [*元型类型*](#metatype_type)
> *类型* → [*数组类型*](#array-type) | [*字典类型*](#dictionary-type) | [*函数类型*](#function-type) | [*类型标识*](#type-identifier) | [*元组类型*](#tuple-type) | [*可选类型*](#optional-type) | [*隐式解析可选类型*](#implicitly-unwrapped-optional-type) | [*协议合成类型*](#protocol-composition-type) | [*元型类型*](#metatype-type)
<a name="type_annotation"></a>
## 类型注解
@ -52,7 +52,8 @@ func someFunction(a: Int) { /* ... */ }
类型注解可以在类型之前包含一个类型特性的可选列表。
> 类型注解语法
> *类型注解* → **:** [*特性列表*](../chapter3/06_Attributes.html#attributes)<sub>可选</sub> [*类型*](#type)
<a name="type-annotation"></a>
> *类型注解* → **:** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](#type)
<a name="type_identifier"></a>
## 类型标识符
@ -75,9 +76,10 @@ var someValue: ExampleModule.MyType
```
> 类型标识符语法
> *类型标识符* → [*类型名称*](#type_name) [*泛型参数子句*](GenericParametersAndArguments.html#generic_argument_clause)<sub>可选</sub> | [*类型名称*](#type_name) [*泛型参数子句*](GenericParametersAndArguments.html#generic_argument_clause)<sub>可选</sub> **.** [*类型标识符*](#type_identifier)
<a name="type_name"></a>
> *类型名称* → [*标识符*](LexicalStructure.html#identifier)
<a name="type-identifier"></a>
> *类型标识符* → [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.html#generic_argument_clause)<sub>可选</sub> | [*类型名称*](#type-name) [*泛型参数子句*](08_Generic_Parameters_and_Arguments.html#generic_argument_clause)<sub>可选</sub> **.** [*类型标识符*](#type-identifier)
<a name="type-name"></a>
> *类型名称* → [*标识符*](02_Lexical_Structure.html#identifier)
<a name="tuple_type"></a>
## 元组类型
@ -89,15 +91,16 @@ var someValue: ExampleModule.MyType
`Void` 是空元组类型 `()` 的别名。如果括号内只有一个元素,那么该类型就是括号内元素的类型。比如,`(Int)` 的类型是 `Int` 而不是 `(Int)`。所以,只有当元组类型包含的元素个数在两个及以上时才可以命名元组元素。
> 元组类型语法
> *元组类型* → **(** [*元组类型主体*](#tuple_type_body)<sub>可选</sub> **)**
<a name="tuple_type_body"></a>
> *元组类型主体* → [*元组类型元素列表*](#tuple_type_element_list) **...**<sub>可选</sub>
<a name="tuple_type_element_list"></a>
> *元组类型元素列表* → [*元组类型元素*](#tuple_type_element) | [*元组类型元素*](#tuple_type_element) **,** [*元组类型元素列表*](#tuple_type_element_list)
<a name="tuple_type_element"></a>
> *元组类型元素* → [*特性列表*](../chapter3/06_Attributes.html#attributes)<sub>可选</sub> **inout**<sub>可选</sub> [*类型*](#type) | **inout**<sub>可选</sub> [*元素名*](#element_name) [*类型注解*](#type_annotation)
<a name="element_name"></a>
> *元素名* → [*标识符*](LexicalStructure.html#identifier)
<a name="tuple-type"></a>
> *元组类型* → **(** [*元组类型主体*](#tuple-type-body)<sub>可选</sub> **)**
<a name="tuple-type-body"></a>
> *元组类型主体* → [*元组类型元素列表*](#tuple-type-element-list) **...**<sub>可选</sub>
<a name="tuple-type-element-list"></a>
> *元组类型元素列表* → [*元组类型元素*](#tuple-type-element) | [*元组类型元素*](#tuple-type-element) **,** [*元组类型元素列表*](#tuple-type-element-list)
<a name="tuple-type-element"></a>
> *元组类型元素* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **inout**<sub>可选</sub> [*类型*](#type) | **inout**<sub>可选</sub> [*元素名*](#element-name) [*类型注解*](#type-annotation)
<a name="element-name"></a>
> *元素名* → [*标识符*](02_Lexical_Structure.html#identifier)
<a name="function_type"></a>
## 函数类型
@ -114,11 +117,12 @@ var someValue: ExampleModule.MyType
为了指定一个 `in-out` 参数,可以在参数类型前加 `inout` 前缀。但是你不可以对可变长参数或返回值类型使用 `inout`。关于这种参数的详细讲解请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。
柯里化函数Curried fuction的函数类型从右向左进行组合。例如,函数类型 `Int -> Int -> Int` 可以理解为 `Int -> (Int -> Int)`,也就是说,该函数类型的参数为 `Int` 类型,其返回类型是一个参数类型为 `Int`,返回类型为 `Int` 的函数类型。关于柯里化函数的讨论见章节 [柯里化函数](05_Declarations.html#curried_functions)。
柯里化函数的函数类型从右向左进行组合。例如,函数类型 `Int -> Int -> Int` 可以理解为 `Int -> (Int -> Int)`,也就是说,该函数类型的参数为 `Int` 类型,其返回类型是一个参数类型为 `Int`,返回类型为 `Int` 的函数类型。关于柯里化函数的讨论见章节 [柯里化函数](05_Declarations.html#curried_functions)。
函数类型若要抛出错误就必须使用 `throws` 关键字来标记,若要重抛错误则必须使用 `rethrows` 关键字来标记。`throws` 关键字是函数类型的一部分,抛出函数nonthrowing function是抛出函数throwing function函数的一个子类型。因此,在使用抛出函数的地方也可以使用不抛出函数。对于柯里化函数,`throws` 关键字只应用于最里层的函数。抛出和重抛函数rethrowing function的相关描述见章节 [抛出函数与方法](05_Declarations.html#throwing_functions_and_methods) 和 [重抛函数与方法](05_Declarations.html#rethrowing_functions_and_methods)。
函数类型若要抛出错误就必须使用 `throws` 关键字来标记,若要重抛错误则必须使用 `rethrows` 关键字来标记。`throws` 关键字是函数类型的一部分,抛出函数是抛出函数函数的一个子类型。因此,在使用抛出函数的地方也可以使用不抛出函数。对于柯里化函数,`throws` 关键字只应用于最里层的函数。抛出和重抛函数的相关描述见章节 [抛出函数与方法](05_Declarations.html#throwing_functions_and_methods) 和 [重抛函数与方法](05_Declarations.html#rethrowing_functions_and_methods)。
> 函数类型语法
<a name="function-type"></a>
> *函数类型* → [*类型*](#type) **throws**<sub>可选</sub> **->** [*类型*](#type)
> *函数类型* → [*类型*](#type) **rethrows**<sub>可选</sub> **->** [*类型*](#type)
@ -149,6 +153,7 @@ var array3D: [[[Int]]] = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
关于 Swift 标准库中 `Array` 类型的详细讨论,请参阅 [数组](../chapter2/04_Collection_Types.html#arrays)。
> 数组类型语法
<a name="array-type"></a>
> *数组类型* → **[** [*类型*](#type) **]**
<a name="dictionary_type"></a>
@ -174,6 +179,7 @@ let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39]
关于 Swift 标准库中 `Dictionary` 类型的详细讨论,请参阅 [字典](../chapter2/04_Collection_Types.html#dictionaries)。
> 字典类型语法
<a name="dictionary-type"></a>
> *字典类型* → **[** [*类型*](#type) **:** [*类型*](#type) **]**
<a name="optional_type"></a>
@ -204,6 +210,7 @@ optionalInteger! // 42
更多细节以及更多如何使用可选类型的例子,请参阅 [可选类型](../chapter2/01_The_Basics.html#optionals)。
> 可选类型语法
<a name="optional-type"></a>
> *可选类型* → [*类型*](#type) **?**
<a name="implicitly_unwrapped_optional_type"></a>
@ -229,6 +236,7 @@ var implicitlyUnwrappedString: ImplicitlyUnwrappedOptional<String>
关于隐式解析可选类型的更多细节,请参阅 [隐式解析可选类型](../chapter2/01_The_Basics.html#implicityly_unwrapped_optionals)。
> 隐式解析可选类型语法
<a name="implicitly-unwrapped-optional-type"></a>
> *隐式解析可选类型* → [*类型*](#type) **!**
<a name="protocol_composition_type"></a>
@ -245,11 +253,12 @@ protocol<`Protocol 1`, `Procotol 2`>
协议合成列表中的每项必须是协议名或协议合成类型的类型别名。如果列表为空,它就会指定一个空协议合成列表,每个类型都符合它。
> 协议合成类型语法
> *协议合成类型* → **protocol** **<** [*协议标识符列表*](#protocol_identifier_list)<sub>可选</sub> **>**
<a name="protocol_identifier_list"></a>
> *协议标识符列表* → [*协议标识符*](#protocol_identifier) | [*协议标识符*](#protocol_identifier) **,** [*协议标识符列表*](#protocol_identifier_list)
<a name="protocol_identifier"></a>
> *协议标识符* → [*类型标识符*](#type_identifier)
<a name="protocol-composition-type"></a>
> *协议合成类型* → **protocol** **<** [*协议标识符列表*](#protocol-identifier-list)<sub>可选</sub> **>**
<a name="protocol-identifier-list"></a>
> *协议标识符列表* → [*协议标识符*](#protocol-identifier) | [*协议标识符*](#protocol-identifier) **,** [*协议标识符列表*](#protocol-identifier-list)
<a name="protocol-identifier"></a>
> *协议标识符* → [*类型标识符*](#type-identifier)
<a name="metatype_type"></a>
## 元类型
@ -306,6 +315,7 @@ let anotherInstance = metatype.init(string: "some string")
```
> 元类型语法
<a name="metatype-type"></a>
> *元类型* → [*类型*](#type) **.** **Type** | [*类型*](#type) **.** **Protocol**
<a name="type_inheritance_clause"></a>
@ -321,12 +331,12 @@ let anotherInstance = metatype.init(string: "some string")
> 类型继承子句语法
<a name="type_inheritance_clause"></a>
> *类型继承子句* → **:** [*类要求*](#class_requirement) **,** [*类型继承列表*](#type_inheritance_list)
> *类型继承子句* → **:** [*类要求*](#class_requirement)
> *类型继承子句* → **:** [*类型继承列表*](#type_inheritance_list)
<a name="type_inheritance_list"></a>
> *类型继承列表* → [*类型标识符*](#type_identifier) | [*类型标识符*](#type_identifier) **,** [*类型继承列表*](#type_inheritance_list)
<a name="class_requirement"></a>
> *类型继承子句* → **:** [*类要求*](#class-requirement) **,** [*类型继承列表*](#type-inheritance-list)
> *类型继承子句* → **:** [*类要求*](#class-requirement)
> *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list)
<a name="type-inheritance-list"></a>
> *类型继承列表* → [*类型标识符*](#type-identifier) | [*类型标识符*](#type-identifier) **,** [*类型继承列表*](#type-inheritance-list)
<a name="class-requirement"></a>
> *类要求* → **class**
<a name="type_inference"></a>

View File

@ -245,10 +245,13 @@ setter的名字和圆括号内的语句是可选的。如果写了一个setter
> *变量声明* → [*变量声明头(Head)*](../chapter3/05_Declarations.html#variable_declaration_head) [*变量名*](../chapter3/05_Declarations.html#variable_name) [*类型标注*](../chapter3/03_Types.html#type_annotation) [*构造器*](../chapter3/05_Declarations.html#initializer) _可选_ [*willSet-didSet代码块*](../chapter3/05_Declarations.html#willSet_didSet_block)
> *变量声明头(Head)* → [*特性(Attributes)列表*](../chapter3/06_Attributes.html#attributes) _可选_ [*声明修饰符(Specifiers)列表*](../chapter3/05_Declarations.html#declaration_specifiers) _可选_ **var**
> *变量名称* → [*标识符*](LexicalStructure.html#identifier)
<a name="getter-setter-block"></a>
> *getter-setter块* → **{** [*getter子句*](../chapter3/05_Declarations.html#getter_clause) [*setter子句*](../chapter3/05_Declarations.html#setter_clause) _可选_ **}**
> *getter-setter块* → **{** [*setter子句*](../chapter3/05_Declarations.html#setter_clause) [*getter子句*](../chapter3/05_Declarations.html#getter_clause) **}**
> *getter子句* → [*特性(Attributes)列表*](../chapter3/06_Attributes.html#attributes) _可选_ **get** [*代码块*](../chapter3/05_Declarations.html#code_block)
> *setter子句* → [*特性(Attributes)列表*](../chapter3/06_Attributes.html#attributes) _可选_ **set** [*setter名称*](../chapter3/05_Declarations.html#setter_name) _可选_ [*代码块*](../chapter3/05_Declarations.html#code_block)
<a name="getter-clause"></a>
> *getter 子句* → [*特性(Attributes)列表*](../chapter3/06_Attributes.html#attributes) _可选_ **get** [*代码块*](../chapter3/05_Declarations.html#code_block)
<a name="setter-clause"></a>
> *setter 子句* → [*特性(Attributes)列表*](../chapter3/06_Attributes.html#attributes) _可选_ **set** [*setter名称*](../chapter3/05_Declarations.html#setter_name) _可选_ [*代码块*](../chapter3/05_Declarations.html#code_block)
> *setter名称* → **(** [*标识符*](LexicalStructure.html#identifier) **)**
> *getter-setter关键字(Keyword)块* → **{** [*getter关键字(Keyword)子句*](../chapter3/05_Declarations.html#getter_keyword_clause) [*setter关键字(Keyword)子句*](../chapter3/05_Declarations.html#setter_keyword_clause) _可选_ **}**
> *getter-setter关键字(Keyword)块* → **{** [*setter关键字(Keyword)子句*](../chapter3/05_Declarations.html#setter_keyword_clause) [*getter关键字(Keyword)子句*](../chapter3/05_Declarations.html#getter_keyword_clause) **}**