|
Hi I wonder if anyone can help with this. I have been attempting the timestable game challenge from day 35 and although I was able to come up with a question generator based on a user input, I am having trouble making it recognise the answer correctly above the 2 times table. I should say I did not attempt to create a new view as I felt it enough of a challenge as it was and did not want to overload myself with problems. Instead I just added a textfield for the user to input their answer. I am wondering, however, if this is part of the problem as the warning message I am getting in XCode is 'Modifying State during view update, this will cause undefined behaviour' (all occurring in the 'multiplication2' function below). Unfortunately, I built it initially on the ipad and got none of these errors (although it worked unpredictably, it didn't show any warnings). I really have no idea how to go about creating a different view and am not sure if there is a way round this. Any advice would be welcome. As an aside, after experiencing these issues, I looked at Paul's hints and tried to approach it in the suggested way, building a question struct and creating a question array but again reached a dead end trying to transfer the array builder function (which all worked fine in playgrounds) from the struct and making it work in the main view struct on the app (keep getting idex of range errors) so have now reached dead ends from two different approaches which is all very frustrating. I've included the main bits of the code from my first approach below. I also made a question generator and various alerts that I have attached to a button that the user presses to get the next question (I can add these if helpful). As I say, it works as expected up to 2 times table but above that stops recognising the answers as correct and so doesn't score properly.
This is the textfield:
this is the timestable difficulty level picker
|
|
Well... your You will probably need to take Paul's advice and create a separate function Then, you will want a separate function called Then, you will need another separate function So, I would start by adding this property to hold all of your questions as
Then work on creating this function...
|
|
Many thanks for your response @Fly0strich . The function is indeed 'wild.' The problem I have been having is making certain variables accessible across different methods. The @State wrappers and computed properties etc. can work to a point but then sometimes I reach a dead end and that's why things get shoehorned into the same function. I'm guessing this could be where structs come in useful. I didn't think to use them initially as my brain is still to some extent compartmentalising the playground concepts and practical swiftUI. I tend to draw on ideas from apps we have already made which can work to an extent but not always. Anyway, thanks for the advice. I hadn't thought at all about dictionaries as I can hardly remember doing them but, having looked through my notes, I'm guessing you mention them as they allow you to store strings and integers together but as discrete values. I created the struct below in a playground mainly to see if I could make a timestable generator based on difficulty level. Anyway it works and I created arrays easily enough, I couldn't then work out how to create a dictionary from the loop. The next thing, of course, will be how to then transfer this to the @State property you mention (or should I make the function within the main view (can I do that?)?). I did wonder if I could use the arrays by somehow mapping the corresponding indexes to one another but I was geting string/integer conflict errors, so that didn't seem viable. Thanks again for your help and apologies for my probably quite basic questions!
|
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
I would rename your Dictionary to something like
Any variables that you declare will basically be accessible to anything within the same set of curly braces that they are declared in, but nowhere outside of it. For example...
|
|
Thanks again for your advice and explanation. My main issue with variables has been when declaring them in relation to another variable. 'answer' in the function I first posted for instance. If I declare it in the main view it will throw up an error message about property initilaisers not running before 'self is available.' If I put it in the question function, however it will be available but only to the question function and unless I can get it to return 2 values then I have no way to access a value for an answer checking function. I can see with a dictionary you get two values at the same time so perhaps that does solve the problem. Now I'm not sure whether I should just try to generate a dictionary from a function within the content view or use the question struct. I actually did just try the former within a playground but it gave error messages (String.Type cannot conform to hashable - no idea what that means but I could have just gone about it in the wrong way). Anyway, I'll have another play around and see if I can get anywhere with either approach.... Thanks again |
|
If you try to do it this way, you will run into those errors.
This is because when you create an instance of any struct (like One way to get around this is by using a computed property.
This way, Another option is to just set
In this example, we are just setting all of our properties to zero to begin with, and then using our One final way that you should be aware of to get around having to initialize all of your properties is using optionals.
This is very similar to our last example, but you can see that
That just makes sure that a zero would show in the text view in that case. |
|
Thanks again for all this. It is good to have a deeper understanding of what is going on. With regard to computed properties, I think I did use them initially but for some reason the answer to the question was not syncing with the question, so I think I then abandoned the idea. Part of my problem is if things don't work, I don't always know why and so possibly move on prematurely. Anyway, I do have another question(sorry) I am struggling to get the dictonary to return a value from the key - it is strange because it works in playgrounds but not in SWiftUI. In Playgrounds, it didn't work initially but then I adjusted the spacing and it was fine. This didn't work in SWiftUI however. I tried using both a nil coalescing operator and a default value but neither worked and just returned 0. The function is using values from the Question struct. It is not at all organised at this stage as I am just trying to make sure that values are pulling through. I am just unclear why it is not returning a value.
Question(difficultyLevel: 2, twoToTwelve: ClosedRange(2...12), answer: 24, questions: ["11 x 2 = ?": 22, "5 x 2 = ?": 10, "8 x 2 = ?": 16, "2 x 2 = ?": 4, "12 x 2 = ?": 24, "3 x 2 = ?": 6, "4 x 2 = ?": 8, "6 x 2 = ?": 12, "9 x 2 = ?": 18, "7 x 2 = ?": 14, "10 x 2 = ?": 20]) 0 0 ()
|
|
update - dictionary does return values when created in a function within contentView just not from the struct mutating function - so maybe I'll do that! |
|
It's hard to say what the problem that you are running into is coming from in that case, without seeing more of your code. But for one, I'm not exactly sure what your This is declared in the scope of my custom View in my project, but you could declare it in ContentView if that is where your
I'll show you how I created my
But in my project, I allowed all values from 0-12. If you want to only allow numbers > 1 you could easily modify those ranges. Then, the You'll often see Paul suggest this along the way of this course, but one thing to keep in mind when trying to create a project is to think small at first. Don't try to build your whole project at once. First just think, "What is the minimum requirement that I need to meet to make this project functional?" In this case, it is basically just "I need to generate all possible questions based on the selected But so far, we have no real need to create a separate struct for our questions, because the dictionary is capable of holding all of the variables that we need to use for them in one place. |
|
Thank you again for your help. I used the struct because Paul suggests it in the hints section of the problem and I was not getting too far with my other approach. That was the only reason, bnut possibly just over compicated it for me. Regarding the functions mentioned, I did set up this questionGenerator yesterday and it worked but then when I go to enter text in the textfield, it starts to change the factors in the question field. Looking at it in XCode I am getting errors again relating to the 'view '. I thought perhaps it is because the function is in the question generator but when I put the same as a separate askQuestion function it doesn't seem to return anything. I made an answer checker separately but I can't check it because I can't generate a question. With an array I just use array.shuffle() and it makes it send out a new value. I don't know if the dictionary needs something like this to send out a new value. It is mightily frustrating to keep hitting these brick walls I must say.
|
|
I'm sorry, I've probably just been making things more confusing for you then with using the dictionary. I just looked at the project instructions, and it does say to use a Question struct, and then add each Question to an array there. I'm not sure now if the instructions have changed since I made this project, or if I just chose to use a dictionary on my own. But, you should be able to either use a Question struct, which has a If you want to use the
Then you would want to declare this property in whichever View struct your generateQuestions() function is in
Then, you could modify the function I provided above to this, to get basically the same result.
As you can see from comparing this to my last response, the code won't change much either way you choose to do it. |
|
Thanks again. Unfortunately I find this is adding both the question and the answer to the array. When I go to print(questions[0]) it prints out the whole struct. I can't just therefore pull out the question. It gives an error if I try to put ("(questions[0]) in a text box, so I think I am going to have to have another think. I will have a look at some of the other posts and try and put something together. I don't really want to take up any more of your time with it(or too much of my own!). |
|
Yes, |
|
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.
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.