|
I am working on Day 35, and have figured out how to setup the Questions, etc. When I am trying to show the first question (which is in an array) I get an array index out of order. My (currently incomplete) view is set to just show the first question. I have created a function to run .onAppear and it it shows that I have my 5 questions and successfully prints all five to the console, but the view still crashes.
And the console error is
I am not sure why I get the array index when I initialize currentQuestion to 0. I must be missing something obvious. |
|
The problem count is 5 and currentQuestion is set to 0. The forEach is trying to get to 5 using 0, 1, 2, 3, 4, 5 which is 6 questions. Initializing currentQuestion to 1 should fix your problem. The way I understood the challenge is you get questions about the number the user picks. So if the choice was 7 the questions should be
your questions seem to be random on both sides. Hope that helps |
|
Thanks! I decided to do Random on both side to make the quiz harder :) However, since I am only trying to show the first question, (not yet looping thru), changing currentQuestion to 1 still caused the crash. Will keep poking around. and will post if I figure it out. I did a po currentQuestion and I am getting the following, does it not get initialized?
|
|
Looking in the debug console.. and entering po problems (which is the array used in the .onAppear) I am getting:
This is confusing because the printIt function used in .onAppear prints the problems array fine (as shown above). Is there some kind of view Initialization problem here I am not understanding? |
|
Well sorry about that! At 7:00am it looked like a simple problem lol. You didn't post the Problem struct, but i assumed question is a string array. Try this and see if this works.
Hope this time it actually helps lol |
|
I basically just copied your code and pasted it into a new XCode project. I had to change the name of the view to But, you don't show how your
|
|
Sorry about that.. Forgot to include Problem Struct (it was in my main ContentView)... (I also just created a GitRepo - https://github.com/TheApApp/TimesTable/tree/main to make life easier)
I do believe the problem has to be on how I pass from ContentView to GameView. I thought I could just define
in GameView and when I am loading GameView, I would pass in the Array like this
|
|
Things can get a bit complicated when trying to pass an array between views and modify it in SwiftUI. But, you will learn the best ways to do that later on in this course. For now, I would move the code for creating your |
|
|
|
But the initial render of the If you change to something like this instead, your problem should go away: first example removed
|
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.