ios - 在字典中添加超过 2 个字符串
问题描述
所以这就是我的Dictionary
样子:
var questions: [String : String] = [
"Question1" : "Answer1",
"Question2" : "Answer2",
"Question3" : "Answer3",
"Question4" : "Answer4",
"Question5" : "Answer5"
]
我随机选择问题和相同的答案,如下所示:
@IBAction func newQuestionButton(_ sender: Any) {
guard currentQuestionIndex != questions.count else {
return
}
// This will give you the Question (and Key)
let question = randomQuestions[currentQuestionIndex]
// Use the Key to extract out the answer (value) from the Dictionary
let answer = questions[question] ?? ""
// Update your labels
questionLabel.text = question
answerLabel.text = answer
// Increment your question index
currentQuestionIndex += 1
}
我该如何更改,以便在“问题”中添加一个类别?类似的东西:"Food" : "Question1" : "Answer1",
。
解决方案
更好地使用枚举和结构:
enum Category{
case drink
case food
}
struct QuestionBlock: Comparable{
let question: String
let answer: String
static func < (lhs: QuestionBlock, rhs: QuestionBlock) -> Bool {
return lhs.question < rhs.question
}
}
两者可以聚集在一起:
var questions: [Category: [QuestionBlock]] = [
.drink: [QuestionBlock(question: "Question1", answer: "Answer1"),
QuestionBlock(question: "Question2", answer: "Answer2")],
.food: [QuestionBlock(question: "FoodQuestion1", answer: "FoodAnswer1"),
QuestionBlock(question: "FoodQuestion1", answer: "FoodAnswer1")]
]
用法:
var counter: [Category: Int] = [.food: 0, .drink: 0]
func getNewQuestion(type: Category) -> (question: String, answer: String)? {
guard let questionsArray = questions[type]?.sorted(by: <) else {
return nil
}
guard (counter[type] ?? 0) < questionsArray.count else {
return nil
}
let element = questionsArray[counter[type] ?? 0]
counter[type] = 1 + (counter[type] ?? 0)
return (question: element.question, answer: element.answer)
}
因此,newQuestionButton
只需调用函数getNewQuestion
,并检查 nil 的结果。我为每个类别制作了不同的计数器
还有另一种方法 - 做一个数组,并按问题编号对其进行排序:
struct QuestionBlock: Comparable{
let type: String
let question: String
let questionNumber: Int
let answer: String
static func < (lhs: ViewController.QuestionBlock, rhs: ViewController.QuestionBlock) -> Bool {
return lhs.questionNumber < rhs.questionNumber
}
}
您可以设置甚至更改问题数组:
var questions: [QuestionBlock] = [
QuestionBlock(type: "food", question: "Question1", questionNumber: 1, answer: "Answer1"),
QuestionBlock(type: "food", question: "Question2", questionNumber: 2, answer: "Answer2"),
QuestionBlock(type: "animal", question: "Question3", questionNumber: 3, answer: "Answer3"),
]
var counter: Int = 0
和检查将是相似的(这里计数器将是整数和所有类型的全局):
func getNewQuestion(type: String) -> (question: String, answer: String)? {
let questionsArray = questions.filter({$0.type == type}).sorted(by: <)
guard counter < questionsArray.count else {
return nil
}
let element = questionsArray[counter]
counter += 1
return (question: element.question, answer: element.answer)
}
推荐阅读
- c++ - 不支持 GLSL 330 内核
- python - 尝试使用 python 在 Instagram 上注册:https://instagram.com/accounts/emailsignup
- javascript - 使用 svg 元素作为范围创建 d3.js 比例
- r - 套索射击和 glmnet 不同的结果
- database - 使用 CSV 和 firebase
- testing - 测试 Rabbitmq ack/nack 响应
- node.js - 哪个是在 ubuntu 16.04 中同时安装 Nginx 和 apache 的最佳方式
- python-3.x - 三个强制的第二个参数
- c# - C# 和 SQL 简单身份验证(Visual Studio)
- tableau-api - 基于 Tableau 中的另一个筛选器自动设置筛选器