1

I want to display a String in a separate function and just call the function when the user choose the wrong answer, the problem is when I create the function and I try to use it my app crash and is telling me an error about the index is out of range..... any suggestion how can I fix this? or a recommendation to do a better and clean job? Here is my code:

//This is my struct
struct Question {
var Question: String!
var Answers: [String]!
var Answer: Int!
var Img: UIImage!
var Info: String!
} 

override func viewDidLoad() {
    super.viewDidLoad()
//Here the way I display the questions
questions = [

        Question(
            Question: "Question #1",
            Answers: ["A","B","C","D"],
            Answer: 1,
            Img: UIImage.self(named: "steve"),
            Info: "Steve"
                ),
]

//Here is my function that I want to create to display the Info:
private function showInformation() {
infoLabel.text = questions[Question].Info
}

Ps: If need more details let me know, by the way my function to create a random question is this

private func pickingRandomQuestion() {
    if questions.count > 0  {
        questionNumber = random() % questions.count //This make a random pick of Question
        questionLabel.text = questions[questionNumber].Question //Converting quesitonLabel into TEXT
        answerNumber = questions[questionNumber].Answer
        imgDisplay.image = questions[questionNumber].Img

//Im trying to use one of this examples to display but is not working :(

        answerA.setTitle(questions[questionNumber].Answers[0], forState: .Normal)
        answerB.setTitle(questions[questionNumber].Answers[1], forState: .Normal)
        answerC.setTitle(questions[questionNumber].Answers[2], forState: .Normal)
        answerD.setTitle(questions[questionNumber].Answers[3], forState: .Normal)

        questions.removeAtIndex(questionNumber)
    } else {

        finishGame.hidden = false
        answerA.hidden = true
        answerB.hidden = true
        answerC.hidden = true
        answerD.hidden = true

    }
}
  • how about `"infoLabel.text = questions[Question]?[Info]!`" You could also try `"infoLabel.text = questions.valueForKeyPath("Question.Info")!`" – Michael Dautermann Sep 10 '16 at 07:07
  • do i use "" or without ""? – Roberto Viramontes Sep 10 '16 at 07:16
  • you do need quotes for the keys. So: "`infoLabel.text = questions["Question"]?["Info"]!`". Don't include the outside edge quotes which I only included to illustrate where the code starts. I found this information [in this related question](http://stackoverflow.com/questions/25475463/how-to-access-deeply-nested-dictionaries-in-swift). – Michael Dautermann Sep 10 '16 at 07:18
  • its not working I already read the post and is not working, im so sad right now – Roberto Viramontes Sep 10 '16 at 08:46

1 Answers1

1

You need to store your question's info in a property before you remove the question from the array of questions.

Add a property to your class:

var questionInfo = ""

In pickingRandomQuestion set the value before calling removeAtIndex:

questionInfo = questions[questionNumber].Info

Then use the property value in showInformation:

private function showInformation() {
    infoLabel.text = questionInfo
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • Your struct fields should really start with a lowercase letter. Only class names and struct names should start with an uppercase letter. – vacawama Sep 10 '16 at 09:06
  • If your question struct is only used as shown, there is not need to make the properties `var` and declare the without the `!`. Why use implicitly unwrapped optionals if you're never planning on initializing them to `nil`? – vacawama Sep 10 '16 at 09:13
  • Im new in swift sorry for some of the code is bad but thank you so much for the answer and i learn something new now, thank you again for the second advice you the best !! – Roberto Viramontes Sep 10 '16 at 22:42