4 Commits

Author SHA1 Message Date
1adc64e619 feat:示例代码更新 2021-06-26 11:03:07 +08:00
872d7af8a3 fix:示例代码更新 (#1148) 2021-06-22 13:03:11 -05:00
24d88f684a fix:示例代码更新 (#1147) 2021-06-22 13:03:02 -05:00
a15735f51b fix:for -> for-in (#1146) 2021-06-22 11:08:23 -05:00
4 changed files with 8 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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