i'm programming quizapp. keep getting error: "fatal error: unexpectedly found nil while unwrapping optional value" in line:"qlabel.text = levels[levelnumber].questions[qnumber].question
". don't understand this. added , removed outlet multiple times.
import uikit import spritekit struct question { var question: string var answers: [string] var answer: int } struct level { var questions = [question]() } class viewcontroller: uiviewcontroller { @iboutlet var qlabel: uilabel! @iboutlet var buttons: [uibutton]! var qnumber = int() var answernumber = int() var levels = [level]() var levelnumber = int() override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } func startgame() { //level1 // question1 let qquestion1 = "what's name of america's president?" let answers1 = ["barack obama", "donald duck", "micky maus", "peter pan"] let question1 = question(question: qquestion1, answers: answers1, answer: 0) // question2 let qquestion2 = "how old i?" let answers2 = ["10 years old.", "20 years old", "5 years old.", "1 year old."] let question2 = question(question: qquestion2, answers: answers2, answer: 2) // question3 let qquestion3 = "who won 2014 'soccer worlcup'?" let answers3 = ["austria", "usa", "mongolia", "germany"] let question3 = question(question: qquestion3, answers: answers3, answer: 3) // question let qquestion4 = "" let answers4 = ["", "", "", ""] let question4 = question(question: qquestion4, answers: answers4, answer: 1) let levelexample = level(questions: [question1, question2, question3, question4]) // levelscience // question5 let qquestion5 = "" let answers5 = ["", "", "", ""] let question5 = question(question: qquestion5, answers: answers5, answer: 2) // question6 let qquestion6 = "" let answers6 = ["", "", "", ""] let question6 = question(question: qquestion6, answers: answers6, answer: 0) // question7 let qquestion7 = "" let answers7 = ["", "", "", ""] let question7 = question(question: qquestion7, answers: answers7, answer: 1) // question8 let qquestion8 = "" let answers8 = ["", "", "", ""] let question8 = question(question: qquestion8, answers: answers8, answer: 3) let levelmoviesliterature = level(questions: [question5, question6, question7, question8]) levels = [levelexample, levelmoviesliterature] } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func examplelevelbutton(sender: anyobject) { examplecategory() } func examplecategory() { levelnumber = 0 startgame() pickquestion() } func pickquestion() { if levels[levelnumber].questions.count > 0 { qnumber = random() % levels[levelnumber].questions.count qlabel.text = levels[levelnumber].questions[qnumber].question answernumber = levels[levelnumber].questions[qnumber].answer in 0..<buttons.count { buttons[i].settitle(levels[levelnumber].questions[qnumber].answers[i], forstate: uicontrolstate.normal) } levels[levelnumber].questions.removeatindex(qnumber) } else { nslog("done!") } }
hoping help! n.f.m.
you call startgame
, sets levels
array after call pickquestion
. access element @ index 0 when array still empty.
Comments
Post a Comment