|
"Change the user interface so that it always shows their recommended bedtime using a nice and large font. You should be able to remove the “Calculate” button entirely." Can somebody please send me in a good direction? I really want to resolve it myself but I don't even understand where to begin and start feeling a bit lost. I get the idea of having the text in the main screen, removing the button. sorry I feel really stuppid not understanding this. I think I just missing out on something maybe really simple. |
|
I have something working. But I really don't understand why it works :/. What I did:
and in the View
I see a time. It goes up/down when drinking more/less coffee, or change anything else. But I do thinnk there is a much more cleaner/ better way to do this. |
|
Suzanne finds herself in a very common situation:
What's really helped me is using the Rubber Duck technique. See -> Rubber Duck Technique 🐤 If you search the forums for my handle, you'll see that I use this technique in most of my answers. This helped me tremendously along my 100 Day path. I started by commenting each and every line as if I were explaining to a new developer. Then I started writing answers to forum posts by trying to explain the answer to myself. Along the way, others pitched in to clarify my directions and understandings. But this is second nature to me now. I encourage you to follow along. If you're rankled because you just don't get it, I'd say don't worry. Get used to stubbing your toes on the code. But keep explaining what you're doing with comments in your code.
Where do you use bedTime variable?Now how are you using this calculated variable? Let's find out! Here you have a
Perhaps you want to display a message saying "Cannot calculate bed time." or something similar? |
|
About the more comments; totally agree. A lot of times I start with the comments. But then things don't work, so try something different, the comments doesn't fit the different thing. So I end up without any comments. About the usage of "wakeUp". That is wrong indeed. I just needed something there while testing (see if calculated variable works). But it really needs to go there indeed. But what I made, was it really a solution? It just feels strange tbh |
|
I think you nailed it. Creating computed variables is a key Swift concept. Nicely done. I pulled up my code from a year ago. I had a function named For example if the user changed the cups of coffee, or a desired amount of sleep, I called the function using the .onChange() modifier. However, I needed this modifier on each view in the application where the user could change parameters. This was way more code to keep updated. I think your approach is much better.
|
|
Thank you for you solution. In the version before the challange it was a function that run when clicking the button. But the button needed to go. But everything I tried said that I couldn't run the function. Don't know the exact error anymore. Searching online give me the idea that is was not possible to use the function. So that is why changed to this. |
|
(solved thanks to the comments here) My learning: as I understood the challenge, "idealBedTime" could be a computed property or a value from a function call (.onAppear and .onChange). The only thing that worked right away for me was .onAppear on the Text(idealBedTime).
But I couldn't write a computed property by simply moving over code from calculateBedTime function, I get "Errors thrown from here are not handled". But suziecode has already successfully used a computed property above, so I guess I just need to figure out the "try" or "try?" and handle optionals, etc. I need to spend sometime figuring this code out. I also tried the onChange (on wakeUp, sleepAmount, coffeeAmount) by myself, but was not able to get the proper syntax, until I saw Obelix's code. Many thanks!! Below is my functional code (not with the computed property), but with onChange.
|
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.