@ -65,7 +65,7 @@ var someInt = 3
|
||||
var anotherInt = 107
|
||||
swapTwoInts(&someInt, &anotherInt)
|
||||
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
|
||||
// 打印 "someInt is now 107, and anotherInt is now 3"
|
||||
// 打印“someInt is now 107, and anotherInt is now 3”
|
||||
|
||||
```
|
||||
`swapTwoInts(_:_:)` 函数很实用,但它只能作用于 `Int` 类型。如果你想交换两个 `String` 类型值,或者 `Double` 类型值,你必须编写对应的函数,类似下面 `swapTwoStrings(_:_:)` 和 `swapTwoDoubles(_:_:)` 函数:
|
||||
@ -268,7 +268,7 @@ extension Stack {
|
||||
if let topItem = stackOfStrings.topItem {
|
||||
print("The top item on the stack is \(topItem).")
|
||||
}
|
||||
// 打印 “The top item on the stack is tres.”
|
||||
// 打印“The top item on the stack is tres.”
|
||||
```
|
||||
|
||||
<a name="type_constraints"></a>
|
||||
@ -316,7 +316,7 @@ let strings = ["cat", "dog", "llama", "parakeet", "terrapin"]
|
||||
if let foundIndex = findIndex(ofString: "llama", in: strings) {
|
||||
print("The index of llama is \(foundIndex)")
|
||||
}
|
||||
// 打印 “The index of llama is 2”
|
||||
// 打印“The index of llama is 2”
|
||||
```
|
||||
如果只能查找字符串在数组中的索引,用处不是很大。不过,你可以用占位类型 `T` 替换 `String` 类型来写出具有相同功能的泛型函数 `findIndex(_:_:)`。
|
||||
|
||||
@ -598,7 +598,7 @@ if allItemsMatch(stackOfStrings, arrayOfStrings) {
|
||||
} else {
|
||||
print("Not all items match.")
|
||||
}
|
||||
// 打印 “All items match.”
|
||||
// 打印“All items match.”
|
||||
```
|
||||
上面的例子创建 `Stack` 实例来存储 `String` 值,然后将三个字符串压栈。这个例子还通过数组字面量创建了一个 `Array` 实例,数组中包含同栈中一样的三个字符串。即使栈和数组是不同的类型,但它们都遵从 `Container` 协议,而且它们都包含相同类型的值。因此你可以用这两个容器作为参数来调用 `allItemsMatch(_:_:)` 函数。在上面的例子中,`allItemsMatch(_:_:)` 函数正确地显示了这两个容器中的所有元素都是相互匹配的。
|
||||
|
||||
@ -630,7 +630,7 @@ if stackOfStrings.isTop("tres") {
|
||||
} else {
|
||||
print("Top element is something else.")
|
||||
}
|
||||
// 打印 "Top element is tres."
|
||||
// 打印“Top element is tres.”
|
||||
```
|
||||
|
||||
如果尝试在其元素不符合 `Equatable` 协议的栈上调用 `isTop(_:)` 方法,则会收到编译时错误。
|
||||
@ -660,7 +660,7 @@ if [9, 9, 9].startsWith(42) {
|
||||
} else {
|
||||
print("Starts with something else.")
|
||||
}
|
||||
// 打印 "Starts with something else."
|
||||
// 打印“Starts with something else.”
|
||||
```
|
||||
|
||||
上述示例中的泛型 `where` 子句要求 `Item` 遵循协议,但也可以编写一个泛型 `where` 子句去要求 `Item` 为特定类型。例如:
|
||||
@ -676,7 +676,7 @@ extension Container where Item == Double {
|
||||
}
|
||||
}
|
||||
print([1260.0, 1200.0, 98.6, 37.0].average())
|
||||
// 打印 "648.9"
|
||||
// 打印“648.9”
|
||||
```
|
||||
此示例将一个 `average()` 方法添加到 `Item` 类型为 `Double` 的容器中。此方法遍历容器中的元素将其累加,并除以容器的数量计算平均值。它将数量从 `Int` 转换为 `Double` 确保能够进行浮点除法。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user