Global scope for array of structs inside function - Swift

520 Views Asked by At

I would like to shuffle my array questions and use the shuffled array in the program when it starts. I have created this function that shuffles the array.

func shuffleQuestions() {
    var shuffledQuestions = shuffle(questions)
}

This gives me an array with questions shuffled that inherits the values from the struct below. The problem I have is that it is local to the shuffleQuestions function. How do I create an empty array of structs variable that I could declare globally to put the shuffled questions into. Would I need to create a new class?

func shuffle<C: MutableCollectionType where C.Index == Int>(var list: C) -> C {
    let c = count(list)
    for i in 0..<(c - 1) {
        let j = Int(arc4random_uniform(UInt32(c - i))) + i
        swap(&list[i], &list[j])
    }
    return list
}

My Array of Structs

class QuizQuestion {

    let question: String!
    let answer: Bool!
    let explanation: String!
    var usersAnswer: Bool?
    var answerSubmitted: Bool?

    init(question: String, answer: Bool, explanation: String) {
        self.question = question
        self.answer = answer
        self.explanation = explanation
    }
}

    var questions = [

    QuizQuestion(question: "Do I like coffee?", answer: true, explanation: "Because it's awesome!"),
    QuizQuestion(question: "Is bacon god's gift to mankind?", answer: true, explanation: "Because it's awesome!"),
    QuizQuestion(question: "Should I take a nap right now?", answer: true, explanation: "You gotta review some code!"),
    QuizQuestion(question: "Should I take a nap right now?", answer: true, explanation: "You gotta review some code!")
]
1

There are 1 best solutions below

2
On BEST ANSWER

"...that doesn`t seem to work..." is a variation on perhaps the least helpful thing you can say when asking for help. What do you mean it "doesn't seem to work"?!?!? How doesn't it work? Explain what it's doing that doesn't meet your needs. Is it crashing? Is the shuffledQuestions array not coming out shuffled?

You wrote a function shuffleQuestions that is totally useless. Look at what you wrote:

func shuffleQuestions() 
{
    var shuffledQuestions = shuffle(questions)
}

That function does not return a result. It declares a local variable, shuffledQuestions. It shuffles the contents of the questions array and stores the results into the local variable shuffledQuestions. Then the function returns.

What happens to the local variable shuffledQuestions when the function returns? You need to think about that until you come up with the answer to that question.

EDIT: With the update your question is clearer.

You wrote a function that doesn't do any good because it creates a sorted array as a local variable. Once the function returns, that variable is discarded.

You have a couple of choices. you can rewrite your function to return the sorted array:

func shuffleQuestions()  -> [QuizQuestion]
{
    return shuffledQuestions = shuffle(questions)
}

Or you could make your shuffleQuestions function operate on an instance variable of the class in which it's declared.

It looks like you are declaring your variable questions as an application global. Global variables are best avoided.

Like I said, it sounds like you don't really understand the concept of variable scope and parameter passing. You should go back and do some reading on basic programming concepts (I understand that there is a Big Nerd Ranch book that teaches Swift now. That might be a good choice.)