【发布时间】:2015-12-31 00:25:25
【问题描述】:
我正在参加 iTunes 大学的课程,名为“使用 Swift 开发 iOS 8 应用程序”。在第三个视频中,我遇到了一个视频中没有出现的问题,即使是相同的代码,如下:
class ViewController: UIViewController{
…
@IBAction func operate(sender: UIButton) {
if userIsInTheMiddleOfTypingANumber{
enter()
}
if let operation = sender.currentTitle {
if let result = brain.performOperation(operation) { > ERROR HERE
displayValue = result
} else {
displayValue = 0
}
}
}
…
}
在阅读了很多关于这个错误的解释后,我想问题来自这里:
class CalculatorBrain
{
…
func performOperation(symbol: String) {
if let operation = knownOps[symbol] { opStack.append(operation)
}
}
}
如果你能帮助我,谢谢!
【问题讨论】:
-
brain.performOperation(operation)是返回任何东西还是一个 void 函数? -
错误是什么?你想让这段代码做什么?
-
performOperation 方法需要返回一个可选类型。 Swift 的错误描述真的很糟糕,但是这个描述得很好
-
func performOperation(symbol: String) -> Int?
-
Leo Dabus:它与 func performOperation(symbol: String) -> Double?谢谢你!!