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
}
myStringcome from? it would be helpful to see more of your code and the exact error you are getting.