|
Hey all, I'm trying the Day 35 challenge an having some severe problems with accessing an array. It seems to me that I'm missing some important concepts. I created an empty array and trying to fill it with the questions to display for the game. It looks like this:
What I'm trying to do is to start the game on button press. At this point I know which tables have to be learned (called categories). For each category I pre-generated all questions and answers. Now I want to put all question objects to the gameQuestions array using an append. The code compiles, the game runs, but there is no object put into the array. The append does not work, neither does insert or something. I can access all the Questions objects and its questionString, as the print statement shows them all correctly on the console. Can you help me to understand which concepts I'm missing here regarding the usage of the array, the @State and working with it? Best regards Andy |
|
You could try
Your |
|
Thank you, just tried it exactly this way. Unfortunately it does not work. Same problem, the array stays empty. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Put a breakpoint on the Does the structure match |
|
Instead of all that looping over indices, do something like this:
|
|
@Greenamberred: Yes, it prints out Question and its content. Thanks for the po advice. I didn't know it yet. @roosterboy: Thanks for your suggestion. It makes the code much more readable an shorter. Great takeaway for me. Unfortunately this also does not work, as gameQuestions still stays empty. |
|
I'm thinking there has to be something going on in the code you didn't post because both my suggestion and that from @Greenamberred should have worked. Can you post your entire |
|
@roosterboy: Yes, no problem. It compiles and the app can be started. It´s still quite dirty coded, maybe there lies some hidden problem. Here we go:
|
|
When I use that code and stick a So
But this doesn't decrement It should be:
This will decrease the number of questions remaining until the user has gone through them all. This is called a
Make those two changes and see if your game functions the way you were expecting. Also, as just a stylistic thing, the Swift standard is to use names that start with a lowercase letter for functions and variables, and names that start with uppercase letters for types. You have used names like
For someone reading this code for the first time, this looks like you are initializing a type called Your functions should have names like It also helps to name your functions with verb forms, so |
|
One other thing you have two In the meantime you could try moving each alert to a separate button - as suggested here. |
|
@roosterboy: Thank you so much for your help. You're right, printing the gameQuestions Array outputs it contents and shows it's not empty. Still I wonder, why the XCode Debugging View for Variables shows that the array is empty. Regarding the numberOfQuestions decrement, you're absolutely right. It was an ugly typo. Thanks for pointing it out. I also will use another variable for the Picker and the "questions left" counting. I'll take those two changes into account and see how the game behaves. Also many thanks for the hint on naming of functions. I wasn't aware of this. I want to learn the coding styleguide of Swift, thus I'll change my functions to lower case. In my workplace we are using Upper Case for functions, but not in a iOS / Swift environment. @Greenamberred: Great suggestion, I wasn't aware of this. I'll take it into account an refactor this point. |
|
Just a short update. Thanks to your help I finally got this game running. I learned a ton of new stuff. |
|
That's great to hear! It might help others find the answers if you mark this as |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.