我下面有这样的课程来从plist那里挑选问题,但现在我想以随机顺序接受它们
class QuizLoader {
public func loadSimpleQuiz(forQuiz guizName: String) throws -> [SimpleQuestion] {
var questions = [SimpleQuestion]()
if let path = Bundle.main.path(forResource: guizName, ofType: "plist") {
if let dict = NSDictionary(contentsOfFile: path) {
let tempArray: Array = dict["Questions"]! as! [Dictionary<String,AnyObject>]
for dictionary in tempArray {
let questionToAdd = SimpleQuestion(question: dictionary["Question"] as! String, correctAnswer: dictionary["CorrectAnswer"] as! String)
}
return questions
} else {
throw LoaderError.dictionaryField
}
} else {
throw LoaderError.pathField
}
}
我尝试使用此hint执行此操作,但我在var unsignedArrayCount = UInt32(quoteDictionary.count)
我已经花了几个小时试图搞清楚,但仍然没有运气。
func loadQuestions() {
do {
questionArray = try quizeLoader.loadMultipleChoiceQuiz(forQuiz: "MultipleChoice")
loadNextQuestion()
} catch {
switch error {
case LoaderError.dictionaryField:
print("Could not load directory")
case LoaderError.pathField:
print("Cound not find valid file at path")
default:
print("Unknown error")
}
}
}
func loadNextQuestion() {
currentQuestion = questionArray[questionIndex]
setTitleForButtons()
}