ios - How to declare variable that has a struct array outside function -
i need use currentquestion variable outside of randomquestiongenerator function. whats proper syntax declaring beforehand?
struct questions { var question: string var answer: int var answers: [string] } class gamescreen: uiviewcontroller { var correctanswer = 0 var fullquestions: [questions] = [] func randomquestiongenerator(){ let randomquestion = int(arc4random_uniform(uint32(fullquestions.count))) var currentquestion = fullquestions[randomquestion] correctanswer = currentquestion.answer
you can declare currentquestion
optional outside function:
var currentquestion : questions? = nil func randomquestiongenerator() { let randomquestion = int(arc4random_uniform(uint32(fullquestions.count))) currentquestion = fullquestions[randomquestion] correctanswer = currentquestion.answer }
although that, better approach make function return random question, this:
func randomquestiongenerator() -> questions { let randomquestion = int(arc4random_uniform(uint32(fullquestions.count))) return fullquestions[randomquestion] }
now can use function retrieve next random question, , fields needed:
let nextquestion = randomquestiongenerator() print(nextquestion.question) print(nextquestion.answers)
Comments
Post a Comment