gpt4 book ai didi

ios - 如何遍历字典,通过快速按下按钮加载下一个项目

转载 作者:行者123 更新时间:2023-11-28 23:24:11 26 4
gpt4 key购买 nike

我正在制作一个应用程序,它会提出一系列问题,并在回答"is"或“否”时给出一个值。我有一个带有问题类型 Collection View 的初始 View Controller 。该 View Controller 将类型信息传递给具有问题布局(图像、标签、按钮)的新 View Controller 。在第二个 View Controller 上,我有一个 switch/case 语句来为传入的类型(通过 questionTypeReceived)提供正确的字典结果,从而正确填充初始 View 。一旦您回答了问题(简单的是或否),我想更新下一个问题。有什么建议可以完成这项工作吗?

额外信息:我重构了 View Controller (此处称为辅助 VC)的问题,因为我最初认为我想重用它,而不仅仅是重新加载它。所有方面都有效除了下一个问题的重新加载...是的,那是因为我没有代码可以做到这一点。我一直在不同的地方尝试不同的 for 循环,但不断出现错误,所以我返回到当前设置,因为没有错误

初始风险投资

    let names = [ "Work", "School", "Family", "Friends", "Random" ]

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
questionType = names[indexPath.row] as String
let storyboard:UIStoryboard = UIStoryboard(name: "questions", bundle: nil)
let questionsVC = storyboard.instantiateViewController(withIdentifier: "questionsViewController") as! questionsViewController
questionsVC.questionTypeReceived = questionType
self.navigationController?.pushViewController(questionsVC, animated: true)
performSegue(withIdentifier: "question_segue", sender: questionType)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let dest = segue.destination as? questionsViewController else {
print("Can't perform downcast")
return
}
dest.questionTypeReceived = questionType
}

二次风险投资

    var questionTypeReceived:String = String()
var answerTotalValue:Double = Double()
var questionNumber:Int = Int()
var questionBody = ""
var questionType = ""

let answerYesValue:Double = Double.random(in: 1...5)
let answerNoValue:Double = Double.random(in: 0..<3)

switch questionTypeReceived {

case "Random":
questionTypeReceived = "Random"
questionsImageView.image = UIImage(named: randomImages.randomElement()!)
let questionNumber = randomQuestions.keys.sorted()
let questionBody = randomQuestions.values
for (questionNumber, questionBody) in randomQuestions {
questionTitleLabel.text = "Question #\(questionNumber)"
questionsTextLabel.text = questionBody
}
default: return
}

let randomQuestions: [Int : String] = [
1: "Did you already agree to attend?",
2: "Does it cost money to attend?",
3: "Is it time consuming (4+ hours)?"
]

@IBAction func yesButtonPressed(_ sender: UIButton) {
answerTotalValue += answerYesValue
print(answerTotalValue)
print(questionTypeReceived)
}


最佳答案

关于如何处理属性和局部变量似乎有些困惑,例如,您有 1 个属性和 2 个名为 questionNumber 的局部变量,并且在我看来您也在迭代整个字典你只想要一个问题。

摆脱这种情况的一种方法和一种解决方案也适用于其他原因,例如不在您的 Controller 类中执行所有操作并分离职责,是将问题和它们的处理移动到一个单独的结构或类中。

让我们创建一个 Questions 结构来保存问题,跟踪已提出的问题并可以提供下一个要问的问题。

struct Questions {
private randomQuestions: [Int : String] = [
1: "Did you already agree to attend?",
2: "Does it cost money to attend?",
3: "Is it time consuming (4+ hours)?"
]

private var questionIndex = 0

mutating func nextQuestion() -> (number: Int, question: String?) {
questionIndex += 1
return (questionIndex, randomQuestions[questionIndex])
}
}

注意返回元组中字符串的问号,如果索引不存在问题 (questionIndex > 3),则返回的问题将为 nil

然后你可以像这样在你的代码中使用它,首先在你的VC中将它声明为一个属性

var questions = Questions()

然后在得到问题的时候

switch questionTypeReceived {
case "Random":
questionsImageView.image = UIImage(named: randomImages.randomElement()!)
let nextQuestion = question.nextQuestion()
if let question = nextQuestion.question {
questionTitleLabel.text = "Question #\(nextQuestion.number)"
questionsTextLabel.text = question
} else {
// no more questions to ask, this needs to be handled
}
default: return
}

因此编写新结构需要多做一些工作,但您在 VC 中的代码变得更简单并且不再需要许多属性和变量。

关于ios - 如何遍历字典,通过快速按下按钮加载下一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59121959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com