在字典中再添加2个字符串

时间:2020-05-13 18:56:14

标签: ios swift xcode

这就是我的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",

1 个答案:

答案 0 :(得分:2)

更好地使用枚举和结构:

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