Ios 在字典中添加多于2个字符串

Ios 在字典中添加多于2个字符串,ios,swift,xcode,Ios,Swift,Xcode,这就是我的字典的样子: var questions: [String : String] = [ "Question1" : "Answer1", "Question2" : "Answer2", "Question3" : "Answer3", "Question4" : "Answer4", "Question5" : "Answer5" ] 我随机选择的问题和相同的答案如下: @IBAction func newQuestionButton(_ s

这就是我的
字典的样子:

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

    }

我该如何更改,以便在“问题”中添加一个类别?类似于:
“食物”:“问题1”:“答案1”,

更好地使用枚举和结构:

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 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)
}
您可以设置甚至更改问题数组:

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(类型:String)->(问题:String,答案:String)?{

let questionsArray=questions.filter({$0.type==type}).排序(按:最好的方法是使用Struct而不是dictionary…@jawadAli How tho?我需要使用循环显示吗?顺便说一下,与其使用
let question=randomQuestions[currentQuestionIndex]
不如使用
let question=randomQuestions.dropFirst(currentQuestionIndex)。首先
并选中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)
}