@ -7,7 +7,7 @@
|
|||||||
>
|
>
|
||||||
> 2.2
|
> 2.2
|
||||||
> 翻译+校对:[SketchK](https://github.com/SketchK) 2016-05-15
|
> 翻译+校对:[SketchK](https://github.com/SketchK) 2016-05-15
|
||||||
>
|
>
|
||||||
> 3.0
|
> 3.0
|
||||||
> 翻译+校对:[shanks](http://codebuild.me) 2016-09-24
|
> 翻译+校对:[shanks](http://codebuild.me) 2016-09-24
|
||||||
> 3.0.1,shanks,2016-11-13
|
> 3.0.1,shanks,2016-11-13
|
||||||
@ -93,22 +93,22 @@ class VendingMachine {
|
|||||||
func dispenseSnack(snack: String) {
|
func dispenseSnack(snack: String) {
|
||||||
print("Dispensing \(snack)")
|
print("Dispensing \(snack)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func vend(itemNamed name: String) throws {
|
func vend(itemNamed name: String) throws {
|
||||||
guard let item = inventory[name] else {
|
guard let item = inventory[name] else {
|
||||||
throw VendingMachineError.InvalidSelection
|
throw VendingMachineError.InvalidSelection
|
||||||
}
|
}
|
||||||
|
|
||||||
guard item.count > 0 else {
|
guard item.count > 0 else {
|
||||||
throw VendingMachineError.OutOfStock
|
throw VendingMachineError.OutOfStock
|
||||||
}
|
}
|
||||||
|
|
||||||
guard item.price <= coinsDeposited else {
|
guard item.price <= coinsDeposited else {
|
||||||
throw VendingMachineError.InsufficientFunds(coinsNeeded: item.price - coinsDeposited)
|
throw VendingMachineError.InsufficientFunds(coinsNeeded: item.price - coinsDeposited)
|
||||||
}
|
}
|
||||||
|
|
||||||
coinsDeposited -= item.price
|
coinsDeposited -= item.price
|
||||||
|
|
||||||
var newItem = item
|
var newItem = item
|
||||||
newItem.count -= 1
|
newItem.count -= 1
|
||||||
inventory[name] = newItem
|
inventory[name] = newItem
|
||||||
@ -120,7 +120,7 @@ class VendingMachine {
|
|||||||
|
|
||||||
在`vend(itemNamed:)`方法的实现中使用了`guard`语句来提前退出方法,确保在购买某个物品所需的条件中,有任一条件不满足时,能提前退出方法并抛出相应的错误。由于`throw`语句会立即退出方法,所以物品只有在所有条件都满足时才会被售出。
|
在`vend(itemNamed:)`方法的实现中使用了`guard`语句来提前退出方法,确保在购买某个物品所需的条件中,有任一条件不满足时,能提前退出方法并抛出相应的错误。由于`throw`语句会立即退出方法,所以物品只有在所有条件都满足时才会被售出。
|
||||||
|
|
||||||
因为`vend(itemNamed:)`方法会传递出它抛出的任何错误,在你的代码中调用此方法的地方,必须要么直接处理这些错误——使用`do-catch`语句,`try?`或`try!`;要么继续将这些错误传递下去。例如下面例子中,`buyFavoriteSnack(_:vendingMachine:)`同样是一个 throwing 函数,任何由`vend(itemNamed:)`方法抛出的错误会一直被传递到`buyFavoriteSnack(person:vendingMachine:) `函数被调用的地方。
|
因为`vend(itemNamed:)`方法会传递出它抛出的任何错误,在你的代码中调用此方法的地方,必须要么直接处理这些错误——使用`do-catch`语句,`try?`或`try!`;要么继续将这些错误传递下去。例如下面例子中,`buyFavoriteSnack(person:vendingMachine:)`同样是一个 throwing 函数,任何由`vend(itemNamed:)`方法抛出的错误会一直被传递到`buyFavoriteSnack(person:vendingMachine:) `函数被调用的地方。
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let favoriteSnacks = [
|
let favoriteSnacks = [
|
||||||
@ -174,7 +174,7 @@ do {
|
|||||||
var vendingMachine = VendingMachine()
|
var vendingMachine = VendingMachine()
|
||||||
vendingMachine.coinsDeposited = 8
|
vendingMachine.coinsDeposited = 8
|
||||||
do {
|
do {
|
||||||
try buyFavoriteSnack("Alice", vendingMachine: vendingMachine)
|
try buyFavoriteSnack(person: "Alice", vendingMachine: vendingMachine)
|
||||||
} catch VendingMachineError.InvalidSelection {
|
} catch VendingMachineError.InvalidSelection {
|
||||||
print("Invalid Selection.")
|
print("Invalid Selection.")
|
||||||
} catch VendingMachineError.OutOfStock {
|
} catch VendingMachineError.OutOfStock {
|
||||||
@ -195,9 +195,9 @@ do {
|
|||||||
func someThrowingFunction() throws -> Int {
|
func someThrowingFunction() throws -> Int {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
let x = try? someThrowingFunction()
|
let x = try? someThrowingFunction()
|
||||||
|
|
||||||
let y: Int?
|
let y: Int?
|
||||||
do {
|
do {
|
||||||
y = try someThrowingFunction()
|
y = try someThrowingFunction()
|
||||||
|
|||||||
Reference in New Issue
Block a user