Babou16 Babou16 - 4 months ago 25
Swift Question

Quiz app - consecutive statement error

First time building an app, so please excuse me if this is a trivial fix. I've gone on youtube looking for help, googled, and search StackOverflow with nothing that seems to be fixing the error when I attempt to build. Would somebody be able to take a look at tell me where I'm going wrong? Looking at working copies of this I can't find a difference =\

The errors I'm getting are (mostly on the case 2/case 3 lines):
-Consecutive statements on a line must be seperated by ';'
-Expected expression
-Use of unresolved identifier 'RandomQuestions'
-Use of unresolved identifier 'case 2' (and '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!")
}
}


}

Answer

Unresolved identifier just means that you didn't define the variable, hence XCode doesn't know what you are looking for.

In your case, you left out the space between case2 and case 3. It should be case 2:

Also, generally function names should be camel case (with the first char as lower case), so it would be great if you can rename RandomQuestions() to randomQuestions(). And try to name your functions in a "doSomething" manner, like, setRandomQuestions, or generateRandomQuestions()