简体   繁体   English

Swift错误:条件绑定的初始化程序必须具有可选类型,而不是'()'

[英]Swift error: Initializer for conditional binding must have Optional type, not '()'

I'm doing the course of iTunes University called "Developing iOS 8 Apps with Swift". 我正在上iTunes大学的课程,名为“使用Swift开发iOS 8应用程序”。 On the third video I encountered with a problem that did not happen in the video, even though it was the same code, that is as follows: 在第三个视频中,我遇到了一个问题,即使它是相同的代码,也没有发生在视频中,如下所示:

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
            }
        }
    } 
…
}

After reading many explanations of this error I suppose the problem comes from here: 阅读了许多关于此错误的解释后,我想问题出在这里:

class CalculatorBrain
{

…
func performOperation(symbol: String) {
        if let operation = knownOps[symbol] {            opStack.append(operation)
        }
    }
}

Thank you if you can help me! 谢谢您的帮助!

performOperation doesn't return anything and needs to return an optional type so that it can be used in your if let statement (to check if it did indeed return a value) and that's what it could be moaning about. performOperation不返回任何内容,需要返回一个可选类型,以便可以在您的if let语句中使用它(以检查它是否确实返回了一个值),这就是它的含义。

Try: 尝试:

func performOperation(symbol: String) -> Int? {

which means it could return an Int, and then your if let statement should be happy. 这意味着它可以返回一个Int,然后您的if let语句应该很高兴。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS Swift:用于条件绑定的初始化程序必须具有可选类型,而不是&#39;()-&gt; String? - iOS Swift: Initializer for conditional binding must have Optional type, not '() -> String? 条件绑定的初始化程序必须具有可选类型,而不是“字符串”-iOS-Swift - Initializer for conditional binding must have Optional type, not 'String' - ios - swift 条件绑定的初始化程序必须具有Optional类型,而不是&#39;IndexPath&#39;-Xcode 8,Swift 3 - Initializer for conditional binding must have Optional type, not 'IndexPath' - Xcode 8, Swift 3 Swift - 条件绑定的初始化程序必须具有可选类型,而不是 'PHFetchResult<phasset> '</phasset> - Swift - Initializer for conditional binding must have Optional type, not 'PHFetchResult<PHAsset>' 条件绑定的初始化程序必须具有可选类型 - Initializer for conditional binding must have optional type 条件绑定的初始化程序必须具有可选类型,而不是&#39;Error.Protocol&#39; - Initializer for conditional binding must have Optional type, not 'Error.Protocol' 出现此错误:条件绑定的初始化程序必须具有Optional类型,而不是&#39;Bool&#39; - Getting this error : Initializer for conditional binding must have Optional type, not 'Bool' Swift错误消息:用于条件绑定的初始化程序必须具有可选类型,而不是“ [CLBeacon]” - Swift Error Message: Initializer for conditional binding must have Optional type, not '[CLBeacon]' “条件绑定的初始化程序必须具有可选类型,而不是 &#39;[String : Any]&#39;” swift 中的错误 - "Initializer for conditional binding must have Optional type, not '[String : Any]'" error in swift Swift:错误-条件绑定的初始化程序必须具有可选类型,而不是&#39;Void&#39;(aka&#39;()&#39;) - Swift: Error - Initializer for conditional binding must have Optional type, not 'Void' (aka '()')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM