测验应用程序 - 连续语句错误

时间:2016-07-18 03:30:55

标签: ios swift xcode

第一次构建应用程序,所以请原谅我这是一个微不足道的修复。我在youtube上寻找帮助,谷歌搜索,并在我尝试构建时没有任何似乎正在修复错误的内容搜索StackOverflow。有人能看看告诉我哪里出错了吗?看看这份的工作副本,我找不到差异= \

我得到的错误(主要是案例2 /案例3行): - 一行上的连续陈述必须由';'分开。 - 期待的表达 - 使用未解析的标识符' RandomQuestions' - 使用未解决的标识符'案例2' (以及'案例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!")
        }
    }


}

1 个答案:

答案 0 :(得分:0)

未解析的标识符只是意味着您没有定义变量,因此XCode不知道您在寻找什么。

在你的情况下,你遗漏了case2和case 3之间的空格。它应该是case 2:

此外,通常函数名称应该是驼峰式的(第一个字符为小写),因此如果可以将RandomQuestions()重命名为randomQuestions(),那将会很棒。并尝试在" doSomething"中命名您的功能。方式,如setRandomQuestions或generateRandomQuestions()