首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型没有成员“Subscript”

类型没有成员“Subscript”
EN

Stack Overflow用户
提问于 2021-03-07 02:35:23
回答 2查看 44关注 0票数 2

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

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

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

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

我试图在Stackoverflow上搜索它,但我还不能弄清楚为什么它会影响我的代码。

这就是我的QuizBrain。

代码语言:javascript
复制
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。

代码语言:javascript
复制
    func possibleAnswers() -> [String] {
    return Question[questionNumber][1] //Throws error "Type 'Question' has no member 'Subscript'
}

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

有人知道怎么回事吗?

下面是我的问题结构

代码语言:javascript
复制
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
}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-07 02:44:41

一条评论:

代码语言:javascript
复制
func possibleAnswers() -> [String] {
    return Question[questionNumber][1] 
    // may you want to access quiz[questionNumber].a 
} 

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

使用正确的问题描述:

代码语言:javascript
复制
func possibleAnswers() -> [String] {
    return quiz[questionNumber].answer
}
票数 0
EN

Stack Overflow用户

发布于 2021-03-07 02:49:39

我想在这里你想从问题中获取a: [String]字段?但是现在:你试图下标结构问题本身,所以我认为你需要从数组中获取问题,并在从数组中获取a: [String]属性之后。如下所示:

代码语言:javascript
复制
    func possibleAnswers() -> [String] {
         return quiz[questionNumber].a 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66509306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档