skip to Main Content

I am currently learning programming in Swift from a course I bought on Udemy.

One of the things I am doing now is to build a quiz app. As a final challenge on the quiz app, I am converting the app to be a multiple choice quiz instead of just true or false.

Note: At this stage I have just learned about the MVC design pattern, and the challenge requires to keep it in this way.

So I have mapped out what I need to do, but I am running into a problem where I get the error "Type ‘Question’ has no member ‘Subscript’.

I tried to search for this on Stackoverflow, but I haven’t been able to figure out why this affects my code.

So this is my QuizBrain.

struct QuizBrain {
let quiz = [
    Question(q: "Which is the largest organ in the human body?", a: ["Heart", "Skin", "Large Intestine"], correctAnswer: "Skin"),
    Question(q: "Five dollars is worth how many nickels?", a: ["25", "50", "100"], correctAnswer: "100"),
    Question(q: "What do the letters in the GMT time zone stand for?", a: ["Global Meridian Time", "Greenwich Mean Time", "General Median Time"], correctAnswer: "Greenwich Mean Time"),
    Question(q: "What is the French word for 'hat'?", a: ["Chapeau", "Écharpe", "Bonnet"], correctAnswer: "Chapeau"),
    Question(q: "In past times, what would a gentleman keep in his fob pocket?", a: ["Notebook", "Handkerchief", "Watch"], correctAnswer: "Watch"),
    Question(q: "How would one say goodbye in Spanish?", a: ["Au Revoir", "Adiós", "Salir"], correctAnswer: "Adiós"),
    Question(q: "Which of these colours is NOT featured in the logo for Google?", a: ["Green", "Orange", "Blue"], correctAnswer: "Orange"),
    Question(q: "What alcoholic drink is made from molasses?", a: ["Rum", "Whisky", "Gin"], correctAnswer: "Rum"),
    Question(q: "What type of animal was Harambe?", a: ["Panda", "Gorilla", "Crocodile"], correctAnswer: "Gorilla"),
    Question(q: "Where is Tasmania located?", a: ["Indonesia", "Australia", "Scotland"], correctAnswer: "Australia")
    
    
]
var questionNumber = 0
var score = 0

mutating func checkAnswer(_ userAnswer: String) -> Bool {
    if userAnswer == quiz[questionNumber].correctAnswer {
        score += 1
        return true
    } else {
        return false
    }
}
func getProgress() -> Float {
    //Return progress
    return Float(questionNumber + 1) / Float(quiz.count)
}
func getQuestionText() -> String {
    //Return question text
    return quiz[questionNumber].text
}

mutating func nextQuestion() {
    if questionNumber + 1 < quiz.count {
        // updates the quiz number
        questionNumber += 1
    } else {
        //restarts the quiz
        questionNumber = 0
        score = 0
    }
}
func getScore() -> Int {
    return score
}
//Todo: Write functions that return possible answers to the UI buttons

func possibleAnswers() -> [String] {
    return Question[questionNumber][1] //Throws error "Type 'Question' has no member 'Subscript'
} }

and what I am trying to do with the last part is to return an array of all possible answers to the question so that I can update the currentTitle of my buttons.

    func possibleAnswers() -> [String] {
    return Question[questionNumber][1] //Throws error "Type 'Question' has no member 'Subscript'
}

In my head, this should work because I am returning the second object in the Question structure, which is an array, so my problem is most likely that my Syntax is wrong.

Anyone know what’s up?

Here is my question structure

struct Question {
let text: String
let answer: [String]
let correctAnswer: String

init(q: String, a: [String], correctAnswer: String) {
    text = q
    answer = a
    self.correctAnswer = correctAnswer
}

}

2

Answers


  1. One comment :

    func possibleAnswers() -> [String] {
        return Question[questionNumber][1] 
        // may you want to access quiz[questionNumber].a 
    } 
    

    As the Question structure is not described I can not tell if .a is the correct property to access the possible answers of required question

    With correct Question description :

    func possibleAnswers() -> [String] {
        return quiz[questionNumber].answer
    }
    
    Login or Signup to reply.
  2. I think here you want to get a: [String] field from Question? But now: Question[questionNumber][1] you trying to subscript structure question itself, so I think you need to get question from array and after get a: [String] property from it. Like this:

        func possibleAnswers() -> [String] {
             return quiz[questionNumber].a 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search