首页 > 解决方案 > 在字典中添加超过 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",

标签: iosswiftxcode

解决方案


更好地使用枚举和结构:

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)
}

推荐阅读