第一次构建应用程序,如果这是一个微不足道的修复,请原谅。当我尝试构建时,我在 youtube 上寻找帮助,在 google 上搜索,并在 StackOverflow 上搜索,但似乎没有任何内容可以修复错误。有人能看一下告诉我哪里出错了吗?看看这个的工作副本我找不到区别=\
我遇到的错误是(主要是在情况 2/情况 3 行上): - 一行中的连续语句必须用“;”分隔 - 预期表达 -使用未解析的标识符“RandomQuestions” -使用未解析的标识符“case 2”(和“case 3”)
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var QuestionLabel: UILabel!
@IBOutlet var Button1: UIButton!
@IBOutlet var Button2: UIButton!
@IBOutlet var Button3: UIButton!
@IBOutlet var Button4: UIButton!
var CorrectAnswer = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
RandomQuestions()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func RandomQuestioins(){
var RandomNumber = arc4random() % 4
RandomNumber += 1
switch(RandomNumber){
case 1:
QuestionLabel.text = "Hello World, What Is My Name?"
Button1.setTitle("John", forState: UIControlState.Normal)
Button2.setTitle("Ryan", forState: UIControlState.Normal)
Button3.setTitle("Alex", forState: UIControlState.Normal)
Button4.setTitle("Google", forState: UIControlState.Normal)
CorrectAnswer = "2"
break
case2:
QuestionLabel.text = "What country do you live in?"
Button1.setTitle("Russia", forState: UIControlState.Normal)
Button2.setTitle("Good Question", forState: UIControlState.Normal)
Button3.setTitle("USA", forState: UIControlState.Normal)
Button4.setTitle("Mexico", forState: UIControlState.Normal)
CorrectAnswer = "3"
break
case3:
QuestionLabel.text = "What day is it?"
Button1.setTitle("Saturday", forState: UIControlState.Normal)
Button2.setTitle("Sunday", forState: UIControlState.Normal)
Button3.setTitle("Monday", forState: UIControlState.Normal)
Button4.setTitle("Thusday", forState: UIControlState.Normal)
CorrectAnswer = "2"
break
case 4:
QuestionLabel.text = "What is your favorite color?"
Button1.setTitle("Blue", forState: UIControlState.Normal)
Button2.setTitle("Red", forState: UIControlState.Normal)
Button3.setTitle("Green", forState: UIControlState.Normal)
Button4.setTitle("Yellow", forState: UIControlState.Normal)
CorrectAnswer = "1"
break
default:
break
}
}
@IBAction func Button1Action(sender: AnyObject) {
if (CorrectAnswer == "1"){
NSLog("Correct")
}
else{
NSLog("Try Again!")
}
}
@IBAction func Button2Action(sender: AnyObject) {
if (CorrectAnswer == "2"){
NSLog("Correct")
}
else{
NSLog("Try Again!")
}
}
@IBAction func Button3Action(sender: AnyObject) {
if (CorrectAnswer == "3"){
NSLog("Correct")
}
else{
NSLog("Try Again!")
}
}
@IBAction func Button4Action(sender: AnyObject) {
if (CorrectAnswer == "4"){
NSLog("Correct")
}
else{
NSLog("Try Again!")
}
}
}
最佳答案
未解析的标识符仅意味着您没有定义变量,因此 XCode 不知道您在寻找什么。
在您的情况下,您遗漏了 case2 和 case 3 之间的空格。它应该是 case 2:
此外,通常函数名称应该是驼峰式命名(第一个字符小写),因此如果您可以将 RandomQuestions() 重命名为 randomQuestions() 那就太好了。并尝试以“doSomething”方式命名您的函数,例如 setRandomQuestions 或generateRandomQuestions()
关于ios - 测验应用程序 - 连续陈述错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38428184/