Translations for Swift 4.1 (#777)
* Translations for Chapter 3 part 01 for Swift 4.0/4.1 * update chapter 3 part 02 for swift 4.1 * update chapter 3 part 03 for Swift 4.1 * update chapter 1 part 01 for Swift 4.1 * update chapter 1 part 02 for Swift 4.1 * update chapter 1 part 03 for Swift 4.1 * update chapter 2 part 01 for Swift 4.1 * update chapter 2 part 02 for Swift 4.1 * update chapter 2 part 3 for Swift 4.1 * update "summary" and corrected file names * update chapter 2 part 4 for Swift 4.1 * update chapter 2 part 5 for Swift 4.1 * update chapter 2 part 6 for Swift 4.1 * update chapter 2 parts 06-11 and other parts' errors * update chapter 2 parts 12-14 * update chapter 2 parts 14-19 * update all chapter 2 * update whole chapter 1 * update some parts of chapter 3
This commit is contained in:
@ -7,15 +7,17 @@
|
||||
|
||||
> 2.0
|
||||
> 翻译+校对:[xtymichael](https://github.com/xtymichael)
|
||||
>
|
||||
|
||||
> 3.0 翻译+校对:[shanks](http://codebuild.me),2016-10-06
|
||||
> 3.0.1 review : 2016-11-09
|
||||
>
|
||||
|
||||
> 3.1 校对: [SketchK](https://github.com/SketchK) 2017-04-08
|
||||
>
|
||||
|
||||
> 4.0
|
||||
> 翻译:[rain2540](https://github.com/rain2540) 2017-09-21
|
||||
>
|
||||
|
||||
> 4.1
|
||||
> 翻译:[mylittleswift](https://github.com/mylittleswift)
|
||||
|
||||
Swift 是一种非常好的编写软件的方式,无论是手机,台式机,服务器,还是其他运行代码的设备。它是一种安全,快速和互动的编程语言,将现代编程语言的精华和苹果工程师文化的智慧,以及来自开源社区的多样化贡献结合了起来。编译器对性能进行了优化,编程语言对开发进行了优化,两者互不干扰,鱼与熊掌兼得。
|
||||
|
||||
@ -26,7 +28,7 @@ Swift通过采用现代编程模式来避免大量常见编程错误:
|
||||
* 变量始终在使用前初始化。
|
||||
* 检查数组索引超出范围的错误。
|
||||
* 检查整数是否溢出。
|
||||
* 可选值确保明确处理 nil 值。
|
||||
* 可选值确保明确处理 `nil` 值。
|
||||
* 内存被自动管理。
|
||||
* 错误处理允许从意外故障控制恢复。
|
||||
|
||||
@ -34,4 +36,4 @@ Swift 代码被编译和优化,以充分利用现代硬件。语法和标准
|
||||
|
||||
Swift 将强大的类型推理和模式匹配与现代轻巧的语法相结合,使复杂的想法能够以清晰简洁的方式表达。因此,代码不仅更容易编写,而且易于阅读和维护。
|
||||
|
||||
Swift 已经进行了多年,并且随着新特性和功能的不断发展。我们对 Swift 的目标是雄心勃勃的。我们迫不及待想看看你创建的内容。
|
||||
Swift 已经进行了多年,并且随着新特性和功能的不断发展。我们对 Swift 的目标是雄心勃勃的。我们迫不及待想看到你用它创建出的东西。
|
||||
@ -2,18 +2,21 @@
|
||||
-----------------
|
||||
|
||||
> 4.0
|
||||
>
|
||||
> 翻译:[muhlenXi](https://github.com/muhlenxi) 2017-09-25
|
||||
|
||||
本书描述的是 Swift 4.0,是 Xcode 9 中包含的默认版本。你可以用 Xcode 9 来构建用 Swift 4 或 Swift 3 写的项目。
|
||||
> 4.1
|
||||
> 翻译:[mylittleswift](https://github.com/mylittleswift)
|
||||
|
||||
本书描述的是 Swift 4.1,是 Xcode 9.2 中包含的默认版本。你可以用 Xcode 9.2 来构建用 Swift 4 或 Swift 3 写的项目。
|
||||
|
||||
> 注意
|
||||
>
|
||||
> 当 Swift 4 编译器编译 Swift 3 版本的代码时,它识别的语言版本为 3.2 版本。因此,你可以使用像 `#if swift(>=3.2)` 条件编译块来编写多版本编译器可以并存的代码。
|
||||
|
||||
当你用 Xcode 编译 Swift 3 的代码,Swift 4 中大部分功能是可以使用的。也就是说,下面的功能仅仅是 Swift 4 的代码中可以使用:
|
||||
当你用 Xcode 9.2 编译 Swift 3 的代码,Swift 4 中大部分功能是可以使用的。也就是说,下面的功能仅仅是 Swift 4 的代码中可以使用:
|
||||
|
||||
* 字符串的子串操作返回的实例是 `Substring` 类型,不再是 `String` 类型。
|
||||
* 在一些地方显式的添加 `@objc` 属性。
|
||||
* 同一文件中一个类型的 extension 可以访问这个类型的 private 属性。
|
||||
* 在更少的地方显式的添加 `@objc` 属性。
|
||||
* 同一文件中类型的扩展可以访问这个类型的私有成员。
|
||||
|
||||
用 Swift 4 写的项目可以依赖用 Swift 3 写的项目,反之亦然。这意味着,如果你将一个大的项目分解成多个框架(framework),你可以依次迁移每个框架到 Swift 4。
|
||||
用 Swift 4 写的项目可以依赖用 Swift 3 写的项目,反之亦然。这意味着,如果你将一个大的项目分解成多个框架(framework),你可以每次一个框架地迁移 Swift 3 代码到 Swift 4。
|
||||
|
||||
@ -22,6 +22,9 @@
|
||||
> 4.0
|
||||
> 翻译+校对:[muhlenxi](https://github.com/muhlenxi) 2017-09-26
|
||||
|
||||
> 4.1
|
||||
> 翻译:[mylittleswift](https://github.com/mylittleswift)
|
||||
|
||||
本页内容包括:
|
||||
|
||||
- [简单值(Simple Values)](#simple_values)
|
||||
@ -43,8 +46,11 @@ print("Hello, world!")
|
||||
|
||||
这个教程会通过一系列编程例子来让你对 Swift 有初步了解,如果你有什么不理解的地方也不用担心——任何本章介绍的内容都会在后面的章节中详细讲解到。
|
||||
|
||||
> 注意:
|
||||
> 在 Mac 中下载 Playground 文件并用双击的方式在 Xcode 中打开:[https://developer.apple.com/go/?id=swift-tour](https://developer.apple.com/go/?id=swift-tour)
|
||||
> 注意
|
||||
>
|
||||
> 最好的体验是把这一章作为Playground文件在Xcode中打开。 Playgrounds允许你可以编辑代码并立刻看到输出结果。
|
||||
>
|
||||
> [Download Playground](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.playground.zip)
|
||||
|
||||
<a name="simple_values"></a>
|
||||
## 简单值
|
||||
@ -57,7 +63,7 @@ myVariable = 50
|
||||
let myConstant = 42
|
||||
```
|
||||
|
||||
常量或者变量的类型必须和你赋给它们的值一样。然而,你不用明确地声明类型。当你通过一个值来声明变量和常量时,编译器会自动推断其类型。在上面的例子中,编译器推断出 `myVariable` 是一个整数类型(integer)因为它的初始值是整数。
|
||||
常量或者变量的类型必须和你赋给它们的值一样。然而,你不用明确地声明类型。当你通过一个值来声明变量和常量时,编译器会自动推断其类型。在上面的例子中,编译器推断出 `myVariable` 是一个整数类型,因为它的初始值是整数。
|
||||
|
||||
如果初始值没有提供足够的信息(或者没有初始值),那你需要在变量后面声明类型,用冒号分割。
|
||||
|
||||
@ -67,7 +73,8 @@ let implicitDouble = 70.0
|
||||
let explicitDouble: Double = 70
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 创建一个常量,显式指定类型为 `Float` 并指定初始值为 4。
|
||||
|
||||
值永远不会被隐式转换为其他类型。如果你需要把一个值转换成其他类型,请显式转换。
|
||||
@ -77,7 +84,8 @@ let label = "The width is"
|
||||
let width = 94
|
||||
let widthLabel = label + String(width)
|
||||
```
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 删除最后一行中的 `String`,错误提示是什么?
|
||||
|
||||
有一种更简单的把值转换成字符串的方法:把值写到括号中,并且在括号之前写一个反斜杠(\)。例如:
|
||||
@ -89,19 +97,16 @@ let appleSummary = "I have \(apples) apples."
|
||||
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 使用 `\()` 来把一个浮点计算转换成字符串,并加上某人的名字,和他打个招呼。
|
||||
|
||||
使用一对三个单引号(`"""`)来包含多行字符串内容,字符串中的内容(包括引号、空格、换行符等)都会保留下来。举个例子:
|
||||
|
||||
```swift
|
||||
let quotation = """
|
||||
Even though there's whitespace to the left,
|
||||
the actual lines aren't indented.
|
||||
Except for this line.
|
||||
Double quotes (") can appear without being escaped.
|
||||
|
||||
I still have \(apples + oranges) pieces of fruit.
|
||||
I said "I have \(apples) apples."
|
||||
And then I said "I have \(apples + oranges) pieces of fruit."
|
||||
"""
|
||||
```
|
||||
|
||||
@ -165,7 +170,8 @@ if let name = optionalName {
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 把 `optionalName` 改成 `nil`,greeting 会是什么?添加一个 `else` 语句,当 `optionalName` 是 `nil` 时给 greeting 赋一个不同的值。
|
||||
|
||||
如果变量的可选值是 `nil`,条件会判断为 `false`,大括号中的代码会被跳过。如果不是 `nil`,会将值解包并赋给 `let` 后面的常量,这样代码块中就可以使用这个值了。
|
||||
@ -193,7 +199,8 @@ default:
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 删除 `default` 语句,看看会有什么错误?
|
||||
|
||||
注意 `let` 在上述例子的等式中是如何使用的,它将匹配等式的值赋给常量 `x`。
|
||||
@ -219,7 +226,8 @@ for (kind, numbers) in interestingNumbers {
|
||||
print(largest)
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 添加另一个变量来记录最大数字的种类(kind),同时仍然记录这个最大数字的值。
|
||||
|
||||
使用 `while` 来重复运行一段代码直到条件改变。循环条件也可以在结尾,保证能至少循环一次。
|
||||
@ -262,7 +270,8 @@ func greet(person: String, day: String) -> String {
|
||||
greet(person:"Bob", day: "Tuesday")
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 删除 `day` 参数,添加一个参数来表示今天吃了什么午饭。
|
||||
|
||||
默认情况下,函数使用它们的参数名称作为它们参数的标签,在参数名称前可以自定义参数标签,或者使用 `_` 表示不使用参数标签。
|
||||
@ -353,7 +362,8 @@ numbers.map({
|
||||
})
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 重写闭包,对所有奇数返回 0。
|
||||
|
||||
有很多种创建更简洁的闭包的方法。如果一个闭包的类型已知,比如作为一个代理的回调,你可以忽略参数,返回值,甚至两个都忽略。单个语句闭包会把它语句的值当做结果返回。
|
||||
@ -384,7 +394,8 @@ class Shape {
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 使用 `let` 添加一个常量属性,再添加一个接收一个参数的方法。
|
||||
|
||||
要创建一个类的实例,在类名后面加上括号。使用点语法来访问实例的属性和方法。
|
||||
@ -443,7 +454,8 @@ test.area()
|
||||
test.simpleDescription()
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 创建 `NamedShape` 的另一个子类 `Circle`,构造器接收两个参数,一个是半径一个是名称,在子类 `Circle` 中实现 `area()` 和 `simpleDescription()` 方法。
|
||||
|
||||
除了储存简单的属性之外,属性可以有 getter 和 setter 。
|
||||
@ -547,7 +559,8 @@ let ace = Rank.ace
|
||||
let aceRawValue = ace.rawValue
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 写一个函数,通过比较它们的原始值来比较两个 `Rank` 值。
|
||||
|
||||
默认情况下,Swift 按照从 0 开始每次加 1 的方式为原始值进行赋值,不过你可以通过显式赋值进行改变。在上面的例子中,`Ace` 被显式赋值为 1,并且剩下的原始值会按照顺序赋值。你也可以使用字符串或者浮点数作为枚举的原始值。使用 `rawValue` 属性来访问一个枚举成员的原始值。
|
||||
@ -582,7 +595,8 @@ let hearts = Suit.hearts
|
||||
let heartsDescription = hearts.simpleDescription()
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 给 `Suit` 添加一个 `color()` 方法,对 `spades` 和 `clubs` 返回 “black” ,对 `hearts` 和 `diamonds` 返回 “red” 。
|
||||
|
||||
注意在上面的例子中用了两种方式引用 `hearts` 枚举成员:给 `hearts` 常量赋值时,枚举成员 `Suit.hearts` 需要用全名来引用,因为常量没有显式指定类型。在 `switch` 里,枚举成员使用缩写 `.hearts` 来引用,因为 `self` 的值已经是一个 `suit` 类型,在已知变量类型的情况下可以使用缩写。
|
||||
@ -606,7 +620,8 @@ case let .failure(message):
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 给 `ServerResponse` 和 switch 添加第三种情况。
|
||||
|
||||
注意日升和日落时间是如何从 `ServerResponse` 中提取到并与 `switch` 的 `case` 相匹配的。
|
||||
@ -625,7 +640,8 @@ let threeOfSpades = Card(rank: .three, suit: .spades)
|
||||
let threeOfSpadesDescription = threeOfSpades.simpleDescription()
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 给 `Card` 添加一个方法,创建一副完整的扑克牌并把每张牌的 rank 和 suit 对应起来。
|
||||
|
||||
<a name="protocols_and_extensions"></a>
|
||||
@ -665,7 +681,8 @@ b.adjust()
|
||||
let bDescription = b.simpleDescription
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 写一个实现这个协议的枚举。
|
||||
|
||||
注意声明 `SimpleStructure` 时候 `mutating` 关键字用来标记一个会修改结构体的方法。`SimpleClass` 的声明不需要标记任何方法,因为类中的方法通常可以修改类属性(类的性质)。
|
||||
@ -684,7 +701,8 @@ extension Int: ExampleProtocol {
|
||||
print(7.simpleDescription)
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 给 `Double` 类型写一个扩展,添加 `absoluteValue` 属性。
|
||||
|
||||
你可以像使用其他命名类型一样使用协议名——例如,创建一个有不同类型但是都实现一个协议的对象集合。当你处理类型是协议的值时,协议外定义的方法不可用。
|
||||
@ -732,7 +750,8 @@ do {
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 将 printer name 改为 `"Never Has Toner"` 使 `send(job:toPrinter:)` 函数抛出错误。
|
||||
|
||||
可以使用多个 `catch` 块来处理特定的错误。参照 switch 中的 `case` 风格来写 `catch`。
|
||||
@ -750,7 +769,8 @@ do {
|
||||
}
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 在 `do` 代码块中添加抛出错误的代码。你需要抛出哪种错误来使第一个 `catch` 块进行接收?怎么使第二个和第三个 `catch` 进行接收呢?
|
||||
|
||||
另一种处理错误的方式使用 `try?` 将结果转换为可选的。如果函数抛出错误,该错误会被抛弃并且结果为 `nil`。否则,结果会是一个包含函数返回值的可选值。
|
||||
@ -792,7 +812,7 @@ func makeArray<Item>(repeating item: Item, numberOfTimes: Int) -> [Item] {
|
||||
}
|
||||
return result
|
||||
}
|
||||
repeatItem(repeating: "knock", numberOfTimes:4)
|
||||
makeArray(repeating: "knock", numberOfTimes: 4)
|
||||
```
|
||||
|
||||
你也可以创建泛型函数、方法、类、枚举和结构体。
|
||||
@ -824,7 +844,8 @@ func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> Bool
|
||||
anyCommonElements([1, 2, 3], [3])
|
||||
```
|
||||
|
||||
> 练习:
|
||||
> 练习
|
||||
>
|
||||
> 修改 `anyCommonElements(_:_:)` 函数来创建一个函数,返回一个数组,内容是两个序列的共有元素。
|
||||
|
||||
`<T: Equatable>` 和 `<T> ... where T: Equatable>` 的写法是等价的。
|
||||
|
||||
@ -45,7 +45,8 @@
|
||||
|
||||
本页内容包括:
|
||||
|
||||
- [Swift 4.0 更新](#swift_4_1)
|
||||
- [Swift 4.1 更新](#swift_4_1)
|
||||
- [Swift 4.0.3 更新](#swift_4_0_3)
|
||||
- [Swift 4.0 更新](#swift_4_0)
|
||||
- [Swift 3.1 更新](#swift_3_1)
|
||||
- [Swift 3.0 更新](#swift_3_0)
|
||||
@ -68,7 +69,7 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td scope="row">2018-02-06</td>
|
||||
<td scope="row">2018-03-29</td>
|
||||
<td>
|
||||
<ul class="list-bullet">
|
||||
<li>更新至 Swift 4.1。</li>
|
||||
@ -81,6 +82,29 @@
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<a name="swift_4_0_3"></a>
|
||||
### Swift 4.0.3 更新
|
||||
|
||||
<table class="graybox" border="0" cellspacing="0" cellpadding="5">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col" width="100">发布日期</th>
|
||||
<th scope="col">语法变更记录</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td scope="row">2017-12-04</td>
|
||||
<td>
|
||||
<ul class="list-bullet">
|
||||
<li>更新至 Swift 4.0.3。</li>
|
||||
<li>更新 <a href='https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/doc/uid/TP40014097-CH32-ID563'>Key-Path 表达式</a>,现在 key path 支持下标子路径。</li>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<a name="swift_4_0"></a>
|
||||
### Swift 4.0 更新
|
||||
|
||||
@ -93,11 +117,18 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td scope="row">2017-12-04</td>
|
||||
<td scope="row">2017-09-19</td>
|
||||
<td>
|
||||
<ul class="list-bullet">
|
||||
<li>更新至 Swift 4.0.3。</li>
|
||||
<li>更新 <a href='https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/doc/uid/TP40014097-CH32-ID563'>Key-Path 表达式</a>,现在 key path 支持下标子路径。</li>
|
||||
<li>更新至 Swift 4.0。</li>
|
||||
<li>Added information about exclusive access to memory to the Memory Safety chapter.</li>
|
||||
<li>Added the Associated Types with a Generic Where Clause section, now that you can use generic where clauses to constrain associated types.</li>
|
||||
<li>Added information about multiline string literals to the String Literals section of the Strings and Characters chapter, and to the String Literals section of the Lexical Structure chapter.</li>
|
||||
<li>Updated the discussion of the objc attribute in Declaration Attributes, now that this attribute is inferred in fewer places.</li>
|
||||
<li>Added the Generic Subscripts section, now that subscripts can be generic.</li>
|
||||
<li>Updated the discussion in the Protocol Composition section of the Protocols chapter, and in the Protocol Composition Type section of the Types chapter, now that protocol composition types can contain a superclass requirement.</li>
|
||||
<li>Updated the discussion of protocol extensions in Extension Declaration now that final isn’t allowed in them.</li>
|
||||
<li>Added information about preconditions and fatal errors to the Assertions and Preconditions section.</li>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
Reference in New Issue
Block a user