2 Commits

9 changed files with 10 additions and 16 deletions

View File

@ -114,7 +114,7 @@ print("\(numberOfChoices) beverages available")
// 打印“3 beverages available” // 打印“3 beverages available”
``` ```
在前面的例子中,通过 `Beverage.allCases` 可以访问到包含 `Beverage` 枚举所有成员的集合。`allCases` 的使用方法和其它一般集合一样——集合中的元素是枚举类型的实例,所以在上面的情况中,这些元素是 `Beverage` 值。在前面的例子中,统计了总共有多少个枚举成员。而在下面的例子中,则使用 `for-in` 循环来遍历所有枚举成员。 在前面的例子中,通过 `Beverage.allCases` 可以访问到包含 `Beverage` 枚举所有成员的集合。`allCases` 的使用方法和其它一般集合一样——集合中的元素是枚举类型的实例,所以在上面的情况中,这些元素是 `Beverage` 值。在前面的例子中,统计了总共有多少个枚举成员。而在下面的例子中,则使用 `for` 循环来遍历所有枚举成员。
```swift ```swift
for beverage in Beverage.allCases { for beverage in Beverage.allCases {

View File

@ -32,10 +32,6 @@ Swift 中结构体和类有很多共同点。两者都可以:
类支持的附加功能是以增加复杂性为代价的。作为一般准则,优先使用结构体,因为它们更容易理解,仅在适当或必要时才使用类。实际上,这意味着你的大多数自定义数据类型都会是结构体和枚举。更多详细的比较参见 [在结构和类之间进行选择](https://developer.apple.com/documentation/swift/choosing_between_structures_and_classes)。 类支持的附加功能是以增加复杂性为代价的。作为一般准则,优先使用结构体,因为它们更容易理解,仅在适当或必要时才使用类。实际上,这意味着你的大多数自定义数据类型都会是结构体和枚举。更多详细的比较参见 [在结构和类之间进行选择](https://developer.apple.com/documentation/swift/choosing_between_structures_and_classes)。
> 注意
>
> 类和 actors 共享很多特性。更多信息请参见 [并发](./28_Concurrency.md)。
### 类型定义的语法 {#definition-syntax} ### 类型定义的语法 {#definition-syntax}
结构体和类有着相似的定义方式。你通过 `struct` 关键字引入结构体,通过 `class` 关键字引入类,并将它们的具体定义放在一对大括号中: 结构体和类有着相似的定义方式。你通过 `struct` 关键字引入结构体,通过 `class` 关键字引入类,并将它们的具体定义放在一对大括号中:

View File

@ -1036,7 +1036,7 @@ class SomeClass {
```swift ```swift
struct Chessboard { struct Chessboard {
let boardColors: [Bool] = { let boardColors: [Bool] = {
var temporaryBoard: [Bool] = [] var temporaryBoard = [Bool]()
var isBlack = false var isBlack = false
for i in 1...8 { for i in 1...8 {
for j in 1...8 { for j in 1...8 {

View File

@ -97,7 +97,7 @@ class Person {
```swift ```swift
class Residence { class Residence {
var rooms: [Room] = [] var rooms = [Room]()
var numberOfRooms: Int { var numberOfRooms: Int {
return rooms.count return rooms.count
} }

View File

@ -141,7 +141,7 @@ Swift 为不确定类型提供了两种特殊的类型别名:
这里有个示例,使用 `Any` 类型来和混合的不同类型一起工作,包括函数类型和非类类型。它创建了一个可以存储 `Any` 类型的数组 `things` 这里有个示例,使用 `Any` 类型来和混合的不同类型一起工作,包括函数类型和非类类型。它创建了一个可以存储 `Any` 类型的数组 `things`
```swift ```swift
var things: [Any] = [] var things = [Any]()
things.append(0) things.append(0)
things.append(0.0) things.append(0.0)

View File

@ -135,7 +135,7 @@ swapTwoValues(&someString, &anotherString)
```swift ```swift
struct IntStack { struct IntStack {
var items: [Int] = [] var items = [Int]()
mutating func push(_ item: Int) { mutating func push(_ item: Int) {
items.append(item) items.append(item)
} }
@ -153,7 +153,7 @@ struct IntStack {
```swift ```swift
struct Stack<Element> { struct Stack<Element> {
var items: [Element] = [] var items = [Element]()
mutating func push(_ item: Element) { mutating func push(_ item: Element) {
items.append(item) items.append(item)
} }
@ -354,7 +354,7 @@ protocol Container {
```swift ```swift
struct IntStack: Container { struct IntStack: Container {
// IntStack 的原始实现部分 // IntStack 的原始实现部分
var items: [Int] = [] var items = [Int]()
mutating func push(_ item: Int) { mutating func push(_ item: Int) {
items.append(item) items.append(item)
} }
@ -386,7 +386,7 @@ struct IntStack: Container {
```swift ```swift
struct Stack<Element>: Container { struct Stack<Element>: Container {
// Stack<Element> 的原始实现部分 // Stack<Element> 的原始实现部分
var items: [Element] = [] var items = [Element]()
mutating func push(_ item: Element) { mutating func push(_ item: Element) {
items.append(item) items.append(item)
} }
@ -721,7 +721,7 @@ protocol ComparableContainer: Container where Item: Comparable { }
extension Container { extension Container {
subscript<Indices: Sequence>(indices: Indices) -> [Item] subscript<Indices: Sequence>(indices: Indices) -> [Item]
where Indices.Iterator.Element == Int { where Indices.Iterator.Element == Int {
var result: [Item] = [] var result = [Item]()
for index in indices { for index in indices {
result.append(self[index]) result.append(self[index])
} }

View File

@ -30,7 +30,7 @@ print("We're number \(one)!")
> >
> 如果你写过并发和多线程的代码,内存访问冲突也许是同样的问题。然而,这里访问冲突的讨论是在单线程的情境下讨论的,并没有使用并发或者多线程。 > 如果你写过并发和多线程的代码,内存访问冲突也许是同样的问题。然而,这里访问冲突的讨论是在单线程的情境下讨论的,并没有使用并发或者多线程。
> >
> 如果你曾经在单线程代码里有访问冲突Swift 可以保证你在编译或者运行时会得到错误。对于多线程的代码,可以使用 [Thread Sanitizer](https://developer.apple.com/documentation/xcode/diagnosing_memory_thread_and_crash_issues_early) 去帮助检测多线程的冲突。 > 如果你曾经在单线程代码里有访问冲突Swift 可以保证你在编译或者运行时会得到错误。对于多线程的代码,可以使用 [Thread Sanitizer](https://developer.apple.com/documentation/code_diagnostics/thread_sanitizer) 去帮助检测多线程的冲突。
### 内存访问性质 {#characteristics-of-memory-access} ### 内存访问性质 {#characteristics-of-memory-access}

View File

@ -1 +0,0 @@
# 并发

View File

@ -23,7 +23,6 @@
* [析构过程](02_language_guide/15_Deinitialization.md) * [析构过程](02_language_guide/15_Deinitialization.md)
* [可选链](02_language_guide/16_Optional_Chaining.md) * [可选链](02_language_guide/16_Optional_Chaining.md)
* [错误处理](02_language_guide/17_Error_Handling.md) * [错误处理](02_language_guide/17_Error_Handling.md)
* [并发](02_language_guide/28_Concurrency.md)
* [类型转换](02_language_guide/18_Type_Casting.md) * [类型转换](02_language_guide/18_Type_Casting.md)
* [嵌套类型](02_language_guide/19_Nested_Types.md) * [嵌套类型](02_language_guide/19_Nested_Types.md)
* [扩展](02_language_guide/20_Extensions.md) * [扩展](02_language_guide/20_Extensions.md)