有没有一种简单的方法来打乱 NSDictionary 的顺序?

Pot*_*sys -2 swift

我正在创建一个测验应用程序,并且有一个 NSDictionary,其键为问题,值为 true 或 false(答案)。我想从这个 NSDictionary 中随机选择 5 个值。如何打乱 NSDictionary 以随机选择值?

这是 NSDictionary

let questions: NSDictionary = [
    "A habit is a behavior that has been repeated enough times to become automatic.": true,
    "Spending a conversation talking mostly about yourself is a good way to influence people.": false,
    "In the 7 Habits of Highly Effective People, the Circle of Influence is all of the things inside an individual's control.": true,
    "In The Outliers by Malcolm Gladwell, the author states that the number of hours you practice is more important than your natural talent": true,
    "Think and Grow Rich by Napoleon Hill is based on the idea that anyone can achieve success if they have the right mindset.": true
];
Run Code Online (Sandbox Code Playgroud)

Geo*_*rth 5

使用 SwiftDictionary而不是NSDictionary此代码可以执行您所要求的操作 - 随机选择 5 个问题(您的示例只有 5 个问题,因此它们都被选中):

let questions: Dictionary = [
    "A habit is a behavior that has been repeated enough times to become automatic.": true,
    "Spending a conversation talking mostly about yourself is a good way to influence people.": false,
    "In the 7 Habits of Highly Effective People, the Circle of Influence is all of the things inside an individual's control.": true,
    "In The Outliers by Malcolm Gladwell, the author states that the number of hours you practice is more important than your natural talent": true,
    "Think and Grow Rich by Napoleon Hill is based on the idea that anyone can achieve success if they have the right mindset.": true
]

let randomQuestions = questions.keys.shuffled().prefix(5)

for question in randomQuestions {
    // It is safe to force unwrap the answer because we know that `question` exists in the keys
    let answer = questions[question]
    print("Q: \(question), A: \(answer)")
}
Run Code Online (Sandbox Code Playgroud)

正如其他人评论的那样,字典可能不是解决此问题的最佳数据结构。我认为更好的解决方案是使用 s 数组Question来实现适当的类型安全和更具可读性的代码:

struct Question {
    let title: String
    let answer: Bool
}

let questions = [
    Question(title: "A habit is a behavior that has been repeated enough times to become automatic.", answer: true),
    Question(title: "Spending a conversation talking mostly about yourself is a good way to influence people.", answer: false),
    Question(title: "In the 7 Habits of Highly Effective People, the Circle of Influence is all of the things inside an individual's control.", answer: true),
    Question(title: "In The Outliers by Malcolm Gladwell, the author states that the number of hours you practice is more important than your natural talent", answer: true),
    Question(title: "Think and Grow Rich by Napoleon Hill is based on the idea that anyone can achieve success if they have the right mindset.", answer: true)
]

let randomQuestions = questions.shuffled().prefix(5)

for question in randomQuestions {
    print("Q: \(question.title), A: \(question.answer)")
}
Run Code Online (Sandbox Code Playgroud)