Swift 条件绑定的初始值设定项必须具有可选类型,而不是;()";

Swift 条件绑定的初始值设定项必须具有可选类型,而不是;()";,swift,xcode7.2,Swift,Xcode7.2,我无法解决代码中的“if-let”问题。根据错误,它说我必须使用可选类型 @IBAction func operate(sender: UIButton) { if userIsInTheMiddleOfTypingANumber{ enter() } if let operation = sender.currentTitle { if let result = calcModel.performOperation(operation)

我无法解决代码中的“if-let”问题。根据错误,它说我必须使用可选类型

@IBAction func operate(sender: UIButton) {

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

我怀疑您的问题在这方面:

if let result = calcModel.performOperation(operation)
您的
performOperation
函数的返回类型为
()
,或void,这意味着它不返回任何内容。要获取标题中的错误,您在
calcModel
上的
performOperation
函数可能具有以下签名:

func performOperation(operation: String) {
请注意,缺少箭头和字体。如果要返回结果,签名应该是这样的:

func performOperation(operation: String) -> Int {

如果您决定返回
Int?

谢谢Josh,那么您只需要
if let
!我想我发现了错误。我忘了执行performOperation来返回值。不客气,欢迎使用堆栈溢出