即使我在 Swift、Xcode 6 中声明,也会出现“预期声明”错误

问题描述 投票:0回答:2

我收到一条错误消息,指出 switch 语句的“预期声明” https://www.dropbox.com/s/3cjeo3sxg0zw431/Screen%20Shot%202014-10-30%20at%2001.01.48.png?dl=0

let questionSelected = Int(arc4random_uniform(1))


switch questionSelected{
case 0:
let x = "(question goes here)"
}
xcode swift switch-statement declaration xcode6.1
2个回答
1
投票

案例必须详尽,否则您必须有默认声明。我修改了 case 0:只是为了让它在游乐场中执行。

let questionSelected = Int(arc4random_uniform(1))


switch questionSelected{
case 0:
    let x = "(question goes here)"
default:
    break
}

好吧,又走了一步。这有效。将标签连接到故事板。

class ViewController: UIViewController {


    @IBOutlet weak var questionBox: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        let questionSelected = Int(arc4random_uniform(1))

        switch questionSelected{
        case 0:
            questionBox.text = "Does this work?"
        default:
            questionBox.text = "Does this work better?"
        }

    }
}

第二次更新:

func thisCodeMustBeInAFunction() {

     let questionSelected = Int(arc4random_uniform(1))

     switch questionSelected{
         case 1:
             questionBox.text = "(question goes here)"
         default:
             break
     }
}

0
投票

检查“questionSelected”变量作用域和 Switch 语句作用域是否处于同一级别

© www.soinside.com 2019 - 2024. All rights reserved.