swift - 完成某个类别下的测验时应用程序崩溃
问题描述
在我正在构建的应用程序中,我有一个测验功能。在测验中,用户可以选择仅询问问题是否属于某个类别。一旦用户完成测验,当所有问题都被问到时,会有一个弹出窗口让用户知道测验已经完成。但是,当用户选择仅从特定类别中获取问题时,该最终弹出窗口会更新显示,并且应用程序会显示新一轮的一个额外问题,并在回答该问题后崩溃。我不知道是什么原因造成的,因为我的代码正在检查当包含所有问题的变量为空时,不会调用获取下一个问题的方法。我确实认为这与检查的代码没有正确“连接”到功能文件有关。
使用的类别是"common"
或.Common
我得到下一个问题的代码:(这是在与 VC 不同的文件中)
fileprivate func startElement() {
if MCTitle.contains("common"){
if elementHasStarted {
for question in elements {
if question.usageRating == .Common {
commonElementsQuestions.append(MCQuestion(body: question.dutchName, correctAnswer: question.symbol, usageRating: question.usageRating, answers: nil))
symbolEnglishNames.append(question.symbol)
}
}
}
} else {
if ElementQuestions.isEmpty && symbolEnglishNames.isEmpty {
for question in elements {
ElementQuestions.append(MCQuestion(body: question.dutchName, correctAnswer: question.symbol, usageRating: question.usageRating, answers: nil))
symbolEnglishNames.append(question.symbol)
}
}
}
}
func getNewElementQuestion(viewTitle: String) -> MCQuestion {
startElement()
MCTitle = viewTitle
// getting new question and removing it from all the questions.
if viewTitle.contains("common") {
newQuestion = commonElementsQuestions.randomElement()! // here is the app crashing
if let index = commonElementsQuestions.firstIndex(where: {$0.body == newQuestion.body}) {
commonElementsQuestions.remove(at: index)
}
} else {
newQuestion = ElementQuestions.randomElement()!
if let index = ElementQuestions.firstIndex(where: {$0.body == newQuestion.body}) {
ElementQuestions.remove(at: index)
}
}
// asinging correct answer to the corectanswer variable and removing that answer from the array
correctAnswer = newQuestion.correctAnswer
if let index = symbolEnglishNames.firstIndex(where: {$0 == correctAnswer}) {
symbolEnglishNames.remove(at: index)
}
//asinging the answers for the question.
let answers = QuestionOptions(correctAnswer: correctAnswer, answer1: getNewSymbolForGuessTheElement(), answer2: getNewSymbolForGuessTheElement(), answer3: getNewSymbolForGuessTheElement())
return MCQuestion(body: newQuestion.body, correctAnswer: correctAnswer, usageRating: newQuestion.usageRating, answers: answers)
}
然后,在我的视图控制器中,我有这段代码来检查问题数组是否为空。
var quizFunctionality = MCQuizFunctionality()
@IBAction func answers(_ sender: UIButton) {
let dismissHUDDelay = 0.5
var style: JGProgressHUDStyle {
if #available(iOS 13.0, *) {
if self.traitCollection.userInterfaceStyle == .dark {
return .dark
} else {
return .light
}
} else {
return .light
}
}
if sender.tag == 1 {
let hud = JGProgressHUD(style: style)
hud.indicatorView = JGProgressHUDSuccessIndicatorView()
hud.textLabel.text = "Correct!"
hud.show(in: self.view)
hud.dismiss()
if LCQuizSubject.contains("Common") {
if self.quizFunctionality.commonElementsQuestions.isEmpty {
FinishedQuizAlert()
} else {
self.getQuestion()
}
} else if LCQuizSubject.contains("Symbol") {
if !self.quizFunctionality.symbolQuestions.isEmpty {
self.getQuestion()
} else {
FinishedQuizAlert()
}
} else {
if !quizFunctionality.ElementQuestions.isEmpty {
self.getQuestion()
} else {
FinishedQuizAlert()
}
}
} else {
let hud = JGProgressHUD(style: style)
hud.indicatorView = JGProgressHUDErrorIndicatorView()
hud.textLabel.text = "Wrong!"
hud.show(in: self.view)
hud.dismiss(afterDelay: dismissHUDDelay)
}
}
我希望我的问题很清楚,并且我提供了足够的代码。
我已尽力解释我的问题。如果不清楚,请告诉我!我只是一个没有那么长时间编码的人,并试图学习很多东西!
解决方案
推荐阅读
- javascript - 如何在 Javascript 中使用 For 循环,例如 jQuery 中的每个循环
- sql - Snowflake While 循环继续 sql 直到 @Counter 达到 365
- python - Django如何在模板中获取GET参数
- javascript - Lightbox2脚本+“条件点击”?
- node.js - NodeJS:提高 SFTP 服务器性能
- laravel - Spatie 媒体库 (Pro) - 将身份验证令牌附加到“上传”调用
- java - 如果在线程中调用函数,则 mockito 模拟静态函数不起作用
- c# - 使用 C# 批量编辑 XML 中的特定元素
- python - 带有数组和 for 循环的 BMI 计算器,得到 TypeError
- firebase-storage - 如何在 Firebase Cloud Storage Emulator 中创建额外的存储桶