【问题标题】:Why is this guard statement throwing me an error?为什么这个守卫语句给我一个错误?
【发布时间】:2015-12-11 04:47:45
【问题描述】:

所以我正在学习 Lynda.com 的教程,使用 Swift 制作 iOS 应用程序,当我插入这行代码时,它给我带来了错误:

guard let text:String = addressBar.text else

我得到的错误是: 连续的语句必须用';'分隔

一旦我有 Xcode 修复它,这些是我得到的错误: 预期的表达。 使用未解析的标识符“守卫”。 表达式解析为未使用的函数。 大括号语句块是未使用的闭包。

我真的是 Xcode 和 Swift 的新手,所以任何帮助都会很棒!谢谢!

【问题讨论】:

    标签: swift


    【解决方案1】:

    可能是您使用了错误的 Xcode 版本(7.0 版)

    也试试吧:

    确保您在正确的条件下使用了保护语句。例如:

    class AddressBar {
        var text: String? = ""
    }
    
    var addressBar = AddressBar()
    addressBar.text = nil
    //addressBar.text = "text"
    
    
    func test() {
        guard let _text: String = addressBar.text else {
            print("Nothing")
            return
        }
        print("I reach this point")
    }
    
    test()
    

    【讨论】:

      【解决方案2】:

      因为你使用过时的 xcode 和 swift 语言。最新版本是 xcode 7 和 swift 2。

      https://developer.apple.com/xcode/

      【讨论】:

        猜你喜欢
        • 2011-05-31
        • 1970-01-01
        • 1970-01-01
        • 2013-11-14
        • 1970-01-01
        • 1970-01-01
        • 2016-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多