class Question {
var type: QuestionType
var query: String
var answer: String
init(type: QuestionType, query: String, answer: String) {
self.type = type
self.query = query
self.answer = answer
}
}
enum QuestionType: String {
case trueFalse = "The sky is blue."
case multipleChoice = "Who is the ugliest Beatle: John, Paul, George or Ringo?"
case shortAnswer = "What is the capital of Oregon?"
case essay = "In 50 words, explain moleceular fusion"
static let types = [trueFalse, multipleChoice, shortAnswerm, essay]
}
enum AnswerType: String {
case trueFalse = "true"
case multipleChoice = "Sgt. Pepper"
case shortAnswer = "Salem"
case essay = "Molecular fusion happens when a daddy molecule and a mommy molecule love each other very much"
static let types = [trueFalse, multipleChoice, shortAnswerm, essay]
}
protocol QuestionGenerator {
func generateRandomQuestion() -> Question
}
class Quiz: QuestionGenerator {
func generateRandomQuestion() -> Question {
let randomNumeral = Int(arc4random_uniform(4))
let randomType = QuestionType.types[randomNumeral]
let randomQuery = randomType.rawValue
let randomAnswer = AnswerType.types[randomNumeral].rawValue
let randomQuestion = Question(type: randomType, query: randomQuery, answer: randomAnswer)
return randomQuestion
}
}
当我将鼠标移到让randomAnswer = AnswerType.types [randomNumeral] .rawValue 时,我看到一个显示错误类型的弹出窗口。我不明白为什么Playground认为有错误
答案 0 :(得分:2)
我将你的代码复制粘贴到游乐场中,我看到的唯一错误就是你的两个枚举中的'static let types'中'shortAnswerm'末尾的字符'm'。除此之外一切正常