0

我通过 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)"
}
4

1 回答 1

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
     }
}
于 2014-10-30T00:58:43.343 回答