Reputation: 77
I am building a trivia app and I have made a class for creating questions. I also made a function that will load when viewDidLoad().
class Question{
var quizQuestion : String = ""
var firstAnswer : String = ""
var secondAnswer : String = ""
var thirdAnswer : String = ""
var fourthAnswer : String = ""
var rightAnswer : Int = 0
init(question:String, answerOne:String, answerTwo:String, answerThree:String, answerFour:String, correctAnswer:Int)
{
self.quizQuestion = question
self.firstAnswer = answerOne
self.secondAnswer = answerTwo
self.thirdAnswer = answerThree
self.fourthAnswer = answerFour
self.rightAnswer = correctAnswer
}
}
let one = Question(question: "What was the first planet to be discovered using a telescope, in 1781?", answerOne: "Mars", answerTwo: "Jupiter", answerThree: "Uranus", answerFour: "Mercury", correctAnswer: 3)
my initialState() function
func initialState() {
var firstQuestion: AnyObject = arrayOfQuestions[0]
currentQuestion = 0
questionLabel.text = "\(firstQuestion[0])"
//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion[1])", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion[2])", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion[3])", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion[4])", forState: UIControlState.Normal)
}
When I want to set the buttons text to the index of the question and answers, it says 'Question' does not have a member named 'subscript', which makes no sense to me. When I run the app, everything is set to "nil."
Can anyone help with this? Please explain your answers as I am trying to learn the language and the errors.
Upvotes: 0
Views: 328
Reputation: 24572
Restructure the code to use an answer array. It will be easier to manage. Also give the correct answer as the index of the array.
class Question{
var quizQuestion : String = ""
var answers : Array<String> = Array()
var rightAnswer : Int = 0
init(question:String, answerOne:String, answerTwo:String, answerThree:String, answerFour:String, correctAnswer:Int)
{
self.quizQuestion = question
self.answers.append(answerOne)
self.answers.append(answerTwo)
self.answers.append(answerThree)
self.answers.append(answerFour)
self.rightAnswer = correctAnswer
}
}
let one = Question(question: "What was the first planet to be discovered using a telescope, in 1781?",
answerOne: "Mars", answerTwo: "Jupiter", answerThree: "Uranus", answerFour: "Mercury", correctAnswer: 3)
func initialState() {
var firstQuestion: Question = arrayOfQuestions[0]
questionLabel.text = "\(firstQuestion.quizQuestion)"
//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion.answers[0])", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion.answers[1])", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion.answers[2])", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion.answers[3])", forState: UIControlState.Normal)
}
Upvotes: 1
Reputation: 16124
questionLabel.text = "\(firstQuestion.quizQuestion)"
//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion.firstAnswer)", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion.secondAnswer)", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion.thirdAnswer)", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion.fourthAnswer)", forState: UIControlState.Normal)
Upvotes: 2