Skip to content

Instantly share code, notes, and snippets.

@jessearmand
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessearmand/15f0efee5d453e7a614b to your computer and use it in GitHub Desktop.
Save jessearmand/15f0efee5d453e7a614b to your computer and use it in GitHub Desktop.
QuizApp
import Foundation
class Answer {
let questionId = 0
let answerId = 0
let answer: String?
}
public class Question {
let questionId = 0
let question: String?
var answers: [Answer] = []
init(questionId: Int, question: String) {
self.questionId = questionId
self.question = question
}
}
class Solution {
let questionId = 0
let answer: Answer?
}
class Category {
var name: String?
var questions: [Int: Question] = [:]
var solutions: [Int: Answer] = [:]
func randomQuestion() -> Question {
let numberOfQuestions = self.questions.count
let questionId = Int(arc4random_uniform(UInt32(numberOfQuestions)))
return self.questions[questionId]!
}
}
class QuizApp {
var category = Category()
init(category: String) {
self.category.name = category;
var questions: [Int: Question] = [:]
for index in 1...10 {
let question = Question(questionId: index, question: "How many times does a ball bounces and hit the ground, when it's thrown from 100 meters above the ground?")
questions[index] = question
self.category.questions = questions
}
}
public func getQuestion() -> Question {
return self.category.randomQuestion();
}
}
let quiz = QuizApp(category: "Science")
let question = quiz.getQuestion()
let questionString = question.question!
println("\(questionString)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment