几天前我刚刚开始学习 Swift。在我的 Xcode 游乐场中,我有以下代码:
//: Playground - noun: a place where people can play
import UIKit
enum VendingMachineError: ErrorType {
case InvalidSelection
case InsufficientFunds(coinsNeeded: Int)
case OutOfStock
}
func requestBeverage(code: Int, coins: Int) throws {
guard code > 0 else {
throw VendingMachineError.InvalidSelection
}
if coins < 2 {
throw VendingMachineError.InsufficientFunds(coinsNeeded: 3)
}
guard coins > 10 else {
throw VendingMachineError.OutOfStock
}
print("everything went ok")
}
try requestBeverage(-1, coins: 4)
print("finished...")
如果我尝试运行它,什么也不会发生。但我希望打印“完成......”因为在我的逻辑中,它试图做某事,失败,然后程序将继续......
所以问题是,为什么程序不继续,我如何用尽可能少的单词告诉代码在出错的情况下继续?
提前致谢