|
I'm sure this is terrible coding but this is how I achieved the Challenge, pointers are much appreciated:
struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } } |
|
I did something similar:
Did you try the third challenge?
I tried as you can see in my code above, but that sets the Amount per person at $-NaN until I enter a value into both text fields. Not sure how to fix that... Otherwise it works fine! |
|
@joforsell I solved that small annoyance of showing infinity or -NaN
|
|
Hi All, Thanks to some of your help , I got inspired and solved the challenge!! Although, I see Amount per person as $-NaN as default , everything else seems ok !! I am sure there must be many ways to fine tune it !! however , below is my code !! import SwiftUI struct ContentView: View {
// Picker("Number of people", selection: $numberOfPeople) { // ForEach(2 ..< 100) { // Text("($0) people") // } // } TextField("Number of people", text:$numberOfPeople) // changed picker to text. .keyboardType(.decimalPad)
struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |
|
Hello @pradeephoskote, Change the following code:
to this:
Hope that solves your problem. |
|
I thought users might want to know how much their tip is, so I added that section, but I get an error "cannot find tipValue in scope". same with grandTotal, even though they are declared near the beginning. Not sure what's going on. P.S. I also fixed the -NaN issue with an if statement
|
|
You need to declare them outside of |
|
Where/How do I declare it? If I copy it under tipPercentages, I get the following error: "Cannot use instance member 'numberOfPeople' within property initializer; property initializers run before 'self' is available"
|
|
Yes, it is under |
|
I made those variables @State private var (not sure if they needed to be private var or not) but I still get the same error message. "Cannot use instance member 'numberOfPeople' within property initializer; property initializers run before 'self' is available" for all 3 variables
|
|
A couple things, now that you tried it: 1- You need to initialize them first in the declaration, then you can use them as per your first example, as part of the computed property. This will stop the error from showing within the section. (referring to the comment of where you show the declaration and error locations). 2- Alternatively you could create new computed properties:
which you can declare after 3- Or if you prefer you can use functions that return a double. I personally prefer that approach as it allows me to see things clearly and perform any safety checks I want without cluttering my code... but it's not "better"... just a preference.
PS: I apologize for not clarifying it earlier, but the error you were getting first, is in reference to scope. Essentially, your properties were declared inside the scope of your computed property and therefore are not accessible outside of it. So when you declare properties, they can be accessed by anything within the same scope or deeper. The use of The second error (which I am apologizing for) is due to the usage of other properties to initialize them. So I hope this explains things clearly. |
|
I'm sorry, but I'm a beginner. I don't understand your vocabulary when you say "You need to initialize them first in the declaration" or "computed property". And sorry this is a lot to ask, but: I've tried to put the code you provided but I keep getting errors. 1. Do I delete the @State private vars of peopleCount, tipSelection, and orderAmount? 2. If I don't, I still get the error I've describe in my earlier posts, but if I delete those then I get an error on let tipValue and grandTotal: "Cannot find 'orderAmount' in scope"
|
|
There's no need to apologize, we're all here learning. By initialize I mean similar to what you are doing with tipPercentage. You are initializing it with the value 2. Basically you are giving it some initial value. The computed property in this example, which I recommend you go over the tutorial for that again, means it is calculated instead of having a set value... hope that explains it. What you have in your example, is duplicates. You have the @State outside of your totalPerPerson and then you have them again inside. You can remove the ones inside now. Then make tipValue and grandTotal @State too so you can update the UI accordingly, and remove them from inside totalPerPerson. You also don't need the orderTotal() function if you are using grandTotal. This was a preference. You use either the function or the property. Your code should look like this:
Important Note: The only reason for placing tipValue, orderAmount and grandTotal outside of totalPerPerson is in order for you to access them for your additional feature. You could have kept everything as per the tutorial, and then use totalPerPerson as your baseline, in order to calculate the new details you want, which is why I provided the example of the function... if you notice it uses totalPerPerson, and multiplies it by peopleCount (which is declared inside the function again)... but this is only if you keep the format of the tutorial... Sorry it takes me time to reply, it all depends on free time and memory. Let me know if you have any more questions on this. |
|
I totally understand that your time is limited and I truly appreciate you taking the time to reply to all of my threads. I copied your code and pasted into mine and I still get the "Cannot use instance member 'tipPercentage' within property initializer; property initializers run before 'self' is available". I also get the error "Cannot find '$tipPercentage' in scope" in my picker and cannot find 'numberOfPeople' in scope" in my grandTotal var even though we declared them at the top with @State(s) |
|
I really should stop checking code late at night. Sorry about that... I don't know what I was thinking. That was the reason I was suggesting the use of functions. But anyway, here's what I should have pasted with comments for clarity. Again sorry. One more thing: you cannot use
This was a good lesson for me too. 😉. Always check and refactor code when you are well rested, otherwise the above mistake happens. We could have turned |
|
This seems like it works even though it rounds up is some cases. struct ContentView: View {
} |
|
Try this: import SwiftUI struct ContentView: View { @State private var checkAmount = "" @State private var numberOfPeople = "" @State private var tipPercentage = 2
} struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |
|
@MarcusKay I think your code is working. The only thing is, there is a large space at the top and so users have to scroll down a tiny bit to see the amount per person textfield instead of everything viewable on one page. Is there anything that can be done about that? thanks |
|
That space on top has to do with I can't really know why your code is doing that... check the layout. |
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.