arrays - 类型没有成员“下标”
问题描述
我目前正在通过我在 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
}
}
解决方案
我想在这里你想a: [String]
从问题中获得领域?但现在:Question[questionNumber][1]
你试图下标结构问题本身,所以我认为你需要从数组中获取问题并从中获取a: [String]
属性。像这样:
func possibleAnswers() -> [String] {
return quiz[questionNumber].a
}
推荐阅读
- javascript - Google Cloud Functions 部署:EROFS:只读文件系统
- xml - 如何使用 Groovy XmlParser 获取 XML 根元素属性,而无需对 XML 进行“双重解析”
- javascript - 在Javascript中搜索表格的多行和多列
- php - 只有工作日的 PHP Carbon 日期差异
- sql - 在两个条件下连接两个表,如果它无法连接到空白单元格的行
- windows - Windows 批处理脚本中的空间 (xcopy)
- html - 元素未显示并且 :hover 无法在 .svg 上工作
- python - AttributeError:类型对象“MyUser”没有属性“USERNAME_FIELD”
- django - 有没有办法用 Django 发送推送通知
- graph - 基于关键字的 OneDrive API 搜索仅提供部分信息,不提供有关页面详细信息的任何信息