【问题标题】:Type has no member 'Subscript'类型没有成员“下标”
【发布时间】:2021-06-05 02:57:48
【问题描述】:

我目前正在通过我在 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
}

}

【问题讨论】:

  • “问题结构,它是一个数组”不,不是。 quiz 是数组。记住?一旦你有一个问题,它的.a 就是另一个数组。
  • 好的,非常感谢!

标签: arrays swift xcode dictionary structure


【解决方案1】:

一条评论:

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

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

正确的问题描述:

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

【讨论】:

  • 我现在添加了我的问题结构。我尝试了你的解决方案,但我得到了同样的错误。
  • 您的评论帮助了我。我需要添加 .answer,而不是 .a
【解决方案2】:

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

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

【讨论】:

  • 所以下面的 Ptit Xav 很感兴趣。由于我的问题结构,我应该使用 .answer 而不是 .a。
猜你喜欢
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多