类型没有成员 'Subscript'

Type has no member 'Subscript'

我目前正在 Swift 通过我在 Udemy 上购买的课程学习编程。

我现在正在做的一件事是构建一个测验应用程序。作为测验应用程序的最后一个挑战,我正在将该应用程序转换为多项选择测验,而不仅仅是对或错。

注:现阶段刚刚了解MVC设计模式,挑战需要保持这种方式。

所以我已经制定了我需要做的事情,但是我 运行 遇到了一个问题,我收到错误“类型 'Question' 没有成员 'Subscript'。

我试图在 Whosebug 上搜索这个,但我一直无法弄清楚为什么这会影响我的代码。

这就是我的 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'
} }

最后一部分我想做的是return问题所有可能答案的数组,这样我就可以更新按钮的当前标题。

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

在我看来,这应该可行,因为我正在 return 问题结构中的第二个 object,它是一个数组,所以我的问题很可能是我的语法错误。

有人知道怎么回事吗?

这是我的问题结构

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
}

}

一条评论:

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

由于未描述问题结构,我无法判断 .a 是否正确 属性 无法访问所需问题的可能答案

问题描述正确:

func possibleAnswers() -> [String] {
    return quiz[questionNumber].answer
}

我想你想从问题中获取 a: [String] 字段?但是现在:Question[questionNumber][1] 你试图下标结构问题本身,所以我认为你需要从数组中获取问题,然后从中获取 a: [String] 属性。像这样:

    func possibleAnswers() -> [String] {
         return quiz[questionNumber].a 
}