移除行末空格 (#782)

This commit is contained in:
BqLin
2018-05-06 19:13:01 +08:00
committed by 安正超
parent a3f0cae500
commit 08a76e561f
29 changed files with 1030 additions and 1030 deletions

View File

@ -1,4 +1,4 @@
> 2016.9.23: 已经更新到 Swift 3.0。 > 2016.9.23: 已经更新到 Swift 3.0。
# 3.0 更新说明 # 3.0 更新说明
Swift 3.0 是自 Swift 开源以来第一个大的版本更新。从语言角度不兼容之前的 Swift 2.2 和 Swift 2.3 版本。Swift 3.0 的更新说明,大家可以查看[官方 blog 的说明](https://swift.org/blog/swift-3-0-released/),也可以关注 [SwiftGG](http://swift.gg) 最新的文章。学习官方文档,是掌握语言特性点的最佳途径,感谢翻译的小伙伴们为 Swift 社区所做贡献! Swift 3.0 是自 Swift 开源以来第一个大的版本更新。从语言角度不兼容之前的 Swift 2.2 和 Swift 2.3 版本。Swift 3.0 的更新说明,大家可以查看[官方 blog 的说明](https://swift.org/blog/swift-3-0-released/),也可以关注 [SwiftGG](http://swift.gg) 最新的文章。学习官方文档,是掌握语言特性点的最佳途径,感谢翻译的小伙伴们为 Swift 社区所做贡献!

View File

@ -1,6 +1,6 @@
# 版本兼容性 # 版本兼容性
----------------- -----------------
> 4.0 > 4.0
> 翻译:[muhlenXi](https://github.com/muhlenxi) 2017-09-25 > 翻译:[muhlenXi](https://github.com/muhlenxi) 2017-09-25

View File

@ -611,10 +611,10 @@ enum ServerResponse {
case result(String, String) case result(String, String)
case failure(String) case failure(String)
} }
let success = ServerResponse.result("6:00 am", "8:09 pm") let success = ServerResponse.result("6:00 am", "8:09 pm")
let failure = ServerResponse.failure("Out of cheese.") let failure = ServerResponse.failure("Out of cheese.")
switch success { switch success {
case let .result(sunrise, sunset): case let .result(sunrise, sunset):
print("Sunrise is at \(sunrise) and sunset is at \(sunset)") print("Sunrise is at \(sunrise) and sunset is at \(sunset)")

View File

@ -13,7 +13,7 @@
> 校对:[shanks](http://codebuild.me)[overtrue](https://github.com/overtrue) > 校对:[shanks](http://codebuild.me)[overtrue](https://github.com/overtrue)
> 2.2 > 2.2
> 校对:[SketchK](https://github.com/SketchK) > 校对:[SketchK](https://github.com/SketchK)
> 3.0 > 3.0
> 校对:[CMB](https://github.com/chenmingbiao)版本时间2016-09-13 > 校对:[CMB](https://github.com/chenmingbiao)版本时间2016-09-13
@ -676,7 +676,7 @@ if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secon
print("\(firstNumber) < \(secondNumber) < 100") print("\(firstNumber) < \(secondNumber) < 100")
} }
// 输出 "4 < 42 < 100" // 输出 "4 < 42 < 100"
if let firstNumber = Int("4") { if let firstNumber = Int("4") {
if let secondNumber = Int("42") { if let secondNumber = Int("42") {
if firstNumber < secondNumber && secondNumber < 100 { if firstNumber < secondNumber && secondNumber < 100 {
@ -774,7 +774,7 @@ do {
func makeASandwich() throws { func makeASandwich() throws {
// ... // ...
} }
do { do {
try makeASandwich() try makeASandwich()
eatASandwich() eatASandwich()

View File

@ -390,7 +390,7 @@ for name in names[2...] {
} }
// Brian // Brian
// Jack // Jack
for name in names[...2] { for name in names[...2] {
print(name) print(name)
} }

View File

@ -10,7 +10,7 @@
> 2.1 > 2.1
> 翻译:[DianQK](https://github.com/DianQK) > 翻译:[DianQK](https://github.com/DianQK)
> 校对:[shanks](http://codebuild.me), [Realank](https://github.com/Realank), > 校对:[shanks](http://codebuild.me), [Realank](https://github.com/Realank),
> 2.2 > 2.2
> 校对:[SketchK](https://github.com/SketchK) > 校对:[SketchK](https://github.com/SketchK)
@ -18,9 +18,9 @@
> 3.0 > 3.0
> 校对:[CMB](https://github.com/chenmingbiao)版本日期2016-09-13 > 校对:[CMB](https://github.com/chenmingbiao)版本日期2016-09-13
> 3.0.1, shanks, 2016-11-11 > 3.0.1, shanks, 2016-11-11
> 4.0 > 4.0
> 翻译:[kemchenj](https://kemchenj.github.io/) 2017-09-21 > 翻译:[kemchenj](https://kemchenj.github.io/) 2017-09-21
> 4.1 > 4.1
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift) > 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
@ -43,7 +43,7 @@
- [字符串的 Unicode 表示形式](#unicode_representations_of_strings) - [字符串的 Unicode 表示形式](#unicode_representations_of_strings)
*字符串*是是一系列字符的集合,例如 `"hello, world"``"albatross"`。Swift 的字符串通过 `String` 类型来表示。 *字符串*是是一系列字符的集合,例如 `"hello, world"``"albatross"`。Swift 的字符串通过 `String` 类型来表示。
一个 `String` 的内容可以用许多方式读取,包括作为一个 `Character` 值的集合。 一个 `String` 的内容可以用许多方式读取,包括作为一个 `Character` 值的集合。
Swift 的 `String``Character` 类型提供了快速和兼容 Unicode 的方式供你的代码使用。创建和操作字符串的语法与 C 语言中字符串操作相似,轻量并且易读。字符串连接操作只需要简单地通过 `+` 符号将两个字符串相连即可。与 Swift 中其他值一样,能否更改字符串的值,取决于其被定义为常量还是变量。你也可以在字符串内插过程中使用字符串插入常量、变量、字面量表达成更长的字符串,这样可以很容易的创建自定义的字符串值,进行展示、存储以及打印。 Swift 的 `String``Character` 类型提供了快速和兼容 Unicode 的方式供你的代码使用。创建和操作字符串的语法与 C 语言中字符串操作相似,轻量并且易读。字符串连接操作只需要简单地通过 `+` 符号将两个字符串相连即可。与 Swift 中其他值一样,能否更改字符串的值,取决于其被定义为常量还是变量。你也可以在字符串内插过程中使用字符串插入常量、变量、字面量表达成更长的字符串,这样可以很容易的创建自定义的字符串值,进行展示、存储以及打印。
@ -78,7 +78,7 @@ let someString = "Some string literal value"
let quotation = """ let quotation = """
The White Rabbit put on his spectacles. "Where shall I begin, The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked. please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on "Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop." till you come to the end; then stop."
""" """
@ -99,7 +99,7 @@ These are the same.
let softWrappedQuotation = """ let softWrappedQuotation = """
The White Rabbit put on his spectacles. "Where shall I begin, \ The White Rabbit put on his spectacles. "Where shall I begin, \
please your Majesty?" he asked. please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on \ "Begin at the beginning," the King said gravely, "and go on \
till you come to the end; then stop." till you come to the end; then stop."
""" """
@ -109,10 +109,10 @@ till you come to the end; then stop."
```swift ```swift
let lineBreaks = """ let lineBreaks = """
This string starts with a line break. This string starts with a line break.
It also ends with a line break. It also ends with a line break.
""" """
``` ```
@ -283,11 +283,11 @@ print(badStart + end)
// 打印两行: // 打印两行:
// one // one
// twothree // twothree
let goodStart = """ let goodStart = """
one one
two two
""" """
print(goodStart + end) print(goodStart + end)
// 打印三行: // 打印三行:
@ -464,7 +464,7 @@ for index in greeting.indices {
> 注意 > 注意
> >
> 您可以使用 `startIndex` 和 `endIndex` 属性或者 `index(before:)` 、`index(after:)` 和 `index(_:offsetBy:)` 方法在任意一个确认的并遵循 `Collection` 协议的类型里面,如上文所示是使用在 `String` 中,您也可以使用在 `Array`、`Dictionary` 和 `Set` 中。 > 您可以使用 `startIndex` 和 `endIndex` 属性或者 `index(before:)` 、`index(after:)` 和 `index(_:offsetBy:)` 方法在任意一个确认的并遵循 `Collection` 协议的类型里面,如上文所示是使用在 `String` 中,您也可以使用在 `Array`、`Dictionary` 和 `Set` 中。
<a name="inserting_and_removing"></a> <a name="inserting_and_removing"></a>
### 插入和删除 ### 插入和删除
@ -475,7 +475,7 @@ for index in greeting.indices {
var welcome = "hello" var welcome = "hello"
welcome.insert("!", at: welcome.endIndex) welcome.insert("!", at: welcome.endIndex)
// welcome 变量现在等于 "hello!" // welcome 变量现在等于 "hello!"
welcome.insert(contentsOf:" there", at: welcome.index(before: welcome.endIndex)) welcome.insert(contentsOf:" there", at: welcome.index(before: welcome.endIndex))
// welcome 变量现在等于 "hello there!" // welcome 变量现在等于 "hello there!"
``` ```
@ -485,7 +485,7 @@ welcome.insert(contentsOf:" there", at: welcome.index(before: welcome.endIndex))
```swift ```swift
welcome.remove(at: welcome.index(before: welcome.endIndex)) welcome.remove(at: welcome.index(before: welcome.endIndex))
// welcome 现在等于 "hello there" // welcome 现在等于 "hello there"
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range) welcome.removeSubrange(range)
// welcome 现在等于 "hello" // welcome 现在等于 "hello"
@ -493,7 +493,7 @@ welcome.removeSubrange(range)
> 注意 > 注意
> >
> 您可以使用 `insert(_:at:)`、`insert(contentsOf:at:)`、`remove(at:)` 和 `removeSubrange(_:)` 方法在任意一个确认的并遵循 `RangeReplaceableCollection` 协议的类型里面,如上文所示是使用在 `String` 中,您也可以使用在 `Array`、`Dictionary` 和 `Set` 中。 > 您可以使用 `insert(_:at:)`、`insert(contentsOf:at:)`、`remove(at:)` 和 `removeSubrange(_:)` 方法在任意一个确认的并遵循 `RangeReplaceableCollection` 协议的类型里面,如上文所示是使用在 `String` 中,您也可以使用在 `Array`、`Dictionary` 和 `Set` 中。
<a name="substrings"></a> <a name="substrings"></a>
## 子字符串 ## 子字符串
@ -505,7 +505,7 @@ let greeting = "Hello, world!"
let index = greeting.index(of: ",") ?? greeting.endIndex let index = greeting.index(of: ",") ?? greeting.endIndex
let beginning = greeting[..<index] let beginning = greeting[..<index]
// beginning 的值为 "Hello" // beginning 的值为 "Hello"
// 把结果转化为 String 以便长期存储。 // 把结果转化为 String 以便长期存储。
let newString = String(beginning) let newString = String(beginning)
``` ```

View File

@ -14,7 +14,7 @@
> 2.2 > 2.2
> 校对:[SketchK](https://github.com/SketchK) 2016-05-13 > 校对:[SketchK](https://github.com/SketchK) 2016-05-13
> 3.0.1shanks2016-11-13 > 3.0.1shanks2016-11-13
> 4.0 > 4.0
> 校对:[kemchenj](https://kemchenj.github.io/) 2017-09-21 > 校对:[kemchenj](https://kemchenj.github.io/) 2017-09-21

View File

@ -9,7 +9,7 @@
> 翻译+校对:[chenmingbiao](https://github.com/chenmingbiao) > 翻译+校对:[chenmingbiao](https://github.com/chenmingbiao)
> 2.1 > 2.1
> 翻译:[Channe](https://github.com/Channe)[Realank](https://github.com/Realank) > 翻译:[Channe](https://github.com/Channe)[Realank](https://github.com/Realank)
> 校对:[shanks](http://codebuild.me)2016-1-23 > 校对:[shanks](http://codebuild.me)2016-1-23
> 2.2 > 2.2
@ -377,7 +377,7 @@ Swift 为类类型提供了两种构造器来确保实例中所有存储型属
*指定构造器*是类中最主要的构造器。一个指定构造器将初始化类中提供的所有属性,并根据父类链往上调用父类合适的构造器来实现父类的初始化。 *指定构造器*是类中最主要的构造器。一个指定构造器将初始化类中提供的所有属性,并根据父类链往上调用父类合适的构造器来实现父类的初始化。
类倾向于拥有少量指定构造器,普遍的是一个类拥有一个指定构造器。指定构造器在初始化的地方通过“管道”将初始化过程持续到父类链。 类倾向于拥有少量指定构造器,普遍的是一个类拥有一个指定构造器。指定构造器在初始化的地方通过“管道”将初始化过程持续到父类链。
每一个类都必须至少拥有一个指定构造器。在某些情况下,许多类通过继承了父类中的指定构造器而满足了这个条件。具体内容请参考后续章节[构造器的自动继承](#automatic_initializer_inheritance)。 每一个类都必须至少拥有一个指定构造器。在某些情况下,许多类通过继承了父类中的指定构造器而满足了这个条件。具体内容请参考后续章节[构造器的自动继承](#automatic_initializer_inheritance)。
@ -746,15 +746,15 @@ for item in breakfastList {
```swift ```swift
let wholeNumber: Double = 12345.0 let wholeNumber: Double = 12345.0
let pi = 3.14159 let pi = 3.14159
if let valueMaintained = Int(exactly: wholeNumber) { if let valueMaintained = Int(exactly: wholeNumber) {
print("\(wholeNumber) conversion to Int maintains value of \(valueMaintained)") print("\(wholeNumber) conversion to Int maintains value of \(valueMaintained)")
} }
// 打印 "12345.0 conversion to Int maintains value of 12345" // 打印 "12345.0 conversion to Int maintains value of 12345"
let valueChanged = Int(exactly: pi) let valueChanged = Int(exactly: pi)
// valueChanged 是 Int? 类型,不是 Int 类型 // valueChanged 是 Int? 类型,不是 Int 类型
if valueChanged == nil { if valueChanged == nil {
print("\(pi) conversion to Int does not maintain value") print("\(pi) conversion to Int does not maintain value")
} }
@ -767,7 +767,7 @@ if valueChanged == nil {
struct Animal { struct Animal {
let species: String let species: String
init?(species: String) { init?(species: String) {
if species.isEmpty { if species.isEmpty {
return nil return nil
} }
self.species = species self.species = species
@ -994,7 +994,7 @@ class UntitledDocument: Document {
在这个例子中,如果在调用父类的可失败构造器 `init?(name:)` 时传入的是空字符串,那么强制解包操作会引发运行时错误。不过,因为这里是通过非空的字符串常量来调用它,所以并不会发生运行时错误。 在这个例子中,如果在调用父类的可失败构造器 `init?(name:)` 时传入的是空字符串,那么强制解包操作会引发运行时错误。不过,因为这里是通过非空的字符串常量来调用它,所以并不会发生运行时错误。
<a name="the_init!_failable_initializer"></a> <a name="the_init!_failable_initializer"></a>
### init!可失败构造器 ### init!可失败构造器
通常来说我们通过在 `init` 关键字后添加问号的方式(`init?`)来定义一个可失败构造器,但你也可以通过在 `init` 后面添加惊叹号的方式来定义一个可失败构造器(`init!`),该可失败构造器将会构建一个对应类型的隐式解包可选类型的对象。 通常来说我们通过在 `init` 关键字后添加问号的方式(`init?`)来定义一个可失败构造器,但你也可以通过在 `init` 后面添加惊叹号的方式来定义一个可失败构造器(`init!`),该可失败构造器将会构建一个对应类型的隐式解包可选类型的对象。

View File

@ -13,7 +13,7 @@
> 校对:[shanks](http://codebuild.me) > 校对:[shanks](http://codebuild.me)
> >
> 2.2 > 2.2
> 翻译+校对:[SketchK](https://github.com/SketchK) > 翻译+校对:[SketchK](https://github.com/SketchK)
> >
> 3.0 > 3.0
> 校对:[CMB](https://github.com/chenmingbiao)版本日期2016-09-13 > 校对:[CMB](https://github.com/chenmingbiao)版本日期2016-09-13
@ -699,7 +699,7 @@ class City: Location, Named {
func beginConcert(in location: Location & Named) { func beginConcert(in location: Location & Named) {
print("Hello, \(location.name)!") print("Hello, \(location.name)!")
} }
let seattle = City(name: "Seattle", latitude: 47.6, longitude: -122.3) let seattle = City(name: "Seattle", latitude: 47.6, longitude: -122.3)
beginConcert(in: seattle) beginConcert(in: seattle)
// Prints "Hello, Seattle!" // Prints "Hello, Seattle!"

View File

@ -76,7 +76,7 @@ func swapTwoStrings(_ a: inout String, _ b: inout String) {
a = b a = b
b = temporaryA b = temporaryA
} }
func swapTwoDoubles(_ a: inout Double, _ b: inout Double) { func swapTwoDoubles(_ a: inout Double, _ b: inout Double) {
let temporaryA = a let temporaryA = a
a = b a = b

View File

@ -210,7 +210,7 @@ Swift 提供了两种办法用来解决你在使用类的属性时所遇到的
> 注意 > 注意
> >
> 当 ARC 设置弱引用为 `nil` 时,属性观察不会被触发。 > 当 ARC 设置弱引用为 `nil` 时,属性观察不会被触发。
下面的例子跟上面 `Person``Apartment` 的例子一致,但是有一个重要的区别。这一次,`Apartment``tenant` 属性被声明为弱引用: 下面的例子跟上面 `Person``Apartment` 的例子一致,但是有一个重要的区别。这一次,`Apartment``tenant` 属性被声明为弱引用:
@ -271,10 +271,10 @@ unit4A = nil
上面的两段代码展示了变量 `john``unit4A` 在被赋值为 `nil` 后,`Person` 实例和 `Apartment` 实例的析构函数都打印出“销毁”的信息。这证明了引用循环被打破了。 上面的两段代码展示了变量 `john``unit4A` 在被赋值为 `nil` 后,`Person` 实例和 `Apartment` 实例的析构函数都打印出“销毁”的信息。这证明了引用循环被打破了。
> 注意 > 注意
> >
> 在使用垃圾收集的系统里,弱指针有时用来实现简单的缓冲机制,因为没有强引用的对象只会在内存压力触发垃圾收集时才被销毁。但是在 ARC 中,一旦值的最后一个强引用被移除,就会被立即销毁,这导致弱引用并不适合上面的用途。 > 在使用垃圾收集的系统里,弱指针有时用来实现简单的缓冲机制,因为没有强引用的对象只会在内存压力触发垃圾收集时才被销毁。但是在 ARC 中,一旦值的最后一个强引用被移除,就会被立即销毁,这导致弱引用并不适合上面的用途。
<a name="unowned_references"></a> <a name="unowned_references"></a>
### 无主引用 ### 无主引用

View File

@ -28,7 +28,7 @@ Swift 也保证同时访问同一块内存时不会冲突,通过约束代码
```swift ```swift
// 向 one 所在的内存区域发起一次写操作 // 向 one 所在的内存区域发起一次写操作
var one = 1 var one = 1
// 向 one 所在的内存区域发起一次读操作 // 向 one 所在的内存区域发起一次读操作
print("We're number \(one)!") print("We're number \(one)!")
``` ```
@ -42,7 +42,7 @@ print("We're number \(one)!")
而当你添加预算项进入表里的时候,它只是一个临时的,错误的状态,因为总数还没有呗更新。在添加预算项的过程中读取总数就会读取到错误的信息。 而当你添加预算项进入表里的时候,它只是一个临时的,错误的状态,因为总数还没有呗更新。在添加预算项的过程中读取总数就会读取到错误的信息。
这个例子也演示了你在修复内存访问冲突时会遇到的问题:有时修复的方式会有很多种,但哪一种是正确的就不总是那么明显了。在这个例子里,根据你是否需要更新后的总数,$5 和 $320 都可能是正确的值。在你修复访问冲突之前,你需要决定它的倾向。 这个例子也演示了你在修复内存访问冲突时会遇到的问题:有时修复的方式会有很多种,但哪一种是正确的就不总是那么明显了。在这个例子里,根据你是否需要更新后的总数,$5 和 $320 都可能是正确的值。在你修复访问冲突之前,你需要决定它的倾向。
> 注意 > 注意
> >
> 如果你写过并发和多线程的代码,内存访问冲突也许是同样的问题。然而,这里访问冲突的讨论是在单线程的情境下讨论的,并没有使用并发或者多线程。 > 如果你写过并发和多线程的代码,内存访问冲突也许是同样的问题。然而,这里访问冲突的讨论是在单线程的情境下讨论的,并没有使用并发或者多线程。
@ -65,7 +65,7 @@ print("We're number \(one)!")
func oneMore(than number: Int) -> Int { func oneMore(than number: Int) -> Int {
return number + 1 return number + 1
} }
var myNumber = 1 var myNumber = 1
myNumber = oneMore(than: myNumber) myNumber = oneMore(than: myNumber)
print(myNumber) print(myNumber)
@ -85,11 +85,11 @@ print(myNumber)
```swift ```swift
var stepSize = 1 var stepSize = 1
func increment(_ number: inout Int) { func increment(_ number: inout Int) {
number += stepSize number += stepSize
} }
increment(&stepSize) increment(&stepSize)
// 错误stepSize 访问冲突 // 错误stepSize 访问冲突
``` ```
@ -104,7 +104,7 @@ increment(&stepSize)
// 复制一份副本 // 复制一份副本
var copyOfStepSize = stepSize var copyOfStepSize = stepSize
increment(&copyOfStepSize) increment(&copyOfStepSize)
// 更新原来的值 // 更新原来的值
stepSize = copyOfStepSize stepSize = copyOfStepSize
// stepSize 现在的值是 2 // stepSize 现在的值是 2
@ -159,7 +159,7 @@ extension Player {
balance(&teammate.health, &health) balance(&teammate.health, &health)
} }
} }
var oscar = Player(name: "Oscar", health: 10, energy: 10) var oscar = Player(name: "Oscar", health: 10, energy: 10)
var maria = Player(name: "Maria", health: 5, energy: 10) var maria = Player(name: "Maria", health: 5, energy: 10)
oscar.shareHealth(with: &maria) // 正常 oscar.shareHealth(with: &maria) // 正常

View File

@ -12,7 +12,7 @@
> 翻译:[Prayer](https://github.com/futantan) > 翻译:[Prayer](https://github.com/futantan)
> 校对:[shanks](http://codebuild.me)2015-11-01 > 校对:[shanks](http://codebuild.me)2015-11-01
> 2.2 > 2.2
> 翻译+校对:[SketchK](https://github.com/SketchK) 2016-05-17 > 翻译+校对:[SketchK](https://github.com/SketchK) 2016-05-17
> 3.0.1 > 3.0.1
@ -77,7 +77,7 @@ Open 只能作用于类和类的成员,它和 Public 的区别如下:
* Public 或者其它更严访问级别的类成员,只能在其定义的模块内部的子类中重写。 * Public 或者其它更严访问级别的类成员,只能在其定义的模块内部的子类中重写。
* Open 的类,可以在其定义的模块中被继承,也可以在引用它的模块中被继承。 * Open 的类,可以在其定义的模块中被继承,也可以在引用它的模块中被继承。
* Open 的类成员,可以在其定义的模块中子类中重写,也可以在引用它的模块中的子类重写。 * Open 的类成员,可以在其定义的模块中子类中重写,也可以在引用它的模块中的子类重写。
把一个类标记为 `open`,明确的表示你已经充分考虑过外部模块使用此类作为父类的影响,并且设计好了你的类的代码了。 把一个类标记为 `open`,明确的表示你已经充分考虑过外部模块使用此类作为父类的影响,并且设计好了你的类的代码了。
<a name="guiding_principle_of_access_levels"></a> <a name="guiding_principle_of_access_levels"></a>
@ -126,7 +126,7 @@ public class SomePublicClass {}
internal class SomeInternalClass {} internal class SomeInternalClass {}
fileprivate class SomeFilePrivateClass {} fileprivate class SomeFilePrivateClass {}
private class SomePrivateClass {} private class SomePrivateClass {}
public var somePublicVariable = 0 public var somePublicVariable = 0
internal let someInternalConstant = 0 internal let someInternalConstant = 0
fileprivate func someFilePrivateFunction() {} fileprivate func someFilePrivateFunction() {}
@ -158,18 +158,18 @@ public class SomePublicClass { // 显式 public 类
fileprivate func someFilePrivateMethod() {} // 显式 fileprivate 类成员 fileprivate func someFilePrivateMethod() {} // 显式 fileprivate 类成员
private func somePrivateMethod() {} // 显式 private 类成员 private func somePrivateMethod() {} // 显式 private 类成员
} }
class SomeInternalClass { // 隐式 internal 类 class SomeInternalClass { // 隐式 internal 类
var someInternalProperty = 0 // 隐式 internal 类成员 var someInternalProperty = 0 // 隐式 internal 类成员
fileprivate func someFilePrivateMethod() {} // 显式 fileprivate 类成员 fileprivate func someFilePrivateMethod() {} // 显式 fileprivate 类成员
private func somePrivateMethod() {} // 显式 private 类成员 private func somePrivateMethod() {} // 显式 private 类成员
} }
fileprivate class SomeFilePrivateClass { // 显式 fileprivate 类 fileprivate class SomeFilePrivateClass { // 显式 fileprivate 类
func someFilePrivateMethod() {} // 隐式 fileprivate 类成员 func someFilePrivateMethod() {} // 隐式 fileprivate 类成员
private func somePrivateMethod() {} // 显式 private 类成员 private func somePrivateMethod() {} // 显式 private 类成员
} }
private class SomePrivateClass { // 显式 private 类 private class SomePrivateClass { // 显式 private 类
func somePrivateMethod() {} // 隐式 private 类成员 func somePrivateMethod() {} // 隐式 private 类成员
} }

View File

@ -11,7 +11,7 @@
> 2.1 > 2.1
> 校对:[shanks](http://codebuild.me)2015-11-01 > 校对:[shanks](http://codebuild.me)2015-11-01
> >
> 2.2 > 2.2
> 翻译+校对:[SketchK](https://github.com/SketchK) 2016-05-17 > 翻译+校对:[SketchK](https://github.com/SketchK) 2016-05-17
> >
> 3.0 > 3.0
@ -329,7 +329,7 @@ signedOverflow = signedOverflow &- 1
struct Vector2D { struct Vector2D {
var x = 0.0, y = 0.0 var x = 0.0, y = 0.0
} }
extension Vector2D { extension Vector2D {
static func + (left: Vector2D, right: Vector2D) -> Vector2D { static func + (left: Vector2D, right: Vector2D) -> Vector2D {
return Vector2D(x: left.x + right.x, y: left.y + right.y) return Vector2D(x: left.x + right.x, y: left.y + right.y)
@ -449,7 +449,7 @@ Swift 为以下自定义类型提等价运算符供合成实现:
struct Vector3D: Equatable { struct Vector3D: Equatable {
var x = 0.0, y = 0.0, z = 0.0 var x = 0.0, y = 0.0, z = 0.0
} }
let twoThreeFour = Vector3D(x: 2.0, y: 3.0, z: 4.0) let twoThreeFour = Vector3D(x: 2.0, y: 3.0, z: 4.0)
let anotherTwoThreeFour = Vector3D(x: 2.0, y: 3.0, z: 4.0) let anotherTwoThreeFour = Vector3D(x: 2.0, y: 3.0, z: 4.0)
if twoThreeFour == anotherTwoThreeFour { if twoThreeFour == anotherTwoThreeFour {

View File

@ -33,13 +33,13 @@ Swift 语言相对较小,这是由于 Swift 代码中在各种地方出现的
举个例子getter-setter 方法块的语法定义如下: 举个例子getter-setter 方法块的语法定义如下:
> getter-setter 方法块语法 > getter-setter 方法块语法
> >
> *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 子句*](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 分句构成,然后用大括号括起来,或者由一个 setter 分句后跟一个 getter 分句构成,然后用大括号括起来。上述的语法产式等价于下面的两个语法产式,
> getter-setter 方法块语法 > getter-setter 方法块语法
> >
> getter-setter 方法块 → { [*getter 子句*](05_Declarations.html#getter-clause) [*setter 子句*](05_Declarations.html#setter-clause)<sub>可选</sub> } > 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) } > getter-setter 方法块 → { [*setter 子句*](05_Declarations.html#setter-clause) [*getter 子句*](05_Declarations.html#getter-clause) }

View File

@ -23,7 +23,7 @@
- [标识符](#identifiers) - [标识符](#identifiers)
- [关键字和标点符号](#keywords) - [关键字和标点符号](#keywords)
- [字面量](#literals) - [字面量](#literals)
- [整数字面量](#integer_literals) - [整数字面量](#integer_literals)
- [浮点数字面量](#floating_point_literals) - [浮点数字面量](#floating_point_literals)
- [字符串字面量](#string_literals) - [字符串字面量](#string_literals)
- [运算符](#operators) - [运算符](#operators)
@ -61,7 +61,7 @@ Swift 的*“词法结构 (lexical structure)”* 描述了能构成该语言中
> *多行注释内容* → [*多行注释内容项*](#multiline-comment-text-item) [*多行注释内容*](#multiline-comment-text)<sub>可选</sub> > *多行注释内容* → [*多行注释内容项*](#multiline-comment-text-item) [*多行注释内容*](#multiline-comment-text)<sub>可选</sub>
> *多行注释内容项* → [*多行注释*](#multiline-comment). > *多行注释内容项* → [*多行注释*](#multiline-comment).
> *多行注释内容项* → [*注释内容项*](#comment-text-item) > *多行注释内容项* → [*注释内容项*](#comment-text-item)
> *多行注释内容项* → 任何 Unicode 标量值, 除了 `/*` 或者 `*/` > *多行注释内容项* → 任何 Unicode 标量值, 除了 `/*` 或者 `*/`
注释可以包含额外的格式和标记,正如 [*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) 所述。
@ -150,9 +150,9 @@ true // 布尔值字面量
<a id="numeric-literal"></a> <a id="numeric-literal"></a>
> *数值字面量* → **-**<sub>可选</sub> [*整数字面量*](#integer-literal) | **-**<sub>可选</sub> [*浮点数字面量*](#floating-point-literal) > *数值字面量* → **-**<sub>可选</sub> [*整数字面量*](#integer-literal) | **-**<sub>可选</sub> [*浮点数字面量*](#floating-point-literal)
> <a id="boolean-literal"></a> > <a id="boolean-literal"></a>
> *布尔值字面量* → **true** | **false** > *布尔值字面量* → **true** | **false**
> <a id="nil-literal"></a> > <a id="nil-literal"></a>
> *nil 字面量* → **nil** > *nil 字面量* → **nil**
<a id="integer_literals"></a> <a id="integer_literals"></a>
@ -177,21 +177,21 @@ true // 布尔值字面量
> *整数字面量* → [*十六进制字面量*](#hexadecimal-literal) > *整数字面量* → [*十六进制字面量*](#hexadecimal-literal)
<a id="binary-literal"></a> <a id="binary-literal"></a>
> *二进制字面量* → **0b** [*二进制数字*](#binary-digit) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub> > *二进制字面量* → **0b** [*二进制数字*](#binary-digit) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
> <a id="binary-digit"></a> > <a id="binary-digit"></a>
> *二进制数字* → 数值 0 到 1 > *二进制数字* → 数值 0 到 1
> <a id="binary-literal-character"></a> > <a id="binary-literal-character"></a>
> *二进制字面量字符* → [*二进制数字*](#binary-digit) | _ > *二进制字面量字符* → [*二进制数字*](#binary-digit) | _
> <a id="binary-literal-characters"></a> > <a id="binary-literal-characters"></a>
> *二进制字面量字符组* → [*二进制字面量字符*](#binary-literal-character) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub> > *二进制字面量字符组* → [*二进制字面量字符*](#binary-literal-character) [*二进制字面量字符组*](#binary-literal-characters)<sub>可选</sub>
<a id="octal-literal"></a> <a id="octal-literal"></a>
> *八进制字面量* → **0o** [*八进字数字*](#octal-digit) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub> > *八进制字面量* → **0o** [*八进字数字*](#octal-digit) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
> <a id="octal-digit"></a> > <a id="octal-digit"></a>
> *八进字数字* → 数值 0 到 7 > *八进字数字* → 数值 0 到 7
> <a id="octal-literal-character"></a> > <a id="octal-literal-character"></a>
> *八进制字符* → [*八进字数字*](#octal-digit) | _ > *八进制字符* → [*八进字数字*](#octal-digit) | _
> <a id="octal-literal-characters"></a> > <a id="octal-literal-characters"></a>
> *八进制字符组* → [*八进制字符*](#octal-literal-character) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub> > *八进制字符组* → [*八进制字符*](#octal-literal-character) [*八进制字符组*](#octal-literal-characters)<sub>可选</sub>
<a id="decimal-literal"></a> <a id="decimal-literal"></a>
@ -203,7 +203,7 @@ true // 布尔值字面量
> <a id="decimal-literal-character"></a> > <a id="decimal-literal-character"></a>
> *十进制字符* → [*十进制数字*](#decimal-digit) | _ > *十进制字符* → [*十进制数字*](#decimal-digit) | _
> <a id="decimal-literal-characters"></a> > <a id="decimal-literal-characters"></a>
> *十进制字符组* → [*十进制字符*](#decimal-literal-character) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub> > *十进制字符组* → [*十进制字符*](#decimal-literal-character) [*十进制字符组*](#decimal-literal-characters)<sub>可选</sub>
<a id="hexadecimal-literal"></a> <a id="hexadecimal-literal"></a>
> *十六进制字面量* → **0x** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub> > *十六进制字面量* → **0x** [*十六进制数字*](#hexadecimal-digit) [*十六进制字面量字符组*](#hexadecimal-literal-characters)<sub>可选</sub>
@ -233,12 +233,12 @@ true // 布尔值字面量
> 浮点数字面量语法 > 浮点数字面量语法
> >
<a id="floating-point-literal"></a> <a id="floating-point-literal"></a>
> *浮点数字面量* → [*十进制字面量*](#decimal-literal) [*十进制分数*](#decimal-fraction)<sub>可选</sub> [*十进制指数*](#decimal-exponent)<sub>可选</sub> > *浮点数字面量* → [*十进制字面量*](#decimal-literal) [*十进制分数*](#decimal-fraction)<sub>可选</sub> [*十进制指数*](#decimal-exponent)<sub>可选</sub>
> *浮点数字面量* → [*十六进制字面量*](#hexadecimal-literal) [*十六进制分数*](#hexadecimal-fraction)<sub>可选</sub> [*十六进制指数*](#hexadecimal-exponent) > *浮点数字面量* → [*十六进制字面量*](#hexadecimal-literal) [*十六进制分数*](#hexadecimal-fraction)<sub>可选</sub> [*十六进制指数*](#hexadecimal-exponent)
<a id="decimal-fraction"></a> <a id="decimal-fraction"></a>
> *十进制分数* → **.** [*十进制字面量*](#decimal-literal) > *十进制分数* → **.** [*十进制字面量*](#decimal-literal)
> <a id="decimal-exponent"></a> > <a id="decimal-exponent"></a>
> *十进制指数* → [*十进制指数 e*](#floating-point-e) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal) > *十进制指数* → [*十进制指数 e*](#floating-point-e) [*正负号*](#sign)<sub>可选</sub> [*十进制字面量*](#decimal-literal)
@ -313,20 +313,20 @@ let textB = "Hello world"
> 字符串字面量语法 > 字符串字面量语法
> >
<a id="string-literal"></a> <a id="string-literal"></a>
> *字符串字面量* → [*静态字符串字面量*](#static-string-literal) | [*插值字符串字面量*](#interpolated-string-literal) > *字符串字面量* → [*静态字符串字面量*](#static-string-literal) | [*插值字符串字面量*](#interpolated-string-literal)
<a id="static-string-literal"></a> <a id="static-string-literal"></a>
> *静态字符串字面量* → **"**[*引用文本*](#quoted-text)<sub>可选</sub>**"** > *静态字符串字面量* → **"**[*引用文本*](#quoted-text)<sub>可选</sub>**"**
> <a id="quoted-text"></a> > <a id="quoted-text"></a>
> *引用文本* → [*引用文本项*](#quoted-text-item) [*引用文本*](#quoted-text)<sub>可选</sub> > *引用文本* → [*引用文本项*](#quoted-text-item) [*引用文本*](#quoted-text)<sub>可选</sub>
> <a id="quoted-text-item"></a> > <a id="quoted-text-item"></a>
> *引用文本项* → [*转义字符*](#escaped-character) > *引用文本项* → [*转义字符*](#escaped-character)
> *引用文本项* → 除了 **"**、**\\**、U+000A、U+000D 以外的所有 Unicode 字符 > *引用文本项* → 除了 **"**、**\\**、U+000A、U+000D 以外的所有 Unicode 字符
<a id="interpolated-string-literal"></a> <a id="interpolated-string-literal"></a>
> *插值字符串字面量* → **"**[*插值文本*](#interpolated-text)<sub>可选</sub>**"** > *插值字符串字面量* → **"**[*插值文本*](#interpolated-text)<sub>可选</sub>**"**
> <a id="interpolated-text"></a> > <a id="interpolated-text"></a>
> *插值文本* → [*插值文本项*](#interpolated-text-item) [*插值文本*](#interpolated-text)<sub>可选</sub> > *插值文本* → [*插值文本项*](#interpolated-text-item) [*插值文本*](#interpolated-text)<sub>可选</sub>
> <a id="interpolated-text-item"></a> > <a id="interpolated-text-item"></a>
> *插值文本项* → **\\****(**[*表达式*](./04_Expressions.html)**)** | [*引用文本项*](#quoted-text-item) > *插值文本项* → **\\****(**[*表达式*](./04_Expressions.html)**)** | [*引用文本项*](#quoted-text-item)
@ -354,7 +354,7 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
运算符两侧的空白被用来区分该运算符是否为前缀运算符、后缀运算符或二元运算符。规则总结如下: 运算符两侧的空白被用来区分该运算符是否为前缀运算符、后缀运算符或二元运算符。规则总结如下:
* 如果运算符两侧都有空白或两侧都无空白,将被看作二元运算符。例如:`a+++b``a +++ b` 当中的 `+++` 运算符会被看作二元运算符。 * 如果运算符两侧都有空白或两侧都无空白,将被看作二元运算符。例如:`a+++b``a +++ b` 当中的 `+++` 运算符会被看作二元运算符。
* 如果运算符只有左侧空白,将被看作一元前缀运算符。例如 `a +++b` 中的 `+++` 运算符会被看做是一元前缀运算符。 * 如果运算符只有左侧空白,将被看作一元前缀运算符。例如 `a +++b` 中的 `+++` 运算符会被看做是一元前缀运算符。
* 如果运算符只有右侧空白,将被看作一元后缀运算符。例如 `a+++ b` 中的 `+++` 运算符会被看作是一元后缀运算符。 * 如果运算符只有右侧空白,将被看作一元后缀运算符。例如 `a+++ b` 中的 `+++` 运算符会被看作是一元后缀运算符。
* 如果运算符左侧没有空白并紧跟 `.`,将被看作一元后缀运算符。例如 `a+++.b` 中的 `+++` 运算符会被视为一元后缀运算符(即上式被视为 `a+++ .b` 而不是 `a +++ .b`)。 * 如果运算符左侧没有空白并紧跟 `.`,将被看作一元后缀运算符。例如 `a+++.b` 中的 `+++` 运算符会被视为一元后缀运算符(即上式被视为 `a+++ .b` 而不是 `a +++ .b`)。
@ -387,7 +387,7 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
> *头部运算符* → U+2794U+2BFF > *头部运算符* → U+2794U+2BFF
> *头部运算符* → U+2E00U+2E7F > *头部运算符* → U+2E00U+2E7F
> *头部运算符* → U+3001U+3003 > *头部运算符* → U+3001U+3003
> *头部运算符* → U+3008U+3030 > *头部运算符* → U+3008U+3030
<a id="operator-character"></a> <a id="operator-character"></a>
> *运算符字符* → [*头部运算符*](#operator-head) > *运算符字符* → [*头部运算符*](#operator-head)
@ -402,9 +402,9 @@ Swift 标准库定义了许多可供使用的运算符,其中大部分在 [基
<a id="dot-operator-head"></a> <a id="dot-operator-head"></a>
> *头部点运算符* → **..** > *头部点运算符* → **..**
> <a id="dot-operator-character"></a> > <a id="dot-operator-character"></a>
> *点运算符字符* → **.** | [*运算符字符*](#operator-character) > *点运算符字符* → **.** | [*运算符字符*](#operator-character)
> <a id="dot-operator-characters"></a> > <a id="dot-operator-characters"></a>
> *点运算符字符组* → [*点运算符字符*](#dot-operator-character) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub> > *点运算符字符组* → [*点运算符字符*](#dot-operator-character) [*点运算符字符组*](#dot-operator-characters)<sub>可选</sub>
<a id="binary-operator"></a> <a id="binary-operator"></a>

View File

@ -94,7 +94,7 @@ let origin: Point = (0, 0)
var someValue: ExampleModule.MyType var someValue: ExampleModule.MyType
``` ```
> 类型标识符语法 > 类型标识符语法
> >
<a name="type-identifier"></a> <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) > *类型标识符* → [*类型名称*](#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)
@ -153,14 +153,14 @@ someTuple = (left: 5, right: 5) // 错误:命名类型不匹配
func someFunction(left: Int, right: Int) {} func someFunction(left: Int, right: Int) {}
func anotherFunction(left: Int, right: Int) {} func anotherFunction(left: Int, right: Int) {}
func functionWithDifferentLabels(top: Int, bottom: Int) {} func functionWithDifferentLabels(top: Int, bottom: Int) {}
var f = someFunction // 函数 f 的类型为 (Int, Int) -> Void, 而不是 (left: Int, right: Int) -> Void. var f = someFunction // 函数 f 的类型为 (Int, Int) -> Void, 而不是 (left: Int, right: Int) -> Void.
f = anotherFunction // 正确 f = anotherFunction // 正确
f = functionWithDifferentLabels // 正确 f = functionWithDifferentLabels // 正确
func functionWithDifferentArgumentTypes(left: Int, right: String) {} func functionWithDifferentArgumentTypes(left: Int, right: String) {}
func functionWithDifferentNumberOfArguments(left: Int, right: Int, top: Int) {} func functionWithDifferentNumberOfArguments(left: Int, right: Int, top: Int) {}
f = functionWithDifferentArgumentTypes // 错误 f = functionWithDifferentArgumentTypes // 错误
f = functionWithDifferentNumberOfArguments // 错误 f = functionWithDifferentNumberOfArguments // 错误
``` ```
@ -215,7 +215,7 @@ func takesTwoFunctions(first: (Any) -> Void, second: (Any) -> Void) {
<a name="function-type-argument"></a> <a name="function-type-argument"></a>
> *函数类型参数* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type) | [*参数标签*](#argument-label) [*类型注解*](#type-annotation) > *函数类型参数* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **输入输出参数**<sub>可选</sub> [*类型*](#type) | [*参数标签*](#argument-label) [*类型注解*](#type-annotation)
<a name="argument-label"></a> <a name="argument-label"></a>
> *参数标签* → [*标识符*](02_Lexical_Structure.html#identifier) > *参数标签* → [*标识符*](02_Lexical_Structure.html#identifier)
<a name="array_type"></a> <a name="array_type"></a>
## 数组类型 ## 数组类型
@ -273,7 +273,7 @@ let someDictionary: Dictionary<String, Int> = ["Alex": 31, "Paul": 39]
> 字典类型语法 > 字典类型语法
> >
<a name="dictionary-type"></a> <a name="dictionary-type"></a>
> *字典类型* → **[** [*类型*](#type) **:** [*类型*](#type) **]** > *字典类型* → **[** [*类型*](#type) **:** [*类型*](#type) **]**
<a name="optional_type"></a> <a name="optional_type"></a>
## 可选类型 ## 可选类型
@ -324,7 +324,7 @@ var explicitlyUnwrappedString: Optional<String>
```swift ```swift
let tupleOfImplicitlyUnwrappedElements: (Int!, Int!) // 错误 let tupleOfImplicitlyUnwrappedElements: (Int!, Int!) // 错误
let implicitlyUnwrappedTuple: (Int, Int)! // 正确 let implicitlyUnwrappedTuple: (Int, Int)! // 正确
let arrayOfImplicitlyUnwrappedElements: [Int!] // 错误 let arrayOfImplicitlyUnwrappedElements: [Int!] // 错误
let implicitlyUnwrappedArray: [Int]! // 正确 let implicitlyUnwrappedArray: [Int]! // 正确
``` ```
@ -337,7 +337,7 @@ let implicitlyUnwrappedArray: [Int]! // 正确
关于隐式解析可选类型的更多细节,请参阅 [隐式解析可选类型](../chapter2/01_The_Basics.html#implicityly_unwrapped_optionals)。 关于隐式解析可选类型的更多细节,请参阅 [隐式解析可选类型](../chapter2/01_The_Basics.html#implicityly_unwrapped_optionals)。
> 隐式解析可选类型语法 > 隐式解析可选类型语法
> >
<a name="implicitly-unwrapped-optional-type"></a> <a name="implicitly-unwrapped-optional-type"></a>
> *隐式解析可选类型* → [*类型*](#type) **!** > *隐式解析可选类型* → [*类型*](#type) **!**
@ -362,7 +362,7 @@ let implicitlyUnwrappedArray: [Int]! // 正确
<a name="protocol-composition-continuation"></a> <a name="protocol-composition-continuation"></a>
> *协议合成延续* → [*协议标识符*](#protocol-identifier) | [*协议合成类型*](#protocol-composition-type) > *协议合成延续* → [*协议标识符*](#protocol-identifier) | [*协议合成类型*](#protocol-composition-type)
<a name="protocol-identifier"></a> <a name="protocol-identifier"></a>
> *协议标识符* → [*类型标识符*](#type-identifier) > *协议标识符* → [*类型标识符*](#type-identifier)
<a name="metatype_type"></a> <a name="metatype_type"></a>
## 元类型 ## 元类型
@ -412,7 +412,7 @@ let anotherInstance = metatype.init(string: "some string")
> 元类型语法 > 元类型语法
> >
<a name="metatype-type"></a> <a name="metatype-type"></a>
> *元类型* → [*类型*](#type) **.** **Type** | [*类型*](#type) **.** **Protocol** > *元类型* → [*类型*](#type) **.** **Type** | [*类型*](#type) **.** **Protocol**
<a name="type_inheritance_clause"></a> <a name="type_inheritance_clause"></a>
## 类型继承子句 ## 类型继承子句
@ -425,7 +425,7 @@ let anotherInstance = metatype.init(string: "some string")
枚举定义中的类型继承子句可以是一系列协议,或是枚举的原始值类型的命名型类型。在枚举定义中使用类型继承子句来指定原始值类型的例子,请参阅 [原始值](../chapter2/08_Enumerations.html#raw_values)。 枚举定义中的类型继承子句可以是一系列协议,或是枚举的原始值类型的命名型类型。在枚举定义中使用类型继承子句来指定原始值类型的例子,请参阅 [原始值](../chapter2/08_Enumerations.html#raw_values)。
> 类型继承子句语法 > 类型继承子句语法
> >
<a name="type_inheritance_clause"></a> <a name="type_inheritance_clause"></a>
> *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list) > *类型继承子句* → **:** [*类型继承列表*](#type-inheritance-list)

View File

@ -11,10 +11,10 @@
> 2.1 > 2.1
> 翻译:[mmoaay](https://github.com/mmoaay) > 翻译:[mmoaay](https://github.com/mmoaay)
> 2.2 > 2.2
> 校对:[175](https://github.com/Brian175) > 校对:[175](https://github.com/Brian175)
> 3.0 > 3.0
> 翻译+校对:[chenmingjia](https://github.com/chenmingjia) > 翻译+校对:[chenmingjia](https://github.com/chenmingjia)
> 4.1 > 4.1
@ -38,7 +38,7 @@
- [通配符表达式](#wildcard_expression) - [通配符表达式](#wildcard_expression)
- [选择器表达式](#selector_expression) - [选择器表达式](#selector_expression)
- [后缀表达式](#postfix_expressions) - [后缀表达式](#postfix_expressions)
- [函数调用表达式](#function_call_expression) - [函数调用表达式](#function_call_expression)
- [构造器表达式](#initializer_expression) - [构造器表达式](#initializer_expression)
- [显式成员表达式](#explicit_member_expression) - [显式成员表达式](#explicit_member_expression)
- [后缀 self 表达式](#postfix_self_expression) - [后缀 self 表达式](#postfix_self_expression)
@ -51,7 +51,7 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
通过前缀表达式和二元表达式可以对简单表达式使用各种运算符。基本表达式从概念上讲是最简单的一种表达式,它是一种访问值的方式。后缀表达式则允许你建立复杂的表达式,例如函数调用和成员访问。每种表达式都在下面有详细论述。 通过前缀表达式和二元表达式可以对简单表达式使用各种运算符。基本表达式从概念上讲是最简单的一种表达式,它是一种访问值的方式。后缀表达式则允许你建立复杂的表达式,例如函数调用和成员访问。每种表达式都在下面有详细论述。
> 表达式语法 > 表达式语法
> >
<a name="expression"></a> <a name="expression"></a>
> *表达式* → [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub> > *表达式* → [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
@ -60,7 +60,7 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
<a name="prefix_expressions"></a> <a name="prefix_expressions"></a>
## 前缀表达式 ## 前缀表达式
前缀表达式由可选的前缀运算符和表达式组成。前缀运算符只接收一个参数,表达式则紧随其后。 前缀表达式由可选的前缀运算符和表达式组成。前缀运算符只接收一个参数,表达式则紧随其后。
关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/25_Advanced_Operators.md)。 关于这些运算符的更多信息,请参阅 [基本运算符](../chapter2/02_Basic_Operators.md) 和 [高级运算符](../chapter2/25_Advanced_Operators.md)。
@ -69,7 +69,7 @@ Swift 中存在四种表达式:前缀表达式,二元表达式,基本表
除了标准库运算符,你也可以对某个变量使用 `&` 运算符,从而将其传递给函数的输入输出参数。更多信息,请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。 除了标准库运算符,你也可以对某个变量使用 `&` 运算符,从而将其传递给函数的输入输出参数。更多信息,请参阅 [输入输出参数](../chapter2/06_Functions.html#in_out_parameters)。
> 前缀表达式语法 > 前缀表达式语法
> >
<a name="prefix-expression"></a> <a name="prefix-expression"></a>
> *前缀表达式* → [*前缀运算符*](02_Lexical_Structure.md#prefix-operator)<sub>可选</sub> [*后缀表达式*](#postfix-expression) > *前缀表达式* → [*前缀运算符*](02_Lexical_Structure.md#prefix-operator)<sub>可选</sub> [*后缀表达式*](#postfix-expression)
@ -109,7 +109,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
关于 `try``try?``try!` 的更多信息,以及该如何使用的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html)。 关于 `try``try?``try!` 的更多信息,以及该如何使用的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html)。
> Try 表达式语法 > Try 表达式语法
> >
<a name="try-operator"></a> <a name="try-operator"></a>
> *try 运算符* → **try** | **try?** | **try!** > *try 运算符* → **try** | **try?** | **try!**
<a name="binary_expressions"></a> <a name="binary_expressions"></a>
@ -128,14 +128,14 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
> 在解析时,一个二元表达式将作为一个扁平列表表示,然后根据运算符的优先级,再进一步进行组合。例如,`2 + 3 * 5` 首先被看作具有五个元素的列表,即 `2`、`+`、`3`、`*`、`5`,随后根据运算符优先级组合为 `(2 + (3 * 5))`。 > 在解析时,一个二元表达式将作为一个扁平列表表示,然后根据运算符的优先级,再进一步进行组合。例如,`2 + 3 * 5` 首先被看作具有五个元素的列表,即 `2`、`+`、`3`、`*`、`5`,随后根据运算符优先级组合为 `(2 + (3 * 5))`。
<a name="binary-expression"></a> <a name="binary-expression"></a>
> 二元表达式语法 > 二元表达式语法
> >
> *二元表达式* → [*二元运算符*](02_Lexical_Structure.md#binary-operator) [*前缀表达式*](#prefix-expression). > *二元表达式* → [*二元运算符*](02_Lexical_Structure.md#binary-operator) [*前缀表达式*](#prefix-expression)
> *二元表达式* → [*赋值运算符*](#assignment-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) > *二元表达式* → [*赋值运算符*](#assignment-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression)
> *二元表达式* → [*条件运算符*](#conditional-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression) > *二元表达式* → [*条件运算符*](#conditional-operator) [*try 运算符*](#try-operator)<sub>可选</sub> [*前缀表达式*](#prefix-expression)
> *二元表达式* → [*类型转换运算符*](#type-casting-operator) > *二元表达式* → [*类型转换运算符*](#type-casting-operator)
<a name="binary-expressions"></a> <a name="binary-expressions"></a>
> *二元表达式列表* → [*二元表达式*](#binary-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub> > *二元表达式列表* → [*二元表达式*](#binary-expression) [*二元表达式列表*](#binary-expressions)<sub>可选</sub>
<a name="assignment_operator"></a> <a name="assignment_operator"></a>
### 赋值表达式 ### 赋值表达式
@ -153,7 +153,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
赋值运算符不返回任何值。 赋值运算符不返回任何值。
> 赋值运算符语法 > 赋值运算符语法
> >
<a name="assignment-operator"></a> <a name="assignment-operator"></a>
> *赋值运算符* → **=** > *赋值运算符* → **=**
@ -169,7 +169,7 @@ sum = (try someThrowingFunction()) + anotherThrowingFunction() // 错误try
关于使用三元条件运算符的例子,请参阅 [三元条件运算符](../chapter2/02_Basic_Operators.md#ternary_conditional_operator)。 关于使用三元条件运算符的例子,请参阅 [三元条件运算符](../chapter2/02_Basic_Operators.md#ternary_conditional_operator)。
> 三元条件运算符语法 > 三元条件运算符语法
> >
<a name="conditional-operator"></a> <a name="conditional-operator"></a>
> *三元条件运算符* → **?** [*表达式*](#expression) **:** > *三元条件运算符* → **?** [*表达式*](#expression) **:**
@ -197,11 +197,11 @@ func f(int: Int) { print("Function for Int") }
let x = 10 let x = 10
f(x) f(x)
// 打印 “Function for Int” // 打印 “Function for Int”
let y: Any = x let y: Any = x
f(y) f(y)
// 打印 “Function for Any” // 打印 “Function for Any”
f(x as Any) f(x as Any)
// 打印 “Function for Any” // 打印 “Function for Any”
``` ```
@ -215,12 +215,12 @@ f(x as Any)
关于类型转换的更多内容和例子,请参阅 [类型转换](../chapter2/19_Type_Casting.md)。 关于类型转换的更多内容和例子,请参阅 [类型转换](../chapter2/19_Type_Casting.md)。
<a name="type-casting-operator"></a> <a name="type-casting-operator"></a>
> 类型转换运算符语法 > 类型转换运算符语法
> >
> *类型转换运算符* → **is** [*类型*](03_Types.md#type) > *类型转换运算符* → **is** [*类型*](03_Types.md#type)
> *类型转换运算符* → **as** [*类型*](03_Types.md#type) > *类型转换运算符* → **as** [*类型*](03_Types.md#type)
> *类型转换运算符* → **as** **?** [*类型*](03_Types.md#type) > *类型转换运算符* → **as** **?** [*类型*](03_Types.md#type)
> *类型转换运算符* → **as** **!** [*类型*](03_Types.md#type) > *类型转换运算符* → **as** **!** [*类型*](03_Types.md#type)
<a name="primary_expressions"></a> <a name="primary_expressions"></a>
## 基本表达式 ## 基本表达式
@ -230,16 +230,16 @@ f(x as Any)
> 基本表达式语法 > 基本表达式语法
> >
<a name="primary-expression"></a> <a name="primary-expression"></a>
> *基本表达式* → [*标识符*](02_Lexical_Structure.md#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub> > *基本表达式* → [*标识符*](02_Lexical_Structure.md#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.md#generic-argument-clause)<sub>可选</sub>
> *基本表达式* → [*字面量表达式*](#literal-expression) > *基本表达式* → [*字面量表达式*](#literal-expression)
> *基本表达式* → [*self 表达式*](#self-expression) > *基本表达式* → [*self 表达式*](#self-expression)
> *基本表达式* → [*超类表达式*](#superclass-expression) > *基本表达式* → [*超类表达式*](#superclass-expression)
> *基本表达式* → [*闭包表达式*](#closure-expression) > *基本表达式* → [*闭包表达式*](#closure-expression)
> *基本表达式* → [*圆括号表达式*](#parenthesized-expression) > *基本表达式* → [*圆括号表达式*](#parenthesized-expression)
> *基本表达式* → [*隐式成员表达式*](#implicit-member-expression) > *基本表达式* → [*隐式成员表达式*](#implicit-member-expression)
> *基本表达式* → [*通配符表达式*](#wildcard-expression) > *基本表达式* → [*通配符表达式*](#wildcard-expression)
> *基本表达式* → [*选择器表达式*](#selector-expression) > *基本表达式* → [*选择器表达式*](#selector-expression)
> *基本表达式* → [*key-path字符串表达式*](#key-patch-string-expression) > *基本表达式* → [*key-path字符串表达式*](#key-patch-string-expression)
<a name="literal_expression"></a> <a name="literal_expression"></a>
### 字面量表达式 ### 字面量表达式
@ -263,7 +263,7 @@ func logFunctionName(string: String = #function) {
print(string) print(string)
} }
func myFunction() { func myFunction() {
logFunctionName() logFunctionName()
} }
myFunction() // 打印 “myFunction()” myFunction() // 打印 “myFunction()”
``` ```
@ -291,23 +291,23 @@ var emptyDictionary: [String : Double] = [:]
> 字面量表达式语法 > 字面量表达式语法
> >
<a name="literal-expression"></a> <a name="literal-expression"></a>
> *字面量表达式* → [*字面量*](02_Lexical_Structure.md#literal) > *字面量表达式* → [*字面量*](02_Lexical_Structure.md#literal)
> *字面量表达式* → [*数组字面量*](#array-literal) | [*字典字面量*](#dictionary-literal) | [*练习场字面量*](#playground-literal) > *字面量表达式* → [*数组字面量*](#array-literal) | [*字典字面量*](#dictionary-literal) | [*练习场字面量*](#playground-literal)
> *字面量表达式* → **#file** | **#line** | **#column** | **#function** > *字面量表达式* → **#file** | **#line** | **#column** | **#function**
> >
<a name="array-literal"></a> <a name="array-literal"></a>
> *数组字面量* → [[*数组字面量项列表*](#array-literal-items)<sub>可选</sub> **]** > *数组字面量* → [[*数组字面量项列表*](#array-literal-items)<sub>可选</sub> **]**
<a name="array-literal-items"></a> <a name="array-literal-items"></a>
> *数组字面量项列表* → [*数组字面量项*](#array-literal-item) **,**<sub>可选</sub> | [*数组字面量项*](#array-literal-item) **,** [*数组字面量项列表*](#array-literal-items) > *数组字面量项列表* → [*数组字面量项*](#array-literal-item) **,**<sub>可选</sub> | [*数组字面量项*](#array-literal-item) **,** [*数组字面量项列表*](#array-literal-items)
<a name="array-literal-item"></a> <a name="array-literal-item"></a>
> *数组字面量项* → [*表达式*](#expression) > *数组字面量项* → [*表达式*](#expression)
> >
<a name="dictionary-literal"></a> <a name="dictionary-literal"></a>
> *字典字面量* → [[*字典字面量项列表*](#dictionary-literal-items) **]** | **[** **:** **]** > *字典字面量* → [[*字典字面量项列表*](#dictionary-literal-items) **]** | **[** **:** **]**
<a name="dictionary-literal-items"></a> <a name="dictionary-literal-items"></a>
> *字典字面量项列表* → [*字典字面量项*](#dictionary-literal-item) **,**<sub>可选</sub> | [*字典字面量项*](#dictionary-literal-item) **,** [*字典字面量项列表*](#dictionary-literal-items) > *字典字面量项列表* → [*字典字面量项*](#dictionary-literal-item) **,**<sub>可选</sub> | [*字典字面量项*](#dictionary-literal-item) **,** [*字典字面量项列表*](#dictionary-literal-items)
<a name="dictionary-literal-item"></a> <a name="dictionary-literal-item"></a>
> *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)。 > *字典字面量项* → [*表达式*](#expression) **:** [*表达式*](#expression)。
<a name="self_expression"></a> <a name="self_expression"></a>
@ -315,11 +315,11 @@ var emptyDictionary: [String : Double] = [:]
`self` 表达式是对当前类型或者当前实例的显式引用,它有如下形式: `self` 表达式是对当前类型或者当前实例的显式引用,它有如下形式:
> self > self
> self.`成员名称` > self.`成员名称`
> self[`下标索引`] > self[`下标索引`]
> self(`构造器参数`) > self(`构造器参数`)
> self.init(`构造器参数`) > self.init(`构造器参数`)
如果在构造器、下标、实例方法中,`self` 引用的是当前类型的实例。在一个类型方法中,`self` 引用的是当前的类型。 如果在构造器、下标、实例方法中,`self` 引用的是当前类型的实例。在一个类型方法中,`self` 引用的是当前的类型。
@ -345,42 +345,42 @@ struct Point {
} }
``` ```
> Self 表达式语法 > Self 表达式语法
> >
<a name="self-expression"></a> <a name="self-expression"></a>
> *self 表达式* → **self** | [*self 方法表达式*](#self-method-expression) [*self 下标表达式*](#self-subscript-expression) | [*self 构造器表达式*](#self-initializer-expression) > *self 表达式* → **self** | [*self 方法表达式*](#self-method-expression) [*self 下标表达式*](#self-subscript-expression) | [*self 构造器表达式*](#self-initializer-expression)
> >
<a name="self-method-expression"></a> <a name="self-method-expression"></a>
> *self 方法表达式* → **self** **.** [*标识符*](02_Lexical_Structure.md#identifier) > *self 方法表达式* → **self** **.** [*标识符*](02_Lexical_Structure.md#identifier)
<a name="self-subscript-expression"></a> <a name="self-subscript-expression"></a>
> *self 下标表达式* → **self** [ [*函数调用参数表*](#function-call-argument-list­) **]** > *self 下标表达式* → **self** [ [*函数调用参数表*](#function-call-argument-list­) **]**
<a name="self-initializer-expression"></a> <a name="self-initializer-expression"></a>
> *self 构造器表达式* → **self** **.** **init** > *self 构造器表达式* → **self** **.** **init**
<a name="superclass_expression"></a> <a name="superclass_expression"></a>
### 父类表达式 ### 父类表达式
*父类*表达式可以使我们在某个类中访问它的超类,它有如下形式: *父类*表达式可以使我们在某个类中访问它的超类,它有如下形式:
> super.`成员名称` > super.`成员名称`
> super[`下标索引`] > super[`下标索引`]
> super.init(`构造器参数`) > super.init(`构造器参数`)
第一种形式用来访问超类的某个成员,第二种形式用来访问超类的下标,第三种形式用来访问超类的构造器。 第一种形式用来访问超类的某个成员,第二种形式用来访问超类的下标,第三种形式用来访问超类的构造器。
子类可以通过超类表达式在它们的成员、下标和构造器中使用超类中的实现。 子类可以通过超类表达式在它们的成员、下标和构造器中使用超类中的实现。
> 父类表达式语法 > 父类表达式语法
> >
<a name="superclass-expression"></a> <a name="superclass-expression"></a>
> *超类表达式* → [*超类方法表达式*](#superclass-method-expression) | [*超类下标表达式*](#superclass-subscript-expression) | [*超类构造器表达式*](#superclass-initializer-expression) > *超类表达式* → [*超类方法表达式*](#superclass-method-expression) | [*超类下标表达式*](#superclass-subscript-expression) | [*超类构造器表达式*](#superclass-initializer-expression)
> >
<a name="superclass-method-expression"></a> <a name="superclass-method-expression"></a>
> *超类方法表达式* → **super** **.** [*标识符*](02_Lexical_Structure.md#identifier) > *超类方法表达式* → **super** **.** [*标识符*](02_Lexical_Structure.md#identifier)
<a name="superclass-subscript-expression"></a> <a name="superclass-subscript-expression"></a>
> *超类下标表达式* → **super** [[*函数调用参数表*](#function-call-argument-list­) **]** > *超类下标表达式* → **super** [[*函数调用参数表*](#function-call-argument-list­) **]**
<a name="superclass-initializer-expression"></a> <a name="superclass-initializer-expression"></a>
> *超类构造器表达式* → **super** **.** **init** > *超类构造器表达式* → **super** **.** **init**
<a name="closure_expression"></a> <a name="closure_expression"></a>
### 闭包表达式 ### 闭包表达式
@ -484,24 +484,24 @@ myFunction { [weak parent = self.parent] in print(parent!.title) }
> 闭包表达式语法 > 闭包表达式语法
> >
<a name="closure-expression"></a> <a name="closure-expression"></a>
> *闭包表达式* → **{** [*闭包签名*](#closure-signature)<sub>可选</sub> [*语句*](#statements) **}** > *闭包表达式* → **{** [*闭包签名*](#closure-signature)<sub>可选</sub> [*语句*](#statements) **}**
<a name="closure-signature"></a> <a name="closure-signature"></a>
> *闭包签名* → [*参数子句*](#parameter-clause) [*函数结果*](05_Declarations.html#function-result)<sub>可选</sub> **in** > *闭包签名* → [*参数子句*](#parameter-clause) [*函数结果*](05_Declarations.html#function-result)<sub>可选</sub> **in**
> *闭包签名* → [*标识符列表*](#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in** > *闭包签名* → [*标识符列表*](#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
> *闭包签名* → [*捕获列表*](#capture-list) [*参数子句*](05_Declarations.md#parameter-clause) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in** > *闭包签名* → [*捕获列表*](#capture-list) [*参数子句*](05_Declarations.md#parameter-clause) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
> *闭包签名* → [*捕获列表*](#capture-list) [*标识符列表*](02_Lexical_Structure.md#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in** > *闭包签名* → [*捕获列表*](#capture-list) [*标识符列表*](02_Lexical_Structure.md#identifier-list) [*函数结果*](05_Declarations.md#function-result)<sub>可选</sub> **in**
> *闭包签名* → [*捕获列表*](#capture-list) **in** > *闭包签名* → [*捕获列表*](#capture-list) **in**
<a name="capture-list"></a> <a name="capture-list"></a>
> *捕获列表* → [ [*捕获列表项列表*](#capture-list-items) **]** > *捕获列表* → [ [*捕获列表项列表*](#capture-list-items) **]**
<a name="capture-list-items"></a> <a name="capture-list-items"></a>
> *捕获列表项列表* → [*捕获列表项*](#capture-list-item) | [*捕获列表项*](#capture-list-item) **,** [*捕获列表项列表*](#capture-list-items) > *捕获列表项列表* → [*捕获列表项*](#capture-list-item) | [*捕获列表项*](#capture-list-item) **,** [*捕获列表项列表*](#capture-list-items)
<a name="capture-list-item"></a> <a name="capture-list-item"></a>
> *捕获列表项* → [*捕获说明符*](#capture-specifier)<sub>可选</sub> [*表达式*](#expression) > *捕获列表项* → [*捕获说明符*](#capture-specifier)<sub>可选</sub> [*表达式*](#expression)
<a name="capture-specifier"></a> <a name="capture-specifier"></a>
> *捕获说明符* → **weak** | **unowned** | **unowned(safe)** | **unowned(unsafe)** > *捕获说明符* → **weak** | **unowned** | **unowned(safe)** | **unowned(unsafe)**
<a name="implicit_member_expression"></a> <a name="implicit_member_expression"></a>
### 隐式成员表达式 ### 隐式成员表达式
@ -516,10 +516,10 @@ var x = MyEnumeration.SomeValue
x = .AnotherValue x = .AnotherValue
``` ```
> 隐式成员表达式语法 > 隐式成员表达式语法
> >
<a name="implicit-member-expression"></a> <a name="implicit-member-expression"></a>
> *隐式成员表达式* → **.** [*标识符*](02_Lexical_Structure.md#identifier) > *隐式成员表达式* → **.** [*标识符*](02_Lexical_Structure.md#identifier)
<a name="parenthesized_expression"></a> <a name="parenthesized_expression"></a>
### 圆括号表达式 ### 圆括号表达式
@ -529,7 +529,7 @@ x = .AnotherValue
> 圆括号表达式语法 > 圆括号表达式语法
> >
<a name="parenthesized-expression"></a> <a name="parenthesized-expression"></a>
> *圆括号表达式* → **( [*表达式*](#expression) )** > *圆括号表达式* → **( [*表达式*](#expression) )**
<a name="Tuple_Expression"></a> <a name="Tuple_Expression"></a>
### 元组表达式 ### 元组表达式
@ -543,11 +543,11 @@ x = .AnotherValue
> 元组表达式语法 > 元组表达式语法
> >
<a name="tuple-expression"></a> <a name="tuple-expression"></a>
> *元组表达式* → **( )** | **(**[*元组元素*](#tuple-element) [*元组元素列表*](#tuple-element-list) **)** > *元组表达式* → **( )** | **(**[*元组元素*](#tuple-element) [*元组元素列表*](#tuple-element-list) **)**
<a name="tuple-element-list"></a> <a name="tuple-element-list"></a>
> *元组元素列表* → [*元组元素*](#tuple-element) | [*元组元素*](#tuple-element) **,** [*元组元素列表*](#tuple-element-list) > *元组元素列表* → [*元组元素*](#tuple-element) | [*元组元素*](#tuple-element) **,** [*元组元素列表*](#tuple-element-list)
<a name="tuple-element"></a> <a name="tuple-element"></a>
> *元组元素* → [*表达式*](#expression) | [*标识符*](identifier) **:** [*表达式*](#expression) > *元组元素* → [*表达式*](#expression) | [*标识符*](identifier) **:** [*表达式*](#expression)
<a name="wildcard_expression"></a> <a name="wildcard_expression"></a>
### 通配符表达式 ### 通配符表达式
@ -559,7 +559,7 @@ x = .AnotherValue
// x 为 1020 被忽略 // x 为 1020 被忽略
``` ```
> 通配符表达式语法 > 通配符表达式语法
> >
<a name="wildcard-expression"></a> <a name="wildcard-expression"></a>
> *通配符表达式* → **_** > *通配符表达式* → **_**
@ -626,16 +626,16 @@ let anotherSelector = #selector(SomeClass.doSomething(_:) as (SomeClass) -> (Str
> 后缀表达式语法 > 后缀表达式语法
<a name="postfix-expression"></a> <a name="postfix-expression"></a>
> *后缀表达式* → [*基本表达式*](#primary-expression) > *后缀表达式* → [*基本表达式*](#primary-expression)
> *后缀表达式* → [*后缀表达式*](#postfix-expression) [*后缀运算符*](02_Lexical_Structure.md#postfix-operator) > *后缀表达式* → [*后缀表达式*](#postfix-expression) [*后缀运算符*](02_Lexical_Structure.md#postfix-operator)
> *后缀表达式* → [*函数调用表达式*](#function-call-expression) > *后缀表达式* → [*函数调用表达式*](#function-call-expression)
> *后缀表达式* → [*构造器表达式*](#initializer-expression) > *后缀表达式* → [*构造器表达式*](#initializer-expression)
> *后缀表达式* → [*显式成员表达式*](#explicit-member-expression) > *后缀表达式* → [*显式成员表达式*](#explicit-member-expression)
> *后缀表达式* → [*后缀 self 表达式*](#postfix-self-expression) > *后缀表达式* → [*后缀 self 表达式*](#postfix-self-expression)
> *后缀表达式* → [*dynamicType 表达式*](#dynamic-type-expression) > *后缀表达式* → [*dynamicType 表达式*](#dynamic-type-expression)
> *后缀表达式* → [*下标表达式*](#subscript-expression) > *后缀表达式* → [*下标表达式*](#subscript-expression)
> *后缀表达式* → [*强制取值表达式*](#forced-value-expression) > *后缀表达式* → [*强制取值表达式*](#forced-value-expression)
> *后缀表达式* → [*可选链表达式*](#optional-chaining-expression) > *后缀表达式* → [*可选链表达式*](#optional-chaining-expression)
<a name="function_call_expression"></a> <a name="function_call_expression"></a>
### 函数调用表达式 ### 函数调用表达式
@ -668,10 +668,10 @@ myData.someMethod {$0 == 13}
> 函数调用表达式语法 > 函数调用表达式语法
<a name="function-call-expression"></a> <a name="function-call-expression"></a>
> *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*圆括号表达式*](#parenthesized-expression) > *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*圆括号表达式*](#parenthesized-expression)
> *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*圆括号表达式*](#parenthesized-expression)<sub>可选</sub> [*尾随闭包*](#trailing-closure) > *函数调用表达式* → [*后缀表达式*](#postfix-expression) [*圆括号表达式*](#parenthesized-expression)<sub>可选</sub> [*尾随闭包*](#trailing-closure)
<a name="trailing-closure"></a> <a name="trailing-closure"></a>
> *尾随闭包* → [*闭包表达式*](#closure-expression) > *尾随闭包* → [*闭包表达式*](#closure-expression)
<a name="initializer_expression"></a> <a name="initializer_expression"></a>
### 构造器表达式 ### 构造器表达式
@ -713,8 +713,8 @@ let s3 = someValue.dynamicType.init(data: 7) // 有效
> 构造器表达式语法 > 构造器表达式语法
<a name="initializer-expression"></a> <a name="initializer-expression"></a>
> *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** > *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init**
> *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** **(** [*参数名称*](#argument-names) **)** > *构造器表达式* → [*后缀表达式*](#postfix-expression) **.** **init** **(** [*参数名称*](#argument-names) **)**
<a name="explicit_member_expression"></a> <a name="explicit_member_expression"></a>
### 显式成员表达式 ### 显式成员表达式
@ -773,14 +773,14 @@ let x = [10, 3, 20, 15, 4]
> 显式成员表达式语法 > 显式成员表达式语法
<a name="explicit-member-expression"></a> <a name="explicit-member-expression"></a>
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*十进制数字*] (02_Lexical_Structure.html#decimal-digit) > *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*十进制数字*] (02_Lexical_Structure.html#decimal-digit)
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02_Lexical_Structure.html#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.html#generic-argument-clause)<sub>可选</sub><br/> > *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*](02_Lexical_Structure.html#identifier) [*泛型实参子句*](08_Generic_Parameters_and_Arguments.html#generic-argument-clause)<sub>可选</sub><br/>
> *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.html#identifier) **(** [*参数名称*](#argument-names) **)** > *显式成员表达式* → [*后缀表达式*](#postfix-expression) **.** [*标识符*] (02_Lexical_Structure.html#identifier) **(** [*参数名称*](#argument-names) **)**
> >
<a name="argument-names"></a> <a name="argument-names"></a>
> *参数名称* → [*参数名*](#argument-name) [*参数名称*](#argument-names)<sub>可选</sub><br/> > *参数名称* → [*参数名*](#argument-name) [*参数名称*](#argument-names)<sub>可选</sub><br/>
<a name="argument-name"></a> <a name="argument-name"></a>
> *参数名* → [*标识符*](02_Lexical_Structure.html#identifier) **:** > *参数名* → [*标识符*](02_Lexical_Structure.html#identifier) **:**
<a name="postfix_self_expression"></a> <a name="postfix_self_expression"></a>
### 后缀 self 表达式 ### 后缀 self 表达式
@ -794,7 +794,7 @@ let x = [10, 3, 20, 15, 4]
第二种形式返回相应的类型。我们可以用它来获取某个实例的类型作为一个值来使用。例如,`SomeClass.self` 会返回 `SomeClass` 类型本身,你可以将其传递给相应函数或者方法作为参数。 第二种形式返回相应的类型。我们可以用它来获取某个实例的类型作为一个值来使用。例如,`SomeClass.self` 会返回 `SomeClass` 类型本身,你可以将其传递给相应函数或者方法作为参数。
> 后缀 self 表达式语法 > 后缀 self 表达式语法
> >
<a name="postfix-self-expression"></a> <a name="postfix-self-expression"></a>
> *后缀 self 表达式* → [*后缀表达式*](#postfix-expression) **.** **self** > *后缀 self 表达式* → [*后缀表达式*](#postfix-expression) **.** **self**
@ -811,7 +811,7 @@ let x = [10, 3, 20, 15, 4]
关于下标的声明,请参阅 [协议下标声明](05_Declarations.html#protocol_subscript_declaration)。 关于下标的声明,请参阅 [协议下标声明](05_Declarations.html#protocol_subscript_declaration)。
> 下标表达式语法 > 下标表达式语法
> >
<a name="subscript-expression"></a> <a name="subscript-expression"></a>
> *下标表达式* → [*后缀表达式*](#postfix-expression) **[** [*表达式列表*](#expression-list) **]** > *下标表达式* → [*后缀表达式*](#postfix-expression) **[** [*表达式列表*](#expression-list) **]**
@ -831,13 +831,13 @@ let x = [10, 3, 20, 15, 4]
var x: Int? = 0 var x: Int? = 0
x!++ x!++
// x 现在是 1 // x 现在是 1
var someDictionary = ["a": [1, 2, 3], "b": [10, 20]] var someDictionary = ["a": [1, 2, 3], "b": [10, 20]]
someDictionary["a"]![0] = 100 someDictionary["a"]![0] = 100
// someDictionary 现在是 [b: [10, 20], a: [100, 2, 3]] // someDictionary 现在是 [b: [10, 20], a: [100, 2, 3]]
``` ```
> 强制取值语法 > 强制取值语法
> >
<a name="forced-value-expression"></a> <a name="forced-value-expression"></a>
> *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!** > *强制取值表达式* → [*后缀表达式*](#postfix-expression) **!**
@ -874,8 +874,8 @@ if let unwrappedC = c {
```swift ```swift
func someFunctionWithSideEffects() -> Int { func someFunctionWithSideEffects() -> Int {
// 译者注:为了能看出此函数是否被执行,加上了一句打印 // 译者注:为了能看出此函数是否被执行,加上了一句打印
print("someFunctionWithSideEffects") print("someFunctionWithSideEffects")
return 42 return 42
} }
var someDictionary = ["a": [1, 2, 3], "b": [10, 20]] var someDictionary = ["a": [1, 2, 3], "b": [10, 20]]
@ -888,7 +888,7 @@ someDictionary["a"]?[0] = someFunctionWithSideEffects()
// someDictionary 现在是 ["b": [10, 20], "a": [42, 2, 3]] // someDictionary 现在是 ["b": [10, 20], "a": [42, 2, 3]]
``` ```
> 可选链表达式语法 > 可选链表达式语法
> >
<a name="optional-chaining-expression"></a> <a name="optional-chaining-expression"></a>
> *可选链表达式* → [*后缀表达式*](#postfix-expression) **?** > *可选链表达式* → [*后缀表达式*](#postfix-expression) **?**

View File

@ -21,11 +21,11 @@
本页包含内容: 本页包含内容:
- [循环语句](#loop_statements) - [循环语句](#loop_statements)
- [For-In 语句](#for-in_statements) - [For-In 语句](#for-in_statements)
- [While 语句](#while_statements) - [While 语句](#while_statements)
- [Repeat-While 语句](#repeat-while_statements) - [Repeat-While 语句](#repeat-while_statements)
- [分支语句](#branch_statements) - [分支语句](#branch_statements)
- [If 语句](#if_statements) - [If 语句](#if_statements)
- [Guard 语句](#guard_statements) - [Guard 语句](#guard_statements)
- [Switch 语句](#switch_statements) - [Switch 语句](#switch_statements)
- [带标签的语句](#labeled_statements) - [带标签的语句](#labeled_statements)
@ -38,7 +38,7 @@
- [Defer 语句](#defer_statements) - [Defer 语句](#defer_statements)
- [Do 语句](#do_statements) - [Do 语句](#do_statements)
- [编译器控制语句](#compiler_control_statements) - [编译器控制语句](#compiler_control_statements)
- [编译配置语句](#build_config_statements) - [编译配置语句](#build_config_statements)
- [行控制语句](#line_control_statements) - [行控制语句](#line_control_statements)
- [可用性条件](#availability_condition) - [可用性条件](#availability_condition)
@ -50,17 +50,17 @@
> 语句语法 > 语句语法
<a name="statement"></a> <a name="statement"></a>
> *语句* → [*表达式*](04_Expressions.md#expression) **;**<sub>可选</sub> > *语句* → [*表达式*](04_Expressions.md#expression) **;**<sub>可选</sub>
> *语句* → [*声明*](05_Declarations.md#declaration) **;**<sub>可选</sub> > *语句* → [*声明*](05_Declarations.md#declaration) **;**<sub>可选</sub>
> *语句* → [*循环语句*](#loop-statement) **;**<sub>可选</sub> > *语句* → [*循环语句*](#loop-statement) **;**<sub>可选</sub>
> *语句* → [*分支语句*](#branch-statement) **;**<sub>可选</sub> > *语句* → [*分支语句*](#branch-statement) **;**<sub>可选</sub>
> *语句* → [*带标签的语句*](#labeled-statement) **;**<sub>可选</sub> > *语句* → [*带标签的语句*](#labeled-statement) **;**<sub>可选</sub>
> *语句* → [*控制转移语句*](#control-transfer-statement) **;**<sub>可选</sub> > *语句* → [*控制转移语句*](#control-transfer-statement) **;**<sub>可选</sub>
> *语句* → [*defer 语句*](#defer-statement) **;**<sub>可选</sub> > *语句* → [*defer 语句*](#defer-statement) **;**<sub>可选</sub>
> *语句* → [*do 语句*](#do-statement) **:**<sub>可选</sub> > *语句* → [*do 语句*](#do-statement) **:**<sub>可选</sub>
> *语句* → [*编译器控制语句*](#compiler-control-statement) > *语句* → [*编译器控制语句*](#compiler-control-statement)
<a name="statements"></a> <a name="statements"></a>
> *多条语句* → [*语句*](#statement) [*多条语句*](#statements)<sub>可选</sub> > *多条语句* → [*语句*](#statement) [*多条语句*](#statements)<sub>可选</sub>
<a name="loop_statements"></a> <a name="loop_statements"></a>
## 循环语句 ## 循环语句
@ -69,12 +69,12 @@
通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。 通过 `break` 语句和 `continue` 语句可以改变循环语句的控制流。有关这两条语句,详情参见 [Break 语句](#break_statement) 和 [Continue 语句](#continue_statement)。
> 循环语句语法 > 循环语句语法
> >
<a name="loop-statement"></a> <a name="loop-statement"></a>
> *循环语句* → [*for-in 语句*](#for-in-statement) > *循环语句* → [*for-in 语句*](#for-in-statement)
> *循环语句* → [*while 语句*](#while-statement) > *循环语句* → [*while 语句*](#while-statement)
> *循环语句* → [*repeat-while 语句*](#repeat-while-statement) > *循环语句* → [*repeat-while 语句*](#repeat-while-statement)
<a name="for-in_statements"></a> <a name="for-in_statements"></a>
### For-In 语句 ### For-In 语句
@ -91,7 +91,7 @@ for item in collection {
`for-in` 语句在循环开始前会调用集合表达式的 `generate()` 方法来获取一个实现了 `GeneratorType` 协议的类型的值。接下来循环开始,反复调用该值的 `next()` 方法。如果其返回值不是 `None`,它将会被赋给“项”,然后执行循环体语句,执行完毕后回到循环开始处,继续重复这一过程;否则,既不会赋值也不会执行循环体语句,`for-in` 语句至此执行完毕。 `for-in` 语句在循环开始前会调用集合表达式的 `generate()` 方法来获取一个实现了 `GeneratorType` 协议的类型的值。接下来循环开始,反复调用该值的 `next()` 方法。如果其返回值不是 `None`,它将会被赋给“项”,然后执行循环体语句,执行完毕后回到循环开始处,继续重复这一过程;否则,既不会赋值也不会执行循环体语句,`for-in` 语句至此执行完毕。
> for-in 语句语法 > for-in 语句语法
> >
<a name="for-in-statement"></a> <a name="for-in-statement"></a>
> *for-in 语句* → **for** **case**<sub>可选</sub> [*模式*](07_Patterns.md#pattern) **in** [*表达式*](04_Expressions.md#expression) [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block) > *for-in 语句* → **for** **case**<sub>可选</sub> [*模式*](07_Patterns.md#pattern) **in** [*表达式*](04_Expressions.md#expression) [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.md#code-block)
@ -124,14 +124,14 @@ while condition {
> *while 语句* → **while** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block) > *while 语句* → **while** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block)
<a name="condition-clause"></a> <a name="condition-clause"></a>
> *条件子句* → [*表达式*](04_Expressions.md#expression) | [*表达式*](04_Expressions.md#expression) **,** [*条件列表*](#condition-list) > *条件子句* → [*表达式*](04_Expressions.md#expression) | [*表达式*](04_Expressions.md#expression) **,** [*条件列表*](#condition-list)
<a name="condition"></a> <a name="condition"></a>
> *条件* → [*表达式*](04_Expressions.md#expression) |[*可用性条件*](#availability-condition) | [*case 条件*](#case-condition) | [*可选绑定条件*](#optional-binding-condition) > *条件* → [*表达式*](04_Expressions.md#expression) |[*可用性条件*](#availability-condition) | [*case 条件*](#case-condition) | [*可选绑定条件*](#optional-binding-condition)
> >
<a name="case-condition"></a> <a name="case-condition"></a>
> *case 条件* → **case** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer) > *case 条件* → **case** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer)
<a name="optional-binding-condition"></a> <a name="optional-binding-condition"></a>
> *可选绑定条件* → **let** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer) | **var** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer) > *可选绑定条件* → **let** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer) | **var** [*模式*](07_Patterns.md#pattern) [*构造器*](05_Declarations.md#initializer)
<a name="repeat-while_statements"></a> <a name="repeat-while_statements"></a>
### Repeat-While 语句 ### Repeat-While 语句
@ -143,7 +143,7 @@ while condition {
```swift ```swift
repeat { repeat {
statements statements
} while condition } while condition
``` ```
`repeat-while` 语句的执行流程如下: `repeat-while` 语句的执行流程如下:
@ -158,7 +158,7 @@ repeat {
> repeat-while 语句语法 > repeat-while 语句语法
> >
<a name="repeat-while-statement"></a> <a name="repeat-while-statement"></a>
> *repeat-while 语句* → **repeat** [*代码块*](05_Declarations.md#code-block) **while** [*表达式*](04_Expressions.md#expression) > *repeat-while 语句* → **repeat** [*代码块*](05_Declarations.md#code-block) **while** [*表达式*](04_Expressions.md#expression)
<a name="branch_statements"></a> <a name="branch_statements"></a>
## 分支语句 ## 分支语句
@ -170,9 +170,9 @@ repeat {
> 分支语句语法 > 分支语句语法
> >
<a name="branch-statement"></a> <a name="branch-statement"></a>
> *分支语句* → [*if 语句*](#if-statement) > *分支语句* → [*if 语句*](#if-statement)
> *分支语句* → [*guard 语句*](#guard-statement) > *分支语句* → [*guard 语句*](#guard-statement)
> *分支语句* → [*switch 语句*](#switch-statement) > *分支语句* → [*switch 语句*](#switch-statement)
<a name="if_statements"></a> <a name="if_statements"></a>
### If 语句 ### If 语句
@ -213,44 +213,44 @@ if condition 1 {
`if` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.md#optional_binding)。 `if` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件语句也可以使用可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.md#optional_binding)。
> if 语句语法 > if 语句语法
> >
<a name="if-statement"></a> <a name="if-statement"></a>
> *if 语句* → **if** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block) [*else 子句*](#else-clause)<sub>可选</sub> > *if 语句* → **if** [*条件子句*](#condition-clause) [*代码块*](05_Declarations.md#code-block) [*else 子句*](#else-clause)<sub>可选</sub>
<a name="else-clause"></a> <a name="else-clause"></a>
> *else 子句* → **else** [*代码块*](05_Declarations.md#code-block) | **else** [*if 语句*](#if-statement) > *else 子句* → **else** [*代码块*](05_Declarations.md#code-block) | **else** [*if 语句*](#if-statement)
<a name="guard_statements"></a> <a name="guard_statements"></a>
### Guard 语句 ### Guard 语句
如果一个或者多个条件不成立,可用 `guard` 语句用来退出当前作用域。 如果一个或者多个条件不成立,可用 `guard` 语句用来退出当前作用域。
`guard` 语句的格式如下: `guard` 语句的格式如下:
```swift ```swift
guard condition else { guard condition else {
statements statements
} }
``` ```
`guard` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件也可以是一条可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.html#optional_binding)。 `guard` 语句中条件的结果必须是 Bool 类型或者 Bool 的桥接类型。另外,条件也可以是一条可选绑定,请参阅 [可选绑定](../chapter2/01_The_Basics.html#optional_binding)。
`guard` 语句中进行可选绑定的常量或者变量,其可用范围从声明开始直到作用域结束。 `guard` 语句中进行可选绑定的常量或者变量,其可用范围从声明开始直到作用域结束。
`guard` 语句必须有 `else` 子句,而且必须在该子句中调用 `Never` 返回类型的函数,或者使用下面的语句退出当前作用域: `guard` 语句必须有 `else` 子句,而且必须在该子句中调用 `Never` 返回类型的函数,或者使用下面的语句退出当前作用域:
* `return` * `return`
* `break` * `break`
* `continue` * `continue`
* `throw` * `throw`
关于控制转移语句,请参阅 [控制转移语句](#control_transfer_statements)。关于 `Never` 返回类型的函数,请参阅 [永不返回的函数](05_Declarations.md#rethrowing_functions_and_methods)。 关于控制转移语句,请参阅 [控制转移语句](#control_transfer_statements)。关于 `Never` 返回类型的函数,请参阅 [永不返回的函数](05_Declarations.md#rethrowing_functions_and_methods)。
> guard 语句语法 > guard 语句语法
> >
<a name="guard-statement"></a> <a name="guard-statement"></a>
> *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05_Declarations.html#code-block) > *guard 语句* → **guard** [*条件子句*](#condition-clause) **else** [*代码块*] (05_Declarations.html#code-block)
<a name="switch_statements"></a> <a name="switch_statements"></a>
### Switch 语句 ### Switch 语句
@ -269,7 +269,7 @@ case pattern 3 where condition,
statements statements
default: default:
statements statements
} }
``` ```
`switch` 语句会先计算*控制表达式*的值,然后与每一个 `case` 的模式进行匹配。如果匹配成功,程序将会执行对应的 `case` 中的语句。另外,每一个 `case` 都不能为空,也就是说在每一个 `case` 中必须至少有一条语句。如果你不想在匹配到的 `case` 中执行代码,只需在该 `case` 中写一条 `break` 语句即可。 `switch` 语句会先计算*控制表达式*的值,然后与每一个 `case` 的模式进行匹配。如果匹配成功,程序将会执行对应的 `case` 中的语句。另外,每一个 `case` 都不能为空,也就是说在每一个 `case` 中必须至少有一条语句。如果你不想在匹配到的 `case` 中执行代码,只需在该 `case` 中写一条 `break` 语句即可。
@ -299,21 +299,21 @@ case let (x, y) where x == y:
> switch 语句语法 > switch 语句语法
> >
<a name="switch-statement"></a> <a name="switch-statement"></a>
> *switch 语句* → **switch** [*表达式*](04_Expressions.html#expression) **{** [*switch-case 列表*](#switch-cases)<sub>可选</sub> **}** > *switch 语句* → **switch** [*表达式*](04_Expressions.html#expression) **{** [*switch-case 列表*](#switch-cases)<sub>可选</sub> **}**
<a name="switch-cases"></a> <a name="switch-cases"></a>
> *switch case 列表* → [*switch-case*](#switch-case) [*switch-case 列表*](#switch-cases)<sub>可选</sub> > *switch case 列表* → [*switch-case*](#switch-case) [*switch-case 列表*](#switch-cases)<sub>可选</sub>
<a name="switch-case"></a> <a name="switch-case"></a>
> *switch case* → [*case 标签*](#case-label) [*多条语句*](#statements) | [*default 标签*](#default-label) [*多条语句*](#statements) > *switch case* → [*case 标签*](#case-label) [*多条语句*](#statements) | [*default 标签*](#default-label) [*多条语句*](#statements)
<a name="case-label"></a> <a name="case-label"></a>
> *case 标签* → **case** [*case 项列表*](#case-item-list) **:** > *case 标签* → **case** [*case 项列表*](#case-item-list) **:**
<a name="case-item-list"></a> <a name="case-item-list"></a>
> *case 项列表* → [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> | [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> **,** [*case 项列表*](#case-item-list) > *case 项列表* → [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> | [*模式*](07_Patterns.md#pattern) [*where 子句*](#where-clause)<sub>可选</sub> **,** [*case 项列表*](#case-item-list)
<a name="default-label"></a> <a name="default-label"></a>
> *default 标签* → **default** **:** > *default 标签* → **default** **:**
> >
<a name="where-clause"></a> <a name="where-clause"></a>
> *where-clause* → **where** [*where 表达式*](#where-expression) > *where-clause* → **where** [*where 表达式*](#where-expression)
<a name="where-expression"></a> <a name="where-expression"></a>
> *where-expression* → [*表达式*](04_Expressions.md#expression) > *where-expression* → [*表达式*](04_Expressions.md#expression)
@ -326,17 +326,17 @@ case let (x, y) where x == y:
关于使用带标签的语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章中的 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。 关于使用带标签的语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章中的 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
> 带标签的语句语法 > 带标签的语句语法
> >
<a name="labeled-statement"></a> <a name="labeled-statement"></a>
> *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#loop-statement) > *带标签的语句* → [*语句标签*](#statement-label) [*循环语句*](#loop-statement)
> *带标签的语句* → [*语句标签*](#statement-label) [*if 语句*](#if-statement) > *带标签的语句* → [*语句标签*](#statement-label) [*if 语句*](#if-statement)
> *带标签的语句* → [*语句标签*](#statement-label) [*switch 语句*](#switch-statement) > *带标签的语句* → [*语句标签*](#statement-label) [*switch 语句*](#switch-statement)
> > *带标签的语句* → [*语句标签*](#statement-label) [*do 语句*](#sdo-statement) > > *带标签的语句* → [*语句标签*](#statement-label) [*do 语句*](#sdo-statement)
<a name="statement-label"></a> <a name="statement-label"></a>
> *语句标签* → [*标签名称*](#label-name) **:** > *语句标签* → [*标签名称*](#label-name) **:**
<a name="label-name"></a> <a name="label-name"></a>
> *标签名称* → [*标识符*](02_Lexical_Structure.md#identifier) > *标签名称* → [*标识符*](02_Lexical_Structure.md#identifier)
<a name="control_transfer_statements"></a> <a name="control_transfer_statements"></a>
## 控制转移语句 ## 控制转移语句
@ -346,11 +346,11 @@ case let (x, y) where x == y:
> 控制转移语句语法 > 控制转移语句语法
> >
<a name="control-transfer-statement"></a> <a name="control-transfer-statement"></a>
> *控制转移语句* → [*break 语句*](#break-statement) > *控制转移语句* → [*break 语句*](#break-statement)
> *控制转移语句* → [*continue 语句*](#continue-statement) > *控制转移语句* → [*continue 语句*](#continue-statement)
> *控制转移语句* → [*fallthrough 语句*](#fallthrough-statement) > *控制转移语句* → [*fallthrough 语句*](#fallthrough-statement)
> *控制转移语句* → [*return 语句*](#return-statement) > *控制转移语句* → [*return 语句*](#return-statement)
> *控制转移语句* → [*throw 语句*](#throw-statement) > *控制转移语句* → [*throw 语句*](#throw-statement)
<a name="break_statement"></a> <a name="break_statement"></a>
### Break 语句 ### Break 语句
@ -368,10 +368,10 @@ case let (x, y) where x == y:
关于使用 `break` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Break](../chapter2/05_Control_Flow.html#break) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。 关于使用 `break` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Break](../chapter2/05_Control_Flow.html#break) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
> break 语句语法 > break 语句语法
> >
<a name="break-statement"></a> <a name="break-statement"></a>
> *break 语句* → **break** [*标签名称*](#label-name)<sub>可选</sub> > *break 语句* → **break** [*标签名称*](#label-name)<sub>可选</sub>
<a name="continue_statement"></a> <a name="continue_statement"></a>
### Continue 语句 ### Continue 语句
@ -391,7 +391,7 @@ case let (x, y) where x == y:
关于使用 `continue` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Continue](../chapter2/05_Control_Flow.html#continue) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。 关于使用 `continue` 语句的例子,请参阅 [控制流](../chapter2/05_Control_Flow.html) 一章的 [Continue](../chapter2/05_Control_Flow.html#continue) 和 [带标签的语句](../chapter2/05_Control_Flow.html#labeled_statements)。
> continue 语句语法 > continue 语句语法
> >
<a name="continue-statement"></a> <a name="continue-statement"></a>
> *continue 语句* → **continue** [*标签名称*](#label-name)<sub>可选</sub> > *continue 语句* → **continue** [*标签名称*](#label-name)<sub>可选</sub>
@ -422,22 +422,22 @@ case let (x, y) where x == y:
`return` 语句后面带表达式时表达式的值将会返回给调用函数或方法。如果表达式的值的类型与函数或者方法声明的返回类型不匹配Swift 则会在返回表达式的值之前将表达式的值的类型转换为返回类型。 `return` 语句后面带表达式时表达式的值将会返回给调用函数或方法。如果表达式的值的类型与函数或者方法声明的返回类型不匹配Swift 则会在返回表达式的值之前将表达式的值的类型转换为返回类型。
> 注意 > 注意
> >
> 正如 [可失败构造器](05_Declarations.html#failable_initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。 > 正如 [可失败构造器](05_Declarations.html#failable_initializers) 中所描述的,`return nil` 在可失败构造器中用于表明构造失败。
而只写 `return` 时,仅仅是从该函数或方法中返回,而不返回任何值(也就是说,函数或方法的返回类型为 `Void` 或者说 `()`)。 而只写 `return` 时,仅仅是从该函数或方法中返回,而不返回任何值(也就是说,函数或方法的返回类型为 `Void` 或者说 `()`)。
> return 语句语法 > return 语句语法
> >
<a name="return-statement"></a> <a name="return-statement"></a>
> *return 语句* → **return** [*表达式*](04_Expressions.html#expression)<sub>可选</sub> > *return 语句* → **return** [*表达式*](04_Expressions.html#expression)<sub>可选</sub>
<a name="throw_statements"></a> <a name="throw_statements"></a>
### Throw 语句 ### Throw 语句
`throw` 语句出现在抛出函数或者抛出方法体内,或者类型被 `throws` 关键字标记的闭包表达式体内。 `throw` 语句出现在抛出函数或者抛出方法体内,或者类型被 `throws` 关键字标记的闭包表达式体内。
`throw` 语句使程序在当前作用域结束执行,并向外围作用域传播错误。抛出的错误会一直传递,直到被 `do` 语句的 `catch` 子句处理掉。 `throw` 语句使程序在当前作用域结束执行,并向外围作用域传播错误。抛出的错误会一直传递,直到被 `do` 语句的 `catch` 子句处理掉。
`throw` 语句由 `throw` 关键字紧跟一个表达式组成,如下所示: `throw` 语句由 `throw` 关键字紧跟一个表达式组成,如下所示:
@ -448,7 +448,7 @@ case let (x, y) where x == y:
关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html) 一章的 [用 throwing 函数传递错误](../chapter2/18_Error_Handling.html#propagating_errors_using_throwing_functions)。 关于如何使用 `throw` 语句的例子,请参阅 [错误处理](../chapter2/18_Error_Handling.html) 一章的 [用 throwing 函数传递错误](../chapter2/18_Error_Handling.html#propagating_errors_using_throwing_functions)。
> throw 语句语法 > throw 语句语法
> >
<a name="throw-statement"></a> <a name="throw-statement"></a>
> *throw 语句* → **throw** [*表达式*](04_Expressions.html#expression) > *throw 语句* → **throw** [*表达式*](04_Expressions.html#expression)
@ -515,24 +515,24 @@ do {
关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../chapter2/17_Error_Handling.html#handling_errors)。 关于如何在 `do` 语句中使用一系列 `catch` 子句的例子,请参阅 [错误处理](../chapter2/17_Error_Handling.html#handling_errors)。
> do 语句语法 > do 语句语法
> >
<a name="do-statement"></a> <a name="do-statement"></a>
> *do 语句* → **do** [*代码块*](05_Declarations.html#code-block) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub> > *do 语句* → **do** [*代码块*](05_Declarations.html#code-block) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
<a name="catch-clauses"></a> <a name="catch-clauses"></a>
> *多条 catch 子句* → [*catch 子句*](#catch-clause) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub> > *多条 catch 子句* → [*catch 子句*](#catch-clause) [*多条 catch 子句*](#catch-clauses)<sub>可选</sub>
<a name="catch-clause"></a> <a name="catch-clause"></a>
> *catch 子句* → **catch** [*模式*](07_Patterns.html#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.html#code-block) > *catch 子句* → **catch** [*模式*](07_Patterns.html#pattern)<sub>可选</sub> [*where 子句*](#where-clause)<sub>可选</sub> [*代码块*](05_Declarations.html#code-block)
<a name="compiler_control_statements"></a> <a name="compiler_control_statements"></a>
## 编译器控制语句 ## 编译器控制语句
编译器控制语句允许程序改变编译器的行为。Swift 有两种编译器控制语句:编译配置语句和线路控制语句。 编译器控制语句允许程序改变编译器的行为。Swift 有两种编译器控制语句:编译配置语句和线路控制语句。
> 编译器控制语句语法 > 编译器控制语句语法
> >
<a name="compiler-control-statement"></a> <a name="compiler-control-statement"></a>
> *编译器控制语句* → [*编译配置语句*](#build-config-statement) > *编译器控制语句* → [*编译配置语句*](#build-config-statement)
> *编译器控制语句* → [*线路控制语句*](#line-control-statement) > *编译器控制语句* → [*线路控制语句*](#line-control-statement)
<a name="Conditional_Compilation_Block"></a> <a name="Conditional_Compilation_Block"></a>
@ -560,7 +560,7 @@ statements
`swift()`(语言版本检测函数)的版本号参数主要由主版本号和次版本号组成并且使用点号(`.`)分隔开,`>=` 和版本号之间不能有空格。 `swift()`(语言版本检测函数)的版本号参数主要由主版本号和次版本号组成并且使用点号(`.`)分隔开,`>=` 和版本号之间不能有空格。
> 注意 > 注意
> >
> `arch(arm)` 平台检测函数在 ARM 64 位设备上不会返回 `true`。如果代码在 32 位的 iOS 模拟器上编译,`arch(i386)` 平台检测函数会返回 `true`。 > `arch(arm)` 平台检测函数在 ARM 64 位设备上不会返回 `true`。如果代码在 32 位的 iOS 模拟器上编译,`arch(i386)` 平台检测函数会返回 `true`。
@ -578,7 +578,7 @@ statements to compile if both compilation conditions are false
#endif #endif
``` ```
> 注意 > 注意
> >
> 即使没有被编译,编译配置中的语句仍然会被解析。然而,唯一的例外是编译配置语句中包含语言版本检测函数:仅当 `Swift` 编译器版本和语言版本检测函数中指定的版本号匹配时,语句才会被解析。这种设定能确保旧的编译器不会尝试去解析新 Swift 版本的语法。 > 即使没有被编译,编译配置中的语句仍然会被解析。然而,唯一的例外是编译配置语句中包含语言版本检测函数:仅当 `Swift` 编译器版本和语言版本检测函数中指定的版本号匹配时,语句才会被解析。这种设定能确保旧的编译器不会尝试去解析新 Swift 版本的语法。
@ -586,35 +586,35 @@ statements to compile if both compilation conditions are false
> 编译配置语句语法 > 编译配置语句语法
> >
<a name="build-configuration-statement"></a> <a name="build-configuration-statement"></a>
> *单个编译配置语句* → **if** [*编译配置*](#build-configuration) [*语句*](#statements)<sub>可选</sub> [*多个编译配置 elseif 子句*](#build-configuration-elseif-clauses)<sub>可选</sub> **-** [*单个编译配置 else 子句*](#build-configuration-else-clause)<sub>可选</sub> **#endif** > *单个编译配置语句* → **if** [*编译配置*](#build-configuration) [*语句*](#statements)<sub>可选</sub> [*多个编译配置 elseif 子句*](#build-configuration-elseif-clauses)<sub>可选</sub> **-** [*单个编译配置 else 子句*](#build-configuration-else-clause)<sub>可选</sub> **#endif**
<a name="build-configuration-elseif-clauses"></a> <a name="build-configuration-elseif-clauses"></a>
> *多个编译配置 elseif 子句* → [*单个编译配置 elseif 子句*](#build-configuration-elseif-clause) [*多个编译配置 elseif 子句*](build-configuration-elseif-clauses)<sub>可选</sub> > *多个编译配置 elseif 子句* → [*单个编译配置 elseif 子句*](#build-configuration-elseif-clause) [*多个编译配置 elseif 子句*](build-configuration-elseif-clauses)<sub>可选</sub>
<a name="build-configuration-elseif-clause"></a> <a name="build-configuration-elseif-clause"></a>
> *单个编译配置 elseif 子句* → **#elseif** [*编译配置*](#build-configuration) [*语句*](#statements)<sub>可选</sub> > *单个编译配置 elseif 子句* → **#elseif** [*编译配置*](#build-configuration) [*语句*](#statements)<sub>可选</sub>
<a name="build-configuration-else-clause"></a> <a name="build-configuration-else-clause"></a>
> *单个编译配置 else 子句* → **#else** [*语句*](#statements)<sub>可选</sub> > *单个编译配置 else 子句* → **#else** [*语句*](#statements)<sub>可选</sub>
<a name="build-configuration"></a> <a name="build-configuration"></a>
> *编译配置* → [*平台检测函数*](#platform-testing-function) > *编译配置* → [*平台检测函数*](#platform-testing-function)
> *编译配置* → [*语言版本检测函数*](#language-version-testing-function) > *编译配置* → [*语言版本检测函数*](#language-version-testing-function)
> *编译配置* → [*标识符*](02_Lexical_Structure.md#identifier) > *编译配置* → [*标识符*](02_Lexical_Structure.md#identifier)
> *编译配置* → [*布尔值字面量*](02_Lexical_Structure.md#boolean-literal) > *编译配置* → [*布尔值字面量*](02_Lexical_Structure.md#boolean-literal)
> *编译配置* → **(** [*编译配置*](#build-configuration) **)** > *编译配置* → **(** [*编译配置*](#build-configuration) **)**
> *编译配置* → **!** [*编译配置*](#build-configuration) > *编译配置* → **!** [*编译配置*](#build-configuration)
> *编译配置* → [*编译配置*](#build-configuration) **&&** [*编译配置*](#build-configuration) > *编译配置* → [*编译配置*](#build-configuration) **&&** [*编译配置*](#build-configuration)
> *编译配置* → [*编译配置*](#build-configuration) **||** [*编译配置*](#build-configuration) > *编译配置* → [*编译配置*](#build-configuration) **||** [*编译配置*](#build-configuration)
<a name="platform-testing-function"></a> <a name="platform-testing-function"></a>
> *平台检测函数* → **os** **(** [*操作系统*](#operating-system) **)** > *平台检测函数* → **os** **(** [*操作系统*](#operating-system) **)**
> *平台检测函数* → **arch** **(** [*架构*](#architecture) **)** > *平台检测函数* → **arch** **(** [*架构*](#architecture) **)**
<a name="language-version-testing-function"></a> <a name="language-version-testing-function"></a>
> *语言版本检测函数* → **swift** **(** **>=** [*swift 版本*](#swift-version) **)** > *语言版本检测函数* → **swift** **(** **>=** [*swift 版本*](#swift-version) **)**
<a name="operating-system"></a> <a name="operating-system"></a>
> *操作系统* → **OSX** | **iOS** | **watchOS** | **tvOS** > *操作系统* → **OSX** | **iOS** | **watchOS** | **tvOS**
<a name="architecture"></a> <a name="architecture"></a>
> *架构* → **i386** | **x86_64** | **arm** | **arm64** > *架构* → **i386** | **x86_64** | **arm** | **arm64**
<a name="swift-version"></a> <a name="swift-version"></a>
> *swift 版本* → [*十进制数字*](02_Lexical_Structure.md#decimal-digit) ­**.** ­[*十进制数字*](02_Lexical_Structure.md#decimal-digit) > *swift 版本* → [*十进制数字*](02_Lexical_Structure.md#decimal-digit) ­**.** ­[*十进制数字*](02_Lexical_Structure.md#decimal-digit)
<a name="line_control_statements"></a> <a name="line_control_statements"></a>
### 行控制语句 ### 行控制语句
@ -634,12 +634,12 @@ statements to compile if both compilation conditions are false
<a name="line-control-statement"></a> <a name="line-control-statement"></a>
> 行控制语句语法 > 行控制语句语法
> >
> *行控制语句* → **#sourceLocation(file:[*文件名*](#file-name),line:[*行号*](#line-number))** > *行控制语句* → **#sourceLocation(file:[*文件名*](#file-name),line:[*行号*](#line-number))**
> *行控制语句* → **#sourceLocation()** > *行控制语句* → **#sourceLocation()**
<a name="line-number"></a> <a name="line-number"></a>
> *行号* → 大于 0 的十进制整数 > *行号* → 大于 0 的十进制整数
<a name="file-name"></a> <a name="file-name"></a>
> *文件名* → [*静态字符串字面量*](02_Lexical_Structure.md#static-string-literal) > *文件名* → [*静态字符串字面量*](02_Lexical_Structure.md#static-string-literal)
<a name="availability_condition"></a> <a name="availability_condition"></a>
### 可用性条件 ### 可用性条件
@ -659,25 +659,25 @@ if #available(platform name version, ..., *) {
使用可用性条件来执行一个代码块时,取决于使用的 API 在运行时是否可用,编译器会根据可用性条件提供的信息来决定是否执行相应的代码块。 使用可用性条件来执行一个代码块时,取决于使用的 API 在运行时是否可用,编译器会根据可用性条件提供的信息来决定是否执行相应的代码块。
可用性条件使用一系列逗号分隔的平台名称和版本。使用 `iOS``OSX`,以及 `watchOS` 等作为平台名称,并写上相应的版本号。`*` 参数是必须写的,用于处理未来的潜在平台。可用性条件确保了运行时的平台不低于条件中指定的平台版本时才执行代码块。 可用性条件使用一系列逗号分隔的平台名称和版本。使用 `iOS``OSX`,以及 `watchOS` 等作为平台名称,并写上相应的版本号。`*` 参数是必须写的,用于处理未来的潜在平台。可用性条件确保了运行时的平台不低于条件中指定的平台版本时才执行代码块。
与布尔类型的条件不同,不能用逻辑运算符 `&&``||` 组合可用性条件。 与布尔类型的条件不同,不能用逻辑运算符 `&&``||` 组合可用性条件。
> 可用性条件语法 > 可用性条件语法
> >
<a name="availability-condition"></a> <a name="availability-condition"></a>
> *可用性条件* → **#available** **(** [*可用性参数列表*](#availability-arguments) **)** > *可用性条件* → **#available** **(** [*可用性参数列表*](#availability-arguments) **)**
<a name="availability-arguments"></a> <a name="availability-arguments"></a>
> *可用性参数列表* → [*可用性参数*](#availability-argument) | [*可用性参数*](#availability-argument) **,** [*可用性参数列表*](#availability-arguments) > *可用性参数列表* → [*可用性参数*](#availability-argument) | [*可用性参数*](#availability-argument) **,** [*可用性参数列表*](#availability-arguments)
<a name="availability-argument"></a> <a name="availability-argument"></a>
> *可用性参数* → [平台名称](#platform-name) [平台版本](#platform-version) > *可用性参数* → [平台名称](#platform-name) [平台版本](#platform-version)
> *可用性条件* → __*__ > *可用性条件* → __*__
> >
<a name="platform-name"></a> <a name="platform-name"></a>
> *平台名称* → **iOS** | **iOSApplicationExtension** > *平台名称* → **iOS** | **iOSApplicationExtension**
> *平台名称* → **OSX** | **macOSApplicationExtension** > *平台名称* → **OSX** | **macOSApplicationExtension**
> *平台名称* → **watchOS** > *平台名称* → **watchOS**
> *平台名称* → **tvOS** > *平台名称* → **tvOS**
<a name="platform-version"></a> <a name="platform-version"></a>
> *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) > *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits)
> *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits) > *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits)
> *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits) > *平台版本* → [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits) **.** [十进制数字](02_Lexical_Structure.md#decimal-digits)

View File

@ -10,7 +10,7 @@
> 翻译+校对:[Lenhoon](https://github.com/Lenhoon), > 翻译+校对:[Lenhoon](https://github.com/Lenhoon),
> [BridgeQ](https://github.com/WXGBridgeQ) > [BridgeQ](https://github.com/WXGBridgeQ)
> 2.1 > 2.1
> 翻译:[mmoaay](https://github.com/mmoaay), [shanks](http://codebuild.me) > 翻译:[mmoaay](https://github.com/mmoaay), [shanks](http://codebuild.me)
> 校对:[shanks](http://codebuild.me) > 校对:[shanks](http://codebuild.me)
@ -70,22 +70,22 @@
> 声明语法 > 声明语法
> <a name="declaration"></a> > <a name="declaration"></a>
> *声明* → [*导入声明*](#import-declaration) > *声明* → [*导入声明*](#import-declaration)
> *声明* → [*常量声明*](#constant-declaration) > *声明* → [*常量声明*](#constant-declaration)
> *声明* → [*变量声明*](#variable-declaration) > *声明* → [*变量声明*](#variable-declaration)
> *声明* → [*类型别名声明*](#typealias-declaration) > *声明* → [*类型别名声明*](#typealias-declaration)
> *声明* → [*函数声明*](#function-declaration) > *声明* → [*函数声明*](#function-declaration)
> *声明* → [*枚举声明*](#enum-declaration) > *声明* → [*枚举声明*](#enum-declaration)
> *声明* → [*结构体声明*](#struct-declaration) > *声明* → [*结构体声明*](#struct-declaration)
> *声明* → [*类声明*](#class-declaration) > *声明* → [*类声明*](#class-declaration)
> *声明* → [*协议声明*](#protocol-declaration) > *声明* → [*协议声明*](#protocol-declaration)
> *声明* → [*构造器声明*](#initializer-declaration) > *声明* → [*构造器声明*](#initializer-declaration)
> *声明* → [*析构器声明*](#deinitializer-declaration) > *声明* → [*析构器声明*](#deinitializer-declaration)
> *声明* → [*扩展声明*](#extension-declaration) > *声明* → [*扩展声明*](#extension-declaration)
> *声明* → [*下标声明*](#subscript-declaration) > *声明* → [*下标声明*](#subscript-declaration)
> *声明* → [*运算符声明*](#operator-declaration) > *声明* → [*运算符声明*](#operator-declaration)
> <a name="declarations"></a> > <a name="declarations"></a>
> *多条声明* → [*声明*](#declaration) [*多条声明*](#declarations)<sub>可选</sub> > *多条声明* → [*声明*](#declaration) [*多条声明*](#declarations)<sub>可选</sub>
<a name="top-level_code"></a> <a name="top-level_code"></a>
## 顶级代码 ## 顶级代码
@ -134,14 +134,14 @@ import 模块.子模块
> 导入声明语法 > 导入声明语法
> >
> <a name="import-declaration"></a> > <a name="import-declaration"></a>
> *导入声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **import** [*导入类型*](#import-kind)<sub>可选</sub> [*导入路径*](#import-path) > *导入声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **import** [*导入类型*](#import-kind)<sub>可选</sub> [*导入路径*](#import-path)
> >
> <a name="import-kind"></a> > <a name="import-kind"></a>
> *导入类型* → **typealias** | **struct** | **class** | **enum** | **protocol** | **let** | **var** | **func** > *导入类型* → **typealias** | **struct** | **class** | **enum** | **protocol** | **let** | **var** | **func**
> <a name="import-path"></a> > <a name="import-path"></a>
> *导入路径* → [*导入路径标识符*](#import-path-identifier) | [*导入路径标识符*](#import-path-identifier) **.** [*导入路径*](#import-path) > *导入路径* → [*导入路径标识符*](#import-path-identifier) | [*导入路径标识符*](#import-path-identifier) **.** [*导入路径*](#import-path)
> <a name="import-path-identifier"></a> > <a name="import-path-identifier"></a>
> *导入路径标识符* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator) > *导入路径标识符* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator)
<a name="constant_declaration"></a> <a name="constant_declaration"></a>
## 常量声明 ## 常量声明
@ -181,13 +181,13 @@ print("The second number is \(secondNumber).")
> 常量声明语法 > 常量声明语法
> >
> <a name="constant-declaration"></a> > <a name="constant-declaration"></a>
> *常量声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **let** [*模式构造器列表*](pattern-initializer-list) > *常量声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **let** [*模式构造器列表*](pattern-initializer-list)
> <a name="pattern-initializer-list"></a> > <a name="pattern-initializer-list"></a>
> *模式构造器列表* → [*模式构造器*](#pattern-initializer) | [*模式构造器*](#pattern-initializer) **,** [*模式构造器列表*](#pattern-initializer-list) > *模式构造器列表* → [*模式构造器*](#pattern-initializer) | [*模式构造器*](#pattern-initializer) **,** [*模式构造器列表*](#pattern-initializer-list)
> <a name="pattern-initializer"></a> > <a name="pattern-initializer"></a>
> *模式构造器* → [*模式*](07_Patterns.html#pattern) [*构造器*](#initializer)<sub>可选</sub> > *模式构造器* → [*模式*](07_Patterns.html#pattern) [*构造器*](#initializer)<sub>可选</sub>
> <a name="initializer"></a> > <a name="initializer"></a>
> *构造器* → **=** [*表达式*](04_Expressions.html#expression) > *构造器* → **=** [*表达式*](04_Expressions.html#expression)
<a name="variable_declaration"></a> <a name="variable_declaration"></a>
## 变量声明 ## 变量声明
@ -291,44 +291,44 @@ var 变量名称: 类型 = 表达式 {
> 变量声明语法 > 变量声明语法
> >
<a name="variable-declaration"></a> <a name="variable-declaration"></a>
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*模式构造器列表*](#pattern-initializer-list) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*模式构造器列表*](#pattern-initializer-list)
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*代码块*](#code-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*代码块*](#code-block)
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 代码块*](#getter-setter-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 代码块*](#getter-setter-block)
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*构造器*](#initializer) [*willSet-didSet 代码块*](#willSet-didSet-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*构造器*](#initializer) [*willSet-didSet 代码块*](#willSet-didSet-block)
> *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*构造器*](#initializer)<sub>可选</sub> [*willSet-didSet 代码块*](#willSet-didSet-block) > *变量声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*构造器*](#initializer)<sub>可选</sub> [*willSet-didSet 代码块*](#willSet-didSet-block)
<a name="variable-declaration-head"></a> <a name="variable-declaration-head"></a>
> *变量声明头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **var** > *变量声明头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **var**
> <a name="variable-name"></a> > <a name="variable-name"></a>
> *变量名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *变量名称* → [*标识符*](02_Lexical_Structure.html#identifier)
<a name="getter-setter-block"></a> <a name="getter-setter-block"></a>
> *getter-setter 代码块* → [*代码块*](#code-block) > *getter-setter 代码块* → [*代码块*](#code-block)
> *getter-setter 代码块* → **{** [*getter 子句*](#getter-clause) [*setter 子句*](#setter-clause)<sub>可选</sub> **}** > *getter-setter 代码块* → **{** [*getter 子句*](#getter-clause) [*setter 子句*](#setter-clause)<sub>可选</sub> **}**
> *getter-setter 代码块* → **{** [*setter 子句*](#setter-clause) [*getter 子句*](#getter-clause) **}** > *getter-setter 代码块* → **{** [*setter 子句*](#setter-clause) [*getter 子句*](#getter-clause) **}**
> <a name="getter-clause"></a> > <a name="getter-clause"></a>
> *getter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get** [*代码块*](#code-block) > *getter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get** [*代码块*](#code-block)
> <a name="setter-clause"></a> > <a name="setter-clause"></a>
> *setter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block) > *setter 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
> <a name="setter-name"></a> > <a name="setter-name"></a>
> *setter 名称* → **(** [*标识符*](02_Lexical_Structure.html#identifier) **)** > *setter 名称* → **(** [*标识符*](02_Lexical_Structure.html#identifier) **)**
<a name="getter-setter-keyword-block"></a> <a name="getter-setter-keyword-block"></a>
> *getter-setter 关键字代码块* → **{** [*getter 关键字子句*](#getter-keyword-clause) [*setter 关键字子句*](#setter-keyword-clause)<sub>可选</sub> **}** > *getter-setter 关键字代码块* → **{** [*getter 关键字子句*](#getter-keyword-clause) [*setter 关键字子句*](#setter-keyword-clause)<sub>可选</sub> **}**
> *getter-setter 关键字代码块* → **{** [*setter 关键字子句*](#setter-keyword-clause) [*getter 关键字子句*](#getter-keyword-clause) **}** > *getter-setter 关键字代码块* → **{** [*setter 关键字子句*](#setter-keyword-clause) [*getter 关键字子句*](#getter-keyword-clause) **}**
> <a name="getter-keyword-clause"></a> > <a name="getter-keyword-clause"></a>
> *getter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get** > *getter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **get**
> <a name="setter-keyword-clause"></a> > <a name="setter-keyword-clause"></a>
> *setter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set** > *setter 关键字子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **set**
<a name="willSet-didSet-block"></a> <a name="willSet-didSet-block"></a>
> *willSet-didSet 代码块* → **{** [*willSet 子句*](#willSet-clause) [*didSet 子句*](#didSet-clause)<sub>可选</sub> **}** > *willSet-didSet 代码块* → **{** [*willSet 子句*](#willSet-clause) [*didSet 子句*](#didSet-clause)<sub>可选</sub> **}**
> *willSet-didSet 代码块* → **{** [*didSet 子句*](#didSet-clause) [*willSet 子句*](#willSet-clause)<sub>可选</sub> **}** > *willSet-didSet 代码块* → **{** [*didSet 子句*](#didSet-clause) [*willSet 子句*](#willSet-clause)<sub>可选</sub> **}**
> <a name="willSet-clause"></a> > <a name="willSet-clause"></a>
> *willSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **willSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block) > *willSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **willSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
> <a name="didSet-clause"></a> > <a name="didSet-clause"></a>
> *didSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **didSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block) > *didSet 子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **didSet** [*setter 名称*](#setter-name)<sub>可选</sub> [*代码块*](#code-block)
<a name="type_alias_declaration"></a> <a name="type_alias_declaration"></a>
## 类型别名声明 ## 类型别名声明
@ -344,7 +344,7 @@ typealias 类型别名 = 现存类型
```swift ```swift
typealias StringDictionary<Value> = Dictionary<String, Value> typealias StringDictionary<Value> = Dictionary<String, Value>
// 下列两个字典拥有同样的类型 // 下列两个字典拥有同样的类型
var dictionary1: StringDictionary<Int> = [:] var dictionary1: StringDictionary<Int> = [:]
var dictionary2: Dictionary<String, Int> = [:] var dictionary2: Dictionary<String, Int> = [:]
@ -364,7 +364,7 @@ protocol Sequence {
associatedtype Iterator: IteratorProtocol associatedtype Iterator: IteratorProtocol
typealias Element = Iterator.Element typealias Element = Iterator.Element
} }
func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int { func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
// ... // ...
} }
@ -378,13 +378,13 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
> 类型别名声明语法 > 类型别名声明语法
> >
> <a name="typealias-declaration"></a> > <a name="typealias-declaration"></a>
> *类型别名声明* → [*类型别名头*](#typealias-head) [*类型别名赋值*](#typealias-assignment) > *类型别名声明* → [*类型别名头*](#typealias-head) [*类型别名赋值*](#typealias-assignment)
> <a name="typealias-head"></a> > <a name="typealias-head"></a>
> *类型别名头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **typealias** [*类型别名名称*](#typealias-name) > *类型别名头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> **typealias** [*类型别名名称*](#typealias-name)
> <a name="typealias-name"></a> > <a name="typealias-name"></a>
> *类型别名名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *类型别名名称* → [*标识符*](02_Lexical_Structure.html#identifier)
> <a name="typealias-assignment"></a> > <a name="typealias-assignment"></a>
> *类型别名赋值* → **=** [*类型*](03_Types.html#type) > *类型别名赋值* → **=** [*类型*](03_Types.html#type)
<a name="function_declaration"></a> <a name="function_declaration"></a>
## 函数声明 ## 函数声明
@ -393,7 +393,7 @@ func sum<T: Sequence>(_ sequence: T) -> Int where T.Element == Int {
```swift ```swift
func 函数名称(参数列表) -> 返回类型 { func 函数名称(参数列表) -> 返回类型 {
语句 语句
} }
``` ```
@ -565,37 +565,37 @@ Swift 定义了 `Never` 类型,它表示函数或者方法不会返回给它
> 函数声明语法 > 函数声明语法
> >
<a name="function-declaration"></a> <a name="function-declaration"></a>
> *函数声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) [*函数体*](#function-body)<sub>可选</sub> > *函数声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) [*函数体*](#function-body)<sub>可选</sub>
<a name="function-head"></a> <a name="function-head"></a>
> *函数头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **func** > *函数头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **func**
> <a name="function-name"></a> > <a name="function-name"></a>
> *函数名* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator) > *函数名* → [*标识符*](02_Lexical_Structure.html#identifier) | [*运算符*](02_Lexical_Structure.html#operator)
<a name="function-signature"></a> <a name="function-signature"></a>
> *函数签名* → [*参数子句列表*](#parameter-clauses) **throws**<sub>可选</sub> [*函数结果*](#function-result)<sub>可选</sub> > *函数签名* → [*参数子句列表*](#parameter-clauses) **throws**<sub>可选</sub> [*函数结果*](#function-result)<sub>可选</sub>
> *函数签名* → [*参数子句列表*](#parameter-clauses) **rethrows** [*函数结果*](#function-result)<sub>可选</sub> > *函数签名* → [*参数子句列表*](#parameter-clauses) **rethrows** [*函数结果*](#function-result)<sub>可选</sub>
> <a name="function-result"></a> > <a name="function-result"></a>
> *函数结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type) > *函数结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type)
> <a name="function-body"></a> > <a name="function-body"></a>
> *函数体* → [*代码块*](#code-block) > *函数体* → [*代码块*](#code-block)
<a name="parameter-clauses"></a> <a name="parameter-clauses"></a>
> *参数子句列表* → [*参数子句*](#parameter-clause) [*参数子句列表*](#parameter-clauses)<sub>可选</sub> > *参数子句列表* → [*参数子句*](#parameter-clause) [*参数子句列表*](#parameter-clauses)<sub>可选</sub>
> <a name="parameter-clause"></a> > <a name="parameter-clause"></a>
> *参数子句* → **(** **)** | **(** [*参数列表*](#parameter-list) **)** > *参数子句* → **(** **)** | **(** [*参数列表*](#parameter-list) **)**
> <a name="parameter-list"></a> > <a name="parameter-list"></a>
> *参数列表* → [*参数*](#parameter) | [*参数*](#parameter) **,** [*参数列表*](#parameter-list) > *参数列表* → [*参数*](#parameter) | [*参数*](#parameter) **,** [*参数列表*](#parameter-list)
> <a name="parameter"></a> > <a name="parameter"></a>
> *参数* → **let**<sub>可选</sub> [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) [*默认参数子句*](#default-argument-clause)<sub>可选</sub> > *参数* → **let**<sub>可选</sub> [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) [*默认参数子句*](#default-argument-clause)<sub>可选</sub>
> *参数* → **inout** [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) > *参数* → **inout** [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation)
> *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) **...** > *参数* → [*外部参数名*](#external-parameter-name)<sub>可选</sub> [*内部参数名*](#local-parameter-name) [*类型标注*](03_Types.html#type-annotation) **...**
> <a name="external-parameter-name"></a> > <a name="external-parameter-name"></a>
> *外部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_** > *外部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_**
> <a name="local-parameter-name"></a> > <a name="local-parameter-name"></a>
> *内部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_** > *内部参数名* → [*标识符*](02_Lexical_Structure.html#identifier) | **_**
> <a name="default-argument-clause"></a> > <a name="default-argument-clause"></a>
> *默认参数子句* → **=** [*表达式*](04_Expressions.html#expression) > *默认参数子句* → **=** [*表达式*](04_Expressions.html#expression)
<a name="enumeration_declaration"></a> <a name="enumeration_declaration"></a>
## 枚举声明 ## 枚举声明
@ -707,45 +707,45 @@ enum WeekendDay: String {
可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../chapter2/08_Enumerations.html#matching_enumeration_values_with_a_switch_statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](07_Patterns.html#enumeration_case_pattern) 所述。 可以使用 `switch` 语句来检验枚举用例的值,正如 [使用 switch 语句匹配枚举值](../chapter2/08_Enumerations.html#matching_enumeration_values_with_a_switch_statement) 所述。枚举类型是模式匹配的,依靠 `switch` 语句 `case` 块中的枚举用例模式,正如 [枚举用例模式](07_Patterns.html#enumeration_case_pattern) 所述。
<a name="grammer_of_an_enumeration_declaration"></a> <a name="grammer_of_an_enumeration_declaration"></a>
> 枚举声明语法 > 枚举声明语法
> >
<a name="enum-declaration"></a> <a name="enum-declaration"></a>
> *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> [*联合风格枚举*](#union-style-enum) > *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier)<sub>可选</sub> [*联合风格枚举*](#union-style-enum)
> *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> [*原始值风格枚举*](#raw-value-style-enum) > *枚举声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> [*原始值风格枚举*](#raw-value-style-enum)
<a name="union-style-enum"></a> <a name="union-style-enum"></a>
> *联合风格枚举* → **indirect**<sub>可选</sub> **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause)<sub>可选</sub> **{** [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub> **}** > *联合风格枚举* → **indirect**<sub>可选</sub> **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause)<sub>可选</sub> **{** [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub> **}**
> <a name="union-style-enum-members"></a> > <a name="union-style-enum-members"></a>
> *多个联合风格枚举成员* → [*联合风格枚举成员*](#union-style-enum-member) [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub> > *多个联合风格枚举成员* → [*联合风格枚举成员*](#union-style-enum-member) [*多个联合风格枚举成员*](#union-style-enum-members)<sub>可选</sub>
> <a name="union-style-enum-member"></a> > <a name="union-style-enum-member"></a>
> *联合风格枚举成员* → [*声明*](#declaration) | [*联合风格枚举用例子句*](#union-style-enum-case-clause) > *联合风格枚举成员* → [*声明*](#declaration) | [*联合风格枚举用例子句*](#union-style-enum-case-clause)
> <a name="union-style-enum-case-clause"></a> > <a name="union-style-enum-case-clause"></a>
> *联合风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **indirect**<sub>可选</sub> **case** [*联合风格枚举用例列表*](#union-style-enum-case-list) > *联合风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **indirect**<sub>可选</sub> **case** [*联合风格枚举用例列表*](#union-style-enum-case-list)
> <a name="union-style-enum-case-list"></a> > <a name="union-style-enum-case-list"></a>
> *联合风格枚举用例列表* → [*联合风格枚举用例*](#union-style-enum-case) | [*联合风格枚举用例*](#union-style-enum-case) **,** [*联合风格枚举用例列表*](#union-style-enum-case-list) > *联合风格枚举用例列表* → [*联合风格枚举用例*](#union-style-enum-case) | [*联合风格枚举用例*](#union-style-enum-case) **,** [*联合风格枚举用例列表*](#union-style-enum-case-list)
> <a name="union-style-enum-case"></a> > <a name="union-style-enum-case"></a>
> *联合风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*元组类型*](03_Types.html#tuple-type)<sub>可选</sub> > *联合风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*元组类型*](03_Types.html#tuple-type)<sub>可选</sub>
> <a name="enum-name"></a> > <a name="enum-name"></a>
> *枚举名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *枚举名称* → [*标识符*](02_Lexical_Structure.html#identifier)
> <a name="enum-case-name"></a> > <a name="enum-case-name"></a>
> *枚举用例名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *枚举用例名称* → [*标识符*](02_Lexical_Structure.html#identifier)
<a name="raw-value-style-enum"></a> <a name="raw-value-style-enum"></a>
> *原始值风格枚举* → **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause) **{** [*多个原始值风格枚举成员*](#raw-value-style-enum-members) **}** > *原始值风格枚举* → **enum** [*枚举名称*](#enum-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause) **{** [*多个原始值风格枚举成员*](#raw-value-style-enum-members) **}**
> <a name="raw-value-style-enum-members"></a> > <a name="raw-value-style-enum-members"></a>
> *多个原始值风格枚举成员* → [*原始值风格枚举成员*](#raw-value-style-enum-member) [*多个原始值风格枚举成员*](#raw-value-style-enum-members)<sub>可选</sub> > *多个原始值风格枚举成员* → [*原始值风格枚举成员*](#raw-value-style-enum-member) [*多个原始值风格枚举成员*](#raw-value-style-enum-members)<sub>可选</sub>
> <a name="raw-value-style-enum-member"></a> > <a name="raw-value-style-enum-member"></a>
> *原始值风格枚举成员* → [*声明*](#declaration) | [*原始值风格枚举用例子句*](#raw-value-style-enum-case-clause) > *原始值风格枚举成员* → [*声明*](#declaration) | [*原始值风格枚举用例子句*](#raw-value-style-enum-case-clause)
> <a name="raw-value-style-enum-case-clause"></a> > <a name="raw-value-style-enum-case-clause"></a>
> *原始值风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **case** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list) > *原始值风格枚举用例子句* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **case** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list)
> <a name="raw-value-style-enum-case-list"></a> > <a name="raw-value-style-enum-case-list"></a>
> *原始值风格枚举用例列表* → [*原始值风格枚举用例*](#raw-value-style-enum-case) | [*原始值风格枚举用例*](#raw-value-style-enum-case) **,** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list) > *原始值风格枚举用例列表* → [*原始值风格枚举用例*](#raw-value-style-enum-case) | [*原始值风格枚举用例*](#raw-value-style-enum-case) **,** [*原始值风格枚举用例列表*](#raw-value-style-enum-case-list)
> <a name="raw-value-style-enum-case"></a> > <a name="raw-value-style-enum-case"></a>
> *原始值风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*原始值赋值*](#raw-value-assignment)<sub>可选</sub> > *原始值风格枚举用例* → [*枚举用例名称*](#enum-case-name) [*原始值赋值*](#raw-value-assignment)<sub>可选</sub>
> <a name="raw-value-assignment"></a> > <a name="raw-value-assignment"></a>
> *原始值赋值* → **=** [*原始值字面量*](#raw-value-literal) > *原始值赋值* → **=** [*原始值字面量*](#raw-value-literal)
> <a name="raw-value-literal"></a> > <a name="raw-value-literal"></a>
> *原始值字面量* → [数字型字面量](02_Lexical_Structure.html#numeric-literal) | [字符串型字面量](02_Lexical_Structure.html#static-string-literal) | [布尔型字面量](02_Lexical_Structure.html#boolean-literal) > *原始值字面量* → [数字型字面量](02_Lexical_Structure.html#numeric-literal) | [字符串型字面量](02_Lexical_Structure.html#static-string-literal) | [布尔型字面量](02_Lexical_Structure.html#boolean-literal)
<a name="structure_declaration"></a> <a name="structure_declaration"></a>
## 结构体声明 ## 结构体声明
@ -783,11 +783,11 @@ struct 结构体名称: 采纳的协议 {
> 结构体声明语法 > 结构体声明语法
> >
> <a name="struct-declaration"></a> > <a name="struct-declaration"></a>
> *结构体声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> **struct** [*结构体名称*](#struct-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*结构体主体*](#struct-body) > *结构体声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*访问级别修饰符*](#access-level-modifier) <sub>可选</sub> **struct** [*结构体名称*](#struct-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [类型继承子句](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*结构体主体*](#struct-body)
> <a name="struct-name"></a> > <a name="struct-name"></a>
> *结构体名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *结构体名称* → [*标识符*](02_Lexical_Structure.html#identifier)
> <a name="struct-body"></a> > <a name="struct-body"></a>
> *结构体主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}** > *结构体主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
<a name="class_declaration"></a> <a name="class_declaration"></a>
## 类声明 ## 类声明
@ -828,11 +828,11 @@ class 类名: 超类, 采纳的协议 {
> 类声明语法 > 类声明语法
> >
> <a name="class-declaration"></a> > <a name="class-declaration"></a>
> *类声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*类主体*](#class-body) > *类声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **class** [*类名*](#class-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*类主体*](#class-body)
> <a name="class-name"></a> > <a name="class-name"></a>
> *类名* → [*标识符*](02_Lexical_Structure.html#identifier) > *类名* → [*标识符*](02_Lexical_Structure.html#identifier)
> <a name="class-body"></a> > <a name="class-body"></a>
> *类主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}** > *类主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
<a name="protocol_declaration"></a> <a name="protocol_declaration"></a>
## 协议声明 ## 协议声明
@ -879,20 +879,20 @@ protocol SomeProtocol: class {
> 协议声明语法 > 协议声明语法
> >
<a name="protocol-declaration"></a> <a name="protocol-declaration"></a>
> *协议声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*协议主体*](#protocol-body) > *协议声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **protocol** [*协议名称*](#protocol-name) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*协议主体*](#protocol-body)
> <a name="protocol-name"></a> > <a name="protocol-name"></a>
> *协议名称* → [*标识符*](02_Lexical_Structure.html#identifier) > *协议名称* → [*标识符*](02_Lexical_Structure.html#identifier)
> <a name="protocol-body"></a> > <a name="protocol-body"></a>
> *协议主体* → **{** [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub> **}** > *协议主体* → **{** [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub> **}**
> >
<a name="protocol-member-declaration"></a> <a name="protocol-member-declaration"></a>
> *协议成员声明* → [*协议属性声明*](#protocol-property-declaration) > *协议成员声明* → [*协议属性声明*](#protocol-property-declaration)
> *协议成员声明* → [*协议方法声明*](#protocol-method-declaration) > *协议成员声明* → [*协议方法声明*](#protocol-method-declaration)
> *协议成员声明* → [*协议构造器声明*](#protocol-initializer-declaration) > *协议成员声明* → [*协议构造器声明*](#protocol-initializer-declaration)
> *协议成员声明* → [*协议下标声明*](#protocol-subscript-declaration) > *协议成员声明* → [*协议下标声明*](#protocol-subscript-declaration)
> *协议成员声明* → [*协议关联类型声明*](#protocol-associated-type-declaration) > *协议成员声明* → [*协议关联类型声明*](#protocol-associated-type-declaration)
> <a name="protocol-member-declarations"></a> > <a name="protocol-member-declarations"></a>
> *协议成员声明列表* → [*协议成员声明*](#protocol-member-declaration) [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub> > *协议成员声明列表* → [*协议成员声明*](#protocol-member-declaration) [*协议成员声明列表*](#protocol-member-declarations)<sub>可选</sub>
<a name="protocol_property_declaration"></a> <a name="protocol_property_declaration"></a>
### 协议属性声明 ### 协议属性声明
@ -913,7 +913,7 @@ var 属性名: 类型 { get set }
> 协议属性声明语法 > 协议属性声明语法
> >
> <a name="protocol-property-declaration"></a> > <a name="protocol-property-declaration"></a>
> *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > *协议属性声明* → [*变量声明头*](#variable-declaration-head) [*变量名称*](#variable-name) [*类型标注*](03_Types.html#type-annotation) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
<a name="protocol_method_declaration"></a> <a name="protocol_method_declaration"></a>
### 协议方法声明 ### 协议方法声明
@ -928,7 +928,7 @@ var 属性名: 类型 { get set }
> 协议方法声明语法 > 协议方法声明语法
> >
> <a name="protocol-method-declaration"></a> > <a name="protocol-method-declaration"></a>
> *协议方法声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature) > *协议方法声明* → [*函数头*](#function-head) [*函数名*](#function-name) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*函数签名*](#function-signature)
<a name="protocol_initializer_declaration"></a> <a name="protocol_initializer_declaration"></a>
### 协议构造器声明 ### 协议构造器声明
@ -946,8 +946,8 @@ var 属性名: 类型 { get set }
> 协议构造器声明语法 > 协议构造器声明语法
> >
> <a name="protocol-initializer-declaration"></a> > <a name="protocol-initializer-declaration"></a>
> *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub> > *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub>
> *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows** > *协议构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows**
<a name="protocol_subscript_declaration"></a> <a name="protocol_subscript_declaration"></a>
### 协议下标声明 ### 协议下标声明
@ -978,7 +978,7 @@ subscript (参数列表) -> 返回类型 { get set }
> 协议关联类型声明语法 > 协议关联类型声明语法
> >
> <a name="protocol-associated-type-declaration"></a> > <a name="protocol-associated-type-declaration"></a>
> *协议关联类型声明* → [*类型别名头*](#typealias-head) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*类型别名赋值*](#typealias-assignment)<sub>可选</sub> > *协议关联类型声明* → [*类型别名头*](#typealias-head) [*类型继承子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*类型别名赋值*](#typealias-assignment)<sub>可选</sub>
<a name="initializer_declaration"></a> <a name="initializer_declaration"></a>
## 构造器声明 ## 构造器声明
@ -1070,14 +1070,14 @@ if let actualInstance = SomeStruct(input: "Hello") {
> 构造器声明语法 > 构造器声明语法
> >
> <a name="initializer-declaration"></a> > <a name="initializer-declaration"></a>
> *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub> [*构造器主体*](#initializer-body) > *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **throws**<sub>可选</sub> [*构造器主体*](#initializer-body)
> *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows**<sub>可选</sub> [*构造器主体*](#initializer-body) > *构造器声明* → [*构造器头*](#initializer-head) [*泛型形参子句*](08_Generic_Parameters_and_Arguments.html#generic-parameter-clause)<sub>可选</sub> [*参数子句*](#parameter-clause) **rethrows**<sub>可选</sub> [*构造器主体*](#initializer-body)
> <a name="initializer-head"></a> > <a name="initializer-head"></a>
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** > *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init**
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **?** > *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **?**
> *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **!** > *构造器头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **init** **!**
> <a name="initializer-body"></a> > <a name="initializer-body"></a>
> *构造器主体* → [*代码块*](#code-block) > *构造器主体* → [*代码块*](#code-block)
<a name="deinitializer_declaration"></a> <a name="deinitializer_declaration"></a>
## 析构器声明 ## 析构器声明
@ -1102,7 +1102,7 @@ deinit {
> 析构器声明语法 > 析构器声明语法
> >
> <a name="deinitializer-declaration"></a> > <a name="deinitializer-declaration"></a>
> *析构器声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block) > *析构器声明* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> **deinit** [*代码块*](#code-block)
<a name="extension_declaration"></a> <a name="extension_declaration"></a>
## 扩展声明 ## 扩展声明
@ -1135,12 +1135,12 @@ extension 类型名称 where 要求 {
> 扩展声明语法 > 扩展声明语法
> >
> <a name="extension-declaration"></a> > <a name="extension-declaration"></a>
> *扩展声明* → [特性](06_Attributes.html#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.html#type-identifier) [*类型-继承-子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*扩展主体*](#extension-body) > *扩展声明* → [特性](06_Attributes.html#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.html#type-identifier) [*类型-继承-子句*](03_Types.html#type-inheritance-clause)<sub>可选</sub> [*扩展主体*](#extension-body)
> <a name="extension-body"></a> > <a name="extension-body"></a>
> *扩展声明* → [特性](06_Attributes.html#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.html#type-identifier) [*泛型-where-子句*](03_Types.html#type-inheritance-clause) [*扩展主体*](#extension-body) > *扩展声明* → [特性](06_Attributes.html#type_attributes)<sub>可选</sub> [访问级别修饰符](#access-level-modifier)<sub>可选</sub> **extension** [*类型标识符*](03_Types.html#type-identifier) [*泛型-where-子句*](03_Types.html#type-inheritance-clause) [*扩展主体*](#extension-body)
> *扩展主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}** > *扩展主体* → **{** [*多条声明*](#declarations)<sub>可选</sub> **}**
> *多条声明* → [单条声明](#subscript_declaration) [多条声明](#declarations) <sub>可选</sub> > *多条声明* → [单条声明](#subscript_declaration) [多条声明](#declarations) <sub>可选</sub>
> *单条声明* → [声明语句](#declarations) | [编译器-控制-语句](10_Statements.html#compiler-control-statement) > *单条声明* → [声明语句](#declarations) | [编译器-控制-语句](10_Statements.html#compiler-control-statement)
<a name="subscript_declaration"></a> <a name="subscript_declaration"></a>
## 下标声明 ## 下标声明
@ -1176,13 +1176,13 @@ subscript (参数列表) -> 返回类型 {
> 下标声明语法 > 下标声明语法
> >
> <a name="subscript-declaration"></a> > <a name="subscript-declaration"></a>
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*代码块*](#code-block) > *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*代码块*](#code-block)
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*getter-setter 代码块*](#getter-setter-block) > *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*getter-setter 代码块*](#getter-setter-block)
> *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*getter-setter 关键字代码块*](#getter-setter-keyword-block) > *下标声明* → [*下标头*](#subscript-head) [*下标结果*](#subscript-result) [*getter-setter 关键字代码块*](#getter-setter-keyword-block)
> <a name="subscript-head"></a> > <a name="subscript-head"></a>
> *下标头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **subscript** [*参数子句*](#parameter-clause) > *下标头* → [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> **subscript** [*参数子句*](#parameter-clause)
> <a name="subscript-result"></a> > <a name="subscript-result"></a>
> *下标结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type) > *下标结果* → **->** [*特性列表*](06_Attributes.html#attributes)<sub>可选</sub> [*类型*](03_Types.html#type)
<a name="operator_declaration"></a> <a name="operator_declaration"></a>
## 运算符声明 ## 运算符声明
@ -1229,17 +1229,17 @@ postfix operator 运算符名称 {}
> 运算符声明语法 > 运算符声明语法
> >
<a name="operator-declaration"></a> <a name="operator-declaration"></a>
> *运算符声明* → [*前缀运算符声明*](#prefix-operator-declaration) | [*后缀运算符声明*](#postfix-operator-declaration) | [*中缀运算符声明*](#infix-operator-declaration) > *运算符声明* → [*前缀运算符声明*](#prefix-operator-declaration) | [*后缀运算符声明*](#postfix-operator-declaration) | [*中缀运算符声明*](#infix-operator-declaration)
> >
<a name="prefix-operator-declaration"></a> <a name="prefix-operator-declaration"></a>
> *前缀运算符声明* → **prefix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** **}** > *前缀运算符声明* → **prefix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** **}**
> <a name="postfix-operator-declaration"></a> > <a name="postfix-operator-declaration"></a>
> *后缀运算符声明* → **postfix** **运算符** [*运算符*] (02_Lexical_Structure.html#operator) **{** **}** > *后缀运算符声明* → **postfix** **运算符** [*运算符*] (02_Lexical_Structure.html#operator) **{** **}**
> <a name="infix-operator-declaration"></a> > <a name="infix-operator-declaration"></a>
> *中缀运算符声明* → **infix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** [*中缀运算符属性*](#infix-operator-attributes)<sub>可选</sub> **}** > *中缀运算符声明* → **infix** **运算符** [*运算符*](02_Lexical_Structure.html#operator) **{** [*中缀运算符属性*](#infix-operator-attributes)<sub>可选</sub> **}**
> >
<a name="infix-operator-group"></a> <a name="infix-operator-group"></a>
> *中缀运算符组* → [*优先级组名称*](#precedence-group-name) > *中缀运算符组* → [*优先级组名称*](#precedence-group-name)
<a name="precedence_group_declaration_modifiers"></a> <a name="precedence_group_declaration_modifiers"></a>
## 优先级组声明 ## 优先级组声明
@ -1256,7 +1256,7 @@ precedencegroup 优先级组名称{
} }
``` ```
较低优先级组和较高优先级组的名称说明了新建的优先级组是依赖于现存的优先级组的。`lowerThan` 优先级组的属性只可以引用当前模块外的优先级组。当两个运算符为同一个操作数竞争时,比如表达式 `2 + 3 * 5`,优先级更高的运算符将优先参与运算。 较低优先级组和较高优先级组的名称说明了新建的优先级组是依赖于现存的优先级组的。`lowerThan` 优先级组的属性只可以引用当前模块外的优先级组。当两个运算符为同一个操作数竞争时,比如表达式 `2 + 3 * 5`,优先级更高的运算符将优先参与运算。
> 注意 > 注意
> >
@ -1272,27 +1272,27 @@ Swift 定义了大量的优先级组来与标准库的运算符配合使用,
> 优先级组声明语法 > 优先级组声明语法
> >
<a name="precedence-group-declaration"></a> <a name="precedence-group-declaration"></a>
> *优先级组声明* → **precedence**[*优先级组名称*](#precedence-group-name){[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> } > *优先级组声明* → **precedence**[*优先级组名称*](#precedence-group-name){[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> }
<a name="precedence-group-attributes"></a> <a name="precedence-group-attributes"></a>
> *优先级组属性* → [*优先级组属性*](#precedence-group-attribute)[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> **{** **}** > *优先级组属性* → [*优先级组属性*](#precedence-group-attribute)[*多优先级组属性*](#precedence-group-attributes)<sub>可选</sub> **{** **}**
<a name="precedence-group-attribute"></a> <a name="precedence-group-attribute"></a>
> *优先级组属性* → [*优先级组关系*](#precedence-group-relation) > *优先级组属性* → [*优先级组关系*](#precedence-group-relation)
> *优先级组属性* → [*优先级组赋值性*](#precedence-group-assignment) > *优先级组属性* → [*优先级组赋值性*](#precedence-group-assignment)
> *优先级组属性* → [*优先级组相关性*](#precedence-group-associativity) > *优先级组属性* → [*优先级组相关性*](#precedence-group-associativity)
> <a name="precedence-group-relation"></a> > <a name="precedence-group-relation"></a>
> *优先级组关系* → **higherThan:**[*多优先级组名称*](#precedence-group-names) > *优先级组关系* → **higherThan:**[*多优先级组名称*](#precedence-group-names)
> *优先级组关系* → **lowerThan:**[*多优先级组名称*](#precedence-group-names) > *优先级组关系* → **lowerThan:**[*多优先级组名称*](#precedence-group-names)
> <a name="precedence-group-assignment"></a> > <a name="precedence-group-assignment"></a>
> *优先级组赋值* → **assignment:**[*布尔字面值*](02_Lexical_Structure.html#boolean-literal) > *优先级组赋值* → **assignment:**[*布尔字面值*](02_Lexical_Structure.html#boolean-literal)
<a name="precedence-group-associativity"></a> <a name="precedence-group-associativity"></a>
> *优先级组结合性* → **associativity:left** > *优先级组结合性* → **associativity:left**
> *优先级组结合性* → **associativity:right** > *优先级组结合性* → **associativity:right**
> *优先级组结合性* → **associativity:none** > *优先级组结合性* → **associativity:none**
> >
<a name="precedence-group-names"></a> <a name="precedence-group-names"></a>
> *多优先级组名称* → [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) > *多优先级组名称* → [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name) | [*优先级组名称*](#precedence-group-name)
<a name="precedence-group-name"></a> <a name="precedence-group-name"></a>
> *优先级组名称* →[*标识符*](02_Lexical_Structure.html#identifier) > *优先级组名称* →[*标识符*](02_Lexical_Structure.html#identifier)
<a name="Declaration_Modifiers"></a> <a name="Declaration_Modifiers"></a>
## 声明修饰符 ## 声明修饰符
@ -1350,12 +1350,12 @@ Swift 提供了三个级别的访问控制:`public`、`internal` 和 `private`
> 声明修饰符的语法 > 声明修饰符的语法
> >
<a name="declaration-modifier"></a> <a name="declaration-modifier"></a>
> *声明修饰符* → **class** | **convenience**| **dynamic** | **final** | **infix** | **lazy** | **mutating** | **nonmutating** | **optional** | **override** | **postfix** | **prefix** | **required** | **static** | **unowned** | **unowned ( safe )** | **unowned ( unsafe )** | **weak** > *声明修饰符* → **class** | **convenience**| **dynamic** | **final** | **infix** | **lazy** | **mutating** | **nonmutating** | **optional** | **override** | **postfix** | **prefix** | **required** | **static** | **unowned** | **unowned ( safe )** | **unowned ( unsafe )** | **weak**
> 声明修饰符 → [*访问级别修饰符*](#access-level-modifier) > 声明修饰符 → [*访问级别修饰符*](#access-level-modifier)
> <a name="declaration-modifiers"></a> > <a name="declaration-modifiers"></a>
> *声明修饰符列表* → [*声明修饰符*](#declaration-modifier) [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub> > *声明修饰符列表* → [*声明修饰符*](#declaration-modifier) [*声明修饰符列表*](#declaration-modifiers)<sub>可选</sub>
> >
<a name="access-level-modifier"></a> <a name="access-level-modifier"></a>
> 访问级别修饰符 → **internal** | **internal ( set )** > 访问级别修饰符 → **internal** | **internal ( set )**
> 访问级别修饰符 → **private** | **private ( set )** > 访问级别修饰符 → **private** | **private ( set )**
> 访问级别修饰符 → **public** | **public ( set )** > 访问级别修饰符 → **public** | **public ( set )**

View File

@ -9,8 +9,8 @@
> 翻译+校对:[KYawn](https://github.com/KYawn) > 翻译+校对:[KYawn](https://github.com/KYawn)
> 2.1 > 2.1
> 翻译:[小铁匠 Linus](https://github.com/kevin833752) > 翻译:[小铁匠 Linus](https://github.com/kevin833752)
> 4.1 > 4.1
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift) > 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
@ -218,16 +218,16 @@ convention 特性总是与下面的参数之一一起出现。
`escaping` `escaping`
在函数或者方法声明上使用该特性,它表示参数将不会被存储以供延迟执行,这将确保参数不会超出函数调用的生命周期。在使用 `escaping` 声明特性的函数类型中访问属性和方法时不需要显式地使用 `self.`。关于如何使用 `escaping` 特性的例子,请参阅 [逃逸闭包](http://wiki.jikexueyuan.com/project/swift/chapter2/07_Closures.html)。 在函数或者方法声明上使用该特性,它表示参数将不会被存储以供延迟执行,这将确保参数不会超出函数调用的生命周期。在使用 `escaping` 声明特性的函数类型中访问属性和方法时不需要显式地使用 `self.`。关于如何使用 `escaping` 特性的例子,请参阅 [逃逸闭包](http://wiki.jikexueyuan.com/project/swift/chapter2/07_Closures.html)。
> 特性语法 > 特性语法
> >
> *特性 *→ @ <font color = 0x3386c8>特性名 特性参数子句</font><sub>可选</sub> > *特性 *→ @ <font color = 0x3386c8>特性名 特性参数子句</font><sub>可选</sub>
> *特性名* → <font color = 0x3386c8>标识符 > *特性名* → <font color = 0x3386c8>标识符
> *特性参数子句* → ( <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub> ) > *特性参数子句* → ( <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub> )
> *特性列表* → <font color = 0x3386c8>特性 特性列表</font><sub>可选</sub> > *特性列表* → <font color = 0x3386c8>特性 特性列表</font><sub>可选</sub>
> >
> *均衡令牌列表* → <font color = 0x3386c8>均衡令牌 均衡令牌列表</font><sub>可选</sub> > *均衡令牌列表* → <font color = 0x3386c8>均衡令牌 均衡令牌列表</font><sub>可选</sub>
> *均衡令牌* → ( <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub> ) > *均衡令牌* → ( <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub> )
> *均衡令牌* → [ <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub> ] > *均衡令牌* → [ <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub> ]
> *均衡令牌* → { <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub>} > *均衡令牌* → { <font color = 0x3386c8>均衡令牌列表</font><sub>可选</sub>}
> *均衡令牌* → 任意标识符,关键字,字面量或运算符 > *均衡令牌* → 任意标识符,关键字,字面量或运算符
> *均衡令牌* → 任意标点除了 ()[]{,或 } > *均衡令牌* → 任意标点除了 ()[]{,或 }

View File

@ -10,9 +10,9 @@
> 2.1 > 2.1
> 翻译:[BridgeQ](https://github.com/WXGBridgeQ) > 翻译:[BridgeQ](https://github.com/WXGBridgeQ)
> 4.1 > 4.1
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift) > 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
本页内容包括: 本页内容包括:
@ -33,17 +33,17 @@ Swift 中的模式分为两类:一种能成功匹配任何类型的值,另
第二类模式用于全模式匹配,这种情况下你试图匹配的值在运行时可能不存在。此类模式包括枚举用例模式、可选模式、表达式模式和类型转换模式。你在 `switch` 语句的 `case` 标签中,`do` 语句的 `catch` 子句中,或者在 `if``while``guard``for-in` 语句的 `case` 条件句中使用这类模式。 第二类模式用于全模式匹配,这种情况下你试图匹配的值在运行时可能不存在。此类模式包括枚举用例模式、可选模式、表达式模式和类型转换模式。你在 `switch` 语句的 `case` 标签中,`do` 语句的 `catch` 子句中,或者在 `if``while``guard``for-in` 语句的 `case` 条件句中使用这类模式。
> 模式语法 > 模式语法
> >
<a name="pattern"></a> <a name="pattern"></a>
> *模式* → [*通配符模式*](#wildcard_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub> > *模式* → [*通配符模式*](#wildcard_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
> *模式* → [*标识符模式*](#identifier_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub> > *模式* → [*标识符模式*](#identifier_pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
> *模式* → [*值绑定模式*](#value-binding-pattern) > *模式* → [*值绑定模式*](#value-binding-pattern)
> *模式* → [*元组模式*](#tuple-pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub> > *模式* → [*元组模式*](#tuple-pattern) [*类型标注*](03_Types.html#type-annotation)<sub>可选</sub>
> *模式* → [*枚举用例模式*](#enum-case-pattern) > *模式* → [*枚举用例模式*](#enum-case-pattern)
> *模式* → [*可选模式*](#optional-pattern) > *模式* → [*可选模式*](#optional-pattern)
> *模式* → [*类型转换模式*](#type-casting-pattern) > *模式* → [*类型转换模式*](#type-casting-pattern)
> *模式* → [*表达式模式*](#expression-pattern) > *模式* → [*表达式模式*](#expression-pattern)
<a name="wildcard_pattern"></a> <a name="wildcard_pattern"></a>
## 通配符模式Wildcard Pattern ## 通配符模式Wildcard Pattern
@ -56,10 +56,10 @@ for _ in 1...3 {
} }
``` ```
> 通配符模式语法 > 通配符模式语法
> >
<a name="wildcard-pattern"></a> <a name="wildcard-pattern"></a>
> *通配符模式* → **_** > *通配符模式* → **_**
<a name="identifier_pattern"></a> <a name="identifier_pattern"></a>
## 标识符模式Identifier Pattern ## 标识符模式Identifier Pattern
@ -74,7 +74,7 @@ let someValue = 42
如果一个变量或常量声明的左边是一个标识符模式,那么这个标识符模式是值绑定模式的子模式。 如果一个变量或常量声明的左边是一个标识符模式,那么这个标识符模式是值绑定模式的子模式。
> 标识符模式语法 > 标识符模式语法
> >
<a name="identifier-pattern"></a> <a name="identifier-pattern"></a>
> *标识符模式* → [*标识符*](02_Lexical_Structure.html#identifier) > *标识符模式* → [*标识符*](02_Lexical_Structure.html#identifier)
@ -98,10 +98,10 @@ case let (x, y):
在上面这个例子中,`let` 会分配到元组模式 `(x, y)` 中的各个标识符模式。因此,`switch` 语句中 `case let (x, y):``case (let x, let y):` 的匹配效果是一样的。 在上面这个例子中,`let` 会分配到元组模式 `(x, y)` 中的各个标识符模式。因此,`switch` 语句中 `case let (x, y):``case (let x, let y):` 的匹配效果是一样的。
> 值绑定模式语法 > 值绑定模式语法
> >
<a name="value-binding-pattern"></a> <a name="value-binding-pattern"></a>
> *值绑定模式* → **var** [*模式*](#pattern) | **let** [*模式*](#pattern) > *值绑定模式* → **var** [*模式*](#pattern) | **let** [*模式*](#pattern)
<a name="tuple_pattern"></a> <a name="tuple_pattern"></a>
## 元组模式 ## 元组模式
@ -128,14 +128,14 @@ let (a) = 2 // a: Int = 2
let (a): Int = 2 // a: Int = 2 let (a): Int = 2 // a: Int = 2
``` ```
> 元组模式语法 > 元组模式语法
> >
<a name="tuple-pattern"></a> <a name="tuple-pattern"></a>
> *元组模式* → **(** [*元组模式元素列表*](#tuple-pattern-element-list)<sub>可选</sub> **)** > *元组模式* → **(** [*元组模式元素列表*](#tuple-pattern-element-list)<sub>可选</sub> **)**
<a name="tuple-pattern-element-list"></a> <a name="tuple-pattern-element-list"></a>
> *元组模式元素列表* → [*元组模式元素*](#tuple-pattern-element) | [*元组模式元素*](#tuple-pattern-element) **,** [*元组模式元素列表*](#tuple-pattern-element-list) > *元组模式元素列表* → [*元组模式元素*](#tuple-pattern-element) | [*元组模式元素*](#tuple-pattern-element) **,** [*元组模式元素列表*](#tuple-pattern-element-list)
<a name="tuple-pattern-element"></a> <a name="tuple-pattern-element"></a>
> *元组模式元素* → [*模式*](#pattern) > *元组模式元素* → [*模式*](#pattern)
<a name="enumeration_case_pattern"></a> <a name="enumeration_case_pattern"></a>
## 枚举用例模式Enumeration Case Pattern ## 枚举用例模式Enumeration Case Pattern
@ -144,10 +144,10 @@ let (a): Int = 2 // a: Int = 2
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../chapter2/08_Enumerations.html#associated_values)。 如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用 `switch` 语句来匹配包含关联值的枚举用例的例子,请参阅 [关联值](../chapter2/08_Enumerations.html#associated_values)。
> 枚举用例模式语法 > 枚举用例模式语法
> >
<a name="enum-case-pattern"></a> <a name="enum-case-pattern"></a>
> *枚举用例模式* → [*类型标识*](03_Types.html#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](05_Declarations.html#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub> > *枚举用例模式* → [*类型标识*](03_Types.html#type-identifier)<sub>可选</sub> **.** [*枚举用例名*](05_Declarations.html#enum-case-name) [*元组模式*](#tuple-pattern)<sub>可选</sub>
<a name="optional_pattern"></a> <a name="optional_pattern"></a>
## 可选模式Optional Pattern ## 可选模式Optional Pattern
@ -182,10 +182,10 @@ for case let number? in arrayOfOptinalInts {
// Found a 5 // Found a 5
``` ```
> 可选模式语法 > 可选模式语法
> >
<a name="optional-pattern"></a> <a name="optional-pattern"></a>
> *可选模式* → [*标识符模式*](03_Types.html#type-identifier) **?** > *可选模式* → [*标识符模式*](03_Types.html#type-identifier) **?**
<a name="type-casting_patterns"></a> <a name="type-casting_patterns"></a>
## 类型转换模式Type-Casting Patterns ## 类型转换模式Type-Casting Patterns
@ -201,14 +201,14 @@ for case let number? in arrayOfOptinalInts {
关于使用 `switch` 语句配合 `is` 模式和 `as` 模式来匹配值的例子,请参阅 [Any 和 AnyObject 的类型转换](../chapter2/18_Type_Casting.html#type_casting_for_any_and_anyobject)。 关于使用 `switch` 语句配合 `is` 模式和 `as` 模式来匹配值的例子,请参阅 [Any 和 AnyObject 的类型转换](../chapter2/18_Type_Casting.html#type_casting_for_any_and_anyobject)。
> 类型转换模式语法 > 类型转换模式语法
> >
<a name="type-casting-pattern"></a> <a name="type-casting-pattern"></a>
> *类型转换模式* → [*is 模式*](#is-pattern) | [*as 模式*](#as-pattern) > *类型转换模式* → [*is 模式*](#is-pattern) | [*as 模式*](#as-pattern)
<a name="is-pattern"></a> <a name="is-pattern"></a>
> *is 模式* → **is** [*类型*](03_Types.html#type) > *is 模式* → **is** [*类型*](03_Types.html#type)
<a name="as-pattern"></a> <a name="as-pattern"></a>
> *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.html#type) > *as 模式* → [*模式*](#pattern) **as** [*类型*](03_Types.html#type)
<a name="expression_pattern"></a> <a name="expression_pattern"></a>
## 表达式模式Expression Pattern ## 表达式模式Expression Pattern
@ -247,7 +247,7 @@ default:
// 打印 “The point is at (1, 2).” // 打印 “The point is at (1, 2).”
``` ```
> 表达式模式语法 > 表达式模式语法
> >
<a name="expression-pattern"></a> <a name="expression-pattern"></a>
> *表达式模式* → [*表达式*](04_Expressions.html#expression) > *表达式模式* → [*表达式*](04_Expressions.html#expression)

View File

@ -9,8 +9,8 @@
> 翻译+校对:[wardenNScaiyi](https:github.com/wardenNScaiyi) > 翻译+校对:[wardenNScaiyi](https:github.com/wardenNScaiyi)
> 3.0 > 3.0
> 翻译+校对:[chenmingjia](https:github.com/chenmingjia) > 翻译+校对:[chenmingjia](https:github.com/chenmingjia)
> 4.1 > 4.1
> 翻译+校对:[mylittleswift](https://github.com/mylittleswift) > 翻译+校对:[mylittleswift](https://github.com/mylittleswift)
@ -72,29 +72,29 @@ simpleMax(3.14159, 2.71828) // T 被推断为 Double 类型
更多关于泛型 where 从句的信息和关于泛型函数声明的例子,可以看一看 [泛型 where 子句](https://github.com/numbbbbb/the-swift-programming-language-in-chinese/blob/gh-pages/source/chapter2/22_Generics.html#where_clauses) 更多关于泛型 where 从句的信息和关于泛型函数声明的例子,可以看一看 [泛型 where 子句](https://github.com/numbbbbb/the-swift-programming-language-in-chinese/blob/gh-pages/source/chapter2/22_Generics.html#where_clauses)
> 泛型形参子句语法 > 泛型形参子句语法
> >
<a name="generic-parameter-clause"></a> <a name="generic-parameter-clause"></a>
> *泛型形参子句* → **<** [*泛型形参列表*](#generic-parameter-list) [*约束子句*](#requirement-clause)<sub>可选</sub> **>** > *泛型形参子句* → **<** [*泛型形参列表*](#generic-parameter-list) [*约束子句*](#requirement-clause)<sub>可选</sub> **>**
<a name="generic-parameter-list"></a> <a name="generic-parameter-list"></a>
> *泛型形参列表* → [*泛形形参*](#generic-parameter) | [*泛形形参*](#generic-parameter) **,** [*泛型形参列表*](#generic-parameter-list) > *泛型形参列表* → [*泛形形参*](#generic-parameter) | [*泛形形参*](#generic-parameter) **,** [*泛型形参列表*](#generic-parameter-list)
<a name="generic-parameter"></a> <a name="generic-parameter"></a>
> *泛形形参* → [*类型名称*](03_Types.html#type-name) > *泛形形参* → [*类型名称*](03_Types.html#type-name)
> *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*类型标识符*](03_Types.html#type-identifier) > *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*类型标识符*](03_Types.html#type-identifier)
> *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*协议合成类型*](03_Types.html#protocol-composition-type) > *泛形形参* → [*类型名称*](03_Types.html#type-name) **:** [*协议合成类型*](03_Types.html#protocol-composition-type)
<a name="requirement-clause"></a> <a name="requirement-clause"></a>
> *约束子句* → **where** [*约束列表*](#requirement-list) > *约束子句* → **where** [*约束列表*](#requirement-list)
<a name="requirement-list"></a> <a name="requirement-list"></a>
> *约束列表* → [*约束*](#requirement) | [*约束*](#requirement) **,** [*约束列表*](#requirement-list) > *约束列表* → [*约束*](#requirement) | [*约束*](#requirement) **,** [*约束列表*](#requirement-list)
<a name="requirement"></a> <a name="requirement"></a>
> *约束* → [*一致性约束*](#conformance-requirement) | [*同类型约束*](#same-type-requirement) > *约束* → [*一致性约束*](#conformance-requirement) | [*同类型约束*](#same-type-requirement)
<a name="conformance-requirement"></a> <a name="conformance-requirement"></a>
> *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*类型标识符*](03_Types.html#type-identifier) > *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*类型标识符*](03_Types.html#type-identifier)
> *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*协议合成类型*](03_Types.html#protocol-composition-type) > *一致性约束* → [*类型标识符*](03_Types.html#type-identifier) **:** [*协议合成类型*](03_Types.html#protocol-composition-type)
<a name="same-type-requirement"></a> <a name="same-type-requirement"></a>
> *同类型约束* → [*类型标识符*](03_Types.html#type-identifier) **==** [*类型*](03_Types.html#type) > *同类型约束* → [*类型标识符*](03_Types.html#type-identifier) **==** [*类型*](03_Types.html#type)
<a name="generic_argument"></a> <a name="generic_argument"></a>
## 泛型实参子句 ## 泛型实参子句
@ -121,11 +121,11 @@ let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
如 [泛型形参子句](#generic_parameter) 所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。 如 [泛型形参子句](#generic_parameter) 所述,不能用泛型实参子句来指定泛型函数或构造器的类型实参。
> 泛型实参子句语法 > 泛型实参子句语法
> >
<a name="generic-argument-clause"></a> <a name="generic-argument-clause"></a>
> *泛型实参子句* → **<** [*泛型实参列表*](#generic-argument-list) **>** > *泛型实参子句* → **<** [*泛型实参列表*](#generic-argument-list) **>**
<a name="generic-argument-list"></a> <a name="generic-argument-list"></a>
> *泛型实参列表* → [*泛型实参*](#generic-argument) | [*泛型实参*](#generic-argument) **,** [*泛型实参列表*](#generic-argument-list) > *泛型实参列表* → [*泛型实参*](#generic-argument) | [*泛型实参*](#generic-argument) **,** [*泛型实参列表*](#generic-argument-list)
<a name="generic-argument"></a> <a name="generic-argument"></a>
> *泛型实参* → [*类型*](03_Types.html#type) > *泛型实参* → [*类型*](03_Types.html#type)

File diff suppressed because it is too large Load Diff

View File

@ -13,4 +13,4 @@ Ballons 不但展现了 playgrounds 许多很赞的特性,还让我们看到
这个 playground 文件用到了 SpriteKit 的新特性,因此需要最新 beta 版本的 Xcode 6和 Yosemite 系统来支持它运行。 这个 playground 文件用到了 SpriteKit 的新特性,因此需要最新 beta 版本的 Xcode 6和 Yosemite 系统来支持它运行。
本文由翻译自 Apple Swift Blog 的博文:[Ballons](https://developer.apple.com/swift/blog/?id=9) 本文由翻译自 Apple Swift Blog 的博文:[Ballons](https://developer.apple.com/swift/blog/?id=9)

View File

@ -2,7 +2,7 @@
----------------- -----------------
> 翻译:[老码团队翻译组-Relly](http://weibo.com/penguinliong/) > 翻译:[老码团队翻译组-Relly](http://weibo.com/penguinliong/)
> 校对:[老码团队翻译组-Tyrion](http://weibo.com/u/5241713117) > 校对:[老码团队翻译组-Tyrion](http://weibo.com/u/5241713117)
本页包含内容: 本页包含内容:

View File

@ -11,7 +11,7 @@
- [如何选择类型](#how-to-choose) - [如何选择类型](#how-to-choose)
### Swift 里面的类型分为两种: ### Swift 里面的类型分为两种:
* **值类型(Value Types)**:每个实例都保留了一分独有的数据拷贝,一般以结构体 `struct`` 枚举enum` 或者 `元组tuple`的形式出现。 * **值类型(Value Types)**:每个实例都保留了一分独有的数据拷贝,一般以结构体 `struct`` 枚举enum` 或者 `元组tuple`的形式出现。
* **引用类型(Reference Type)**:每个实例共享同一份数据来源,一般以 `类class`的形式出现。 * **引用类型(Reference Type)**:每个实例共享同一份数据来源,一般以 `类class`的形式出现。

View File

@ -8,7 +8,7 @@
很多其他编程语言都有一种”protected“设定可以限制某些类方法只能被它的子类所使用。 很多其他编程语言都有一种”protected“设定可以限制某些类方法只能被它的子类所使用。
Swift 支持了访问控制后,大家给我们的反馈都很不错。而有的开发者问我们:“为什么 Swift 没有类似 protected 的选项?” Swift 支持了访问控制后,大家给我们的反馈都很不错。而有的开发者问我们:“为什么 Swift 没有类似 protected 的选项?”
**当我们在设计 Swift 访问控制的不同等级时,我们认为有两种主要场景:** **当我们在设计 Swift 访问控制的不同等级时,我们认为有两种主要场景:**

View File

@ -63,7 +63,7 @@ extension Dictionary {
```swift ```swift
var dic: Dictionary = [ "1": 2, "3":3, "4":5 ] var dic: Dictionary = [ "1": 2, "3":3, "4":5 ]
var t = dic.valuesForKeys(["1", "4"]) var t = dic.valuesForKeys(["1", "4"])
//结果为:[Optional(2), Optional(5)] //结果为:[Optional(2), Optional(5)]
var t = dict.valuesForKeys(["3", "9"]) var t = dict.valuesForKeys(["3", "9"])