1

This is a simple quiz app with a label and four buttons. I want code in the action button to execute by referencing the question in the if statement. (The problem with referencing the answer tag is that there are only four buttons, but more than four questions.) The code below gives an error that you can't use the binary operator. How can I make this work?

    struct Question {
        var Question : String!
        var Answers : [String]!
        var Answer : Int!
    }


    var Questions = [Question]()
        var QNumber = Int()
        var AnswerNumber = Int()

    Questions = [Question(Question: "One", Answers: ["", "", "", ""], Answer: 0),
                         Question(Question: "Two", Answers: ["", "", "", ""], Answer: 1),
                         Question(Question: "Three", Answers: ["", "", "", ""], Answer: 2),
                         Question(Question: "Four", Answers: ["", "", "", ""], Answer: 3),
                         Question(Question: "Five", Answers: ["", "", "", ""], Answer: 0),]


func PickQuestion(){
        if Questions.count > 0{
            QNumber = 0
            QLabel.text = Questions[QNumber].Question

            AnswerNumber = Questions[QNumber].Answer

            for i in 0..<Buttons.count{
                Buttons[i].setTitle(Questions[QNumber].Answers[i], for: UIControlState.normal)
            }
            Questions.remove(at: QNumber)
        }


    @IBAction func Btn4(_ sender: Any) {

                if(Question == "Five")  {

                    //CODE THAT NEEDS TO EXECUTVE
                } else if(Question == "Four") {
                   //EXECUTE A DIFFERENT CODE
                }
2
  • 2
    Where has myString come from? it would be helpful to see more of your code and the exact error you are getting. Commented Nov 22, 2016 at 15:01
  • 2
    What is your array? What does "a particular string is called" mean? What exactly should be compared? Commented Nov 22, 2016 at 15:05

1 Answer 1

1

Your question is super unclear and it looks like you would benefit from re-structuring your logic. That said, if this is a simple quiz app and you have a few buttons with tags 0, 1, 2 and 3 then you should simply be able to compare the Question's Answer property with the buttons tag and avoid comparing any strings altogether.

It's also not clear from your question how the "current question" is selected, so you may want to firm that one up too, I'd recommend storing the object in a var somewhere so you can do something like the following...

var currentQuestion:Question!

// Get the first question or something.
currentQuestion = Questions.first

@IBAction func buttonTapped(sender: UIButton) {
    if sender.tag == currentQuestion.Answer {
        print("You are a winner")
        // Time to get a new question son.
    }
}

The above code is untested and I hope it doesn't just confuse you further, however, in the current format your question may be closed as it is not completely clear what you are asking.


Edit:

Thanks for updating your question, It looks now like you are trying to compare the Question struct against the String "Five". these two objects are not comparable.

To make your code work you should use the AnswerNumber variable that you have made and check if the number matches like so.

@IBAction func Btn4(_ sender: Any) {
    if AnswerNumber == 4 {
        print("Correct Answer")
        //CODE THAT NEEDS TO EXECUTVE
    } else {
        print("Wrong Answer")
        //EXECUTE DIFFERENT CODE
    }
}

Assuming you have an IBAction for each button you will need to repeat this for each, so Btn5 would look like this.

@IBAction func Btn5(_ sender: Any) {
    if AnswerNumber == 5 {

    ...

Edit:

After chatting away, we figured out that you needed a custom action for each question (if the correct answer was selected). this took the form of an mp3 file that was played depending on which question it was.

We came to the conclusion that following the existing structure you should add another variable to hold the mp3 in the Question struct but also for the current question as below.

struct Question { 
    var Question : String! 
    var Answers : [String]! 
    var Answer : Int! 
    var audioFile: String! 
}

var AnswerMP3 = ""

Then when we set the current question alongside AnswerNumber we can set the mp3 like so.

AnswerMP3 = Questions[QNumber].audioFile

Then in this way you do not need to have hardcoded actions for each question. the buttons simply pass the correct mp3 on to another method that plays the audio file.

if AnswerNumer == 4 { 
    playMP3File(AnswerMP3) 
} 

func playMP3File(fileName:String) { 
// user the fileName to play the audio file. 
}
Sign up to request clarification or add additional context in comments.

8 Comments

I updated the post to try to make the goal more clear. I hope this helps. Thanks.
@Phillip I have updated my answer to include a fix for your code.
Thank you @Wez, but I actually have 10 questions and only four buttons... What do I do after the fourth question? I already have AnswerNumber compared to 1-4. I can't use it twice for the same button.
Doesn't sound like a problem to me. Each question only has 4 Answers, The buttons are re-used for each question so it shouldn't matter how many questions you have unless some Questions have more answers than others.
Okay so let's say Question 1 and Question 2 both have the answer as the first option. So in button 1 "if AnswerNumber == 0 {" will be there twice. So that means it will run the code for both of the if statements because the if statements are exactly the same. It will run both codes. That's not good. I need one of the codes to run. Not both. If you use your method you can only call four different codes. I need to call 10 different codes. One for each question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.