Merge branch 'gh-pages' into feature/Ch-Closures

This commit is contained in:
chenxi
2021-06-22 07:49:53 +08:00
4 changed files with 15 additions and 10 deletions

View File

@ -1,11 +1,13 @@
# 版本兼容性
本书描述的是在 Xcode 12默认 Swift 版本 Swift 5.3。你可以使用 Xcode 12 来构建 Swift 5.3、Swift 4.2 或 Swift 4 写的项目。
本书描述的是在 Xcode 13 中默认包含的 Swift 5.5 版本。你可以使用 Xcode 13 来构建 Swift 5.5、Swift 4.2 或 Swift 4 写的项目。
当您使用 Xcode 12 构建 Swift 4 和 Swift 4.2 代码时Swift 5.3 的大多数功能都适用。但以下功能仅支持 Swift 5.3 或更高版本:
使用 Xcode 13 构建 Swift 4 和 Swift 4.2 代码时Swift 5.5 的大多数功能都适用。但以下功能仅支持 Swift 5.5 或更高版本:
* 返回值是不透明类型的函数依赖 Swift 5.1 运行时。
* **try?** 表达式不会为已返回可选类型的代码引入额外的可选类型层级。
* 大数字的整型字面量初始化代码的类型将会被正确推导,例如 **UInt64(0xffff_ffff_ffff_ffff)** 将会被推导为整型类型而非溢出。
Swift 5.3 写的项目可以依赖用 Swift 4.2 或 Swift 4 写的项目反之亦然。这意味着如果你将一个大的项目分解成多个框架framework你可以逐个地将框架从 Swift 4 代码迁移到 Swift 5.3
并发特性需要 Swift 5.5 及以上版本,以及一个提供了并发相关类型的 Swift 标准库版本。要应用于苹果平台,请至少将部署版本设置为 iOS 15、macOS 12、tvOS 15 或 watchOS 8.0
用 Swift 5.5 写的项目可以依赖用 Swift 4.2 或 Swift 4 写的项目反之亦然。这意味着如果你将一个大的项目分解成多个框架framework你可以逐个地将框架从 Swift 4 代码迁移到 Swift 5.5。

View File

@ -97,8 +97,8 @@ print(shoppingList)
使用初始化语法来创建一个空数组或者空字典。
```swift
let emptyArray = [String]()
let emptyDictionary = [String: Float]()
let emptyArray: [String] = []
let emptyDictionary: [String: Float] = [:]
```
如果类型信息可以被推断出来,你可以用 `[]``[:]` 来创建空数组和空字典——比如,在给变量赋新值或者给函数传参数的时候。
@ -187,7 +187,7 @@ let interestingNumbers = [
"Square": [1, 4, 9, 16, 25],
]
var largest = 0
for (kind, numbers) in interestingNumbers {
for (_, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number
@ -195,11 +195,12 @@ for (kind, numbers) in interestingNumbers {
}
}
print(largest)
// 输出 "25"
```
> 练习
>
> 添加另一个变量来记录最大数字的种类kind同时仍然记录这个最大数字的值
> 将 _ 替换成变量名,以确定哪种类型的值是最大的
使用 `while` 来重复运行一段代码直到条件改变。循环条件也可以在结尾,保证能至少循环一次。
@ -771,7 +772,7 @@ print(fridgeIsOpen)
```swift
func makeArray<Item>(repeating item: Item, numberOfTimes: Int) -> [Item] {
var result = [Item]()
var result: [Item] = []
for _ in 0..<numberOfTimes {
result.append(item)
}

View File

@ -37,7 +37,7 @@ Swift 中数组的完整写法为 `Array<Element>`,其中 `Element` 是这个
你可以使用构造语法来创建一个由特定数据类型构成的空数组:
```swift
var someInts = [Int]()
var someInts: [Int] = []
print("someInts is of type [Int] with \(someInts.count) items.")
// 打印“someInts is of type [Int] with 0 items.”
```
@ -473,7 +473,7 @@ Swift 的字典使用 `Dictionary<Key, Value>` 定义,其中 `Key` 是一种
你可以像数组一样使用构造语法创建一个拥有确定类型的空字典:
```swift
var namesOfIntegers = [Int: String]()
var namesOfIntegers: [Int: String] = [:]
// namesOfIntegers 是一个空的 [Int: String] 字典
```

View File

@ -97,6 +97,8 @@ for tickMark in stride(from: 3, through: hours, by: hourInterval) {
}
```
以上示例使用 `for-in` 循环来遍历范围、数组、字典和字符串。你可以用它来遍历任何的集合,包括实现了 [Sequence](https://developer.apple.com/documentation/swift/sequence) 协议的自定义类或集合类型。
## While 循环 {#while-loops}
`while` 循环会一直运行一段语句直到条件变成 `false`。这类循环适合使用在第一次迭代前迭代次数未知的情况下。Swift 提供两种 `while` 循环形式: