我收到一条错误消息,指出 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)"
}
案例必须详尽,否则您必须有默认声明。我修改了 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
}
}
检查“questionSelected”变量作用域和 Switch 语句作用域是否处于同一级别