Phillip Phillip - 7 days ago 4
Swift Question

Xcode Swift: Converting Arrayed String With If Statement

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
}

Wez Wez
Answer

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. 
}
Comments