首页 > 解决方案 > 类型没有成员“下标”

问题描述

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

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

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

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

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

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

最后一部分我想要做的是返回问题的所有可能答案的数组,以便我可以更新按钮的 currentTitle。

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

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

有谁知道怎么回事?

这是我的问题结构

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
}

}

标签: arraysswiftxcodedictionarystructure

解决方案


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

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

推荐阅读