1

我无法解决我的代码中的“如果让”问题。根据错误说我必须使用可选类型?

@IBAction func operate(sender: UIButton) {

    if userIsInTheMiddleOfTypingANumber{
        enter()
    }
    if let operation = sender.currentTitle {
        if let result = calcModel.performOperation(operation){
            displayValue = result
        }else {
            displayValue = 0
        }
    }
}
4

1 回答 1

1

我怀疑你的问题出在这一行:

if let result = calcModel.performOperation(operation)

您的performOperation函数的返回类型是()或 void,这意味着它不返回任何内容。有可能,要获得标题中的错误,您的performOperation函数 oncalcModel具有以下签名:

func performOperation(operation: String) {

请注意缺少箭头和类型。如果你要返回一个结果,签名应该是这样的:

func performOperation(operation: String) -> Int {

if let如果您决定返回,您只需要Int?.

于 2016-03-10T17:56:37.653 回答